|  | 
| 17 | 17 | //!  - Add the activation logic in [`default_configuration`] | 
| 18 | 18 | //!  - Add the cfg to [`CheckCfg::fill_well_known`] (and related files), | 
| 19 | 19 | //!    so that the compiler can know the cfg is expected | 
|  | 20 | +//!  - Add the cfg in [`disallow_cfgs`] to disallow users from setting it via `--cfg` | 
| 20 | 21 | //!  - Add the feature gating in `compiler/rustc_feature/src/builtin_attrs.rs` | 
| 21 | 22 | 
 | 
|  | 23 | +use rustc_ast::ast; | 
| 22 | 24 | use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexSet}; | 
|  | 25 | +use rustc_lint_defs::builtin::UNEXPECTED_BUILTIN_CFGS; | 
|  | 26 | +use rustc_lint_defs::BuiltinLintDiag; | 
| 23 | 27 | use rustc_span::symbol::{sym, Symbol}; | 
| 24 | 28 | use rustc_target::abi::Align; | 
| 25 | 29 | use rustc_target::spec::{PanicStrategy, RelocModel, SanitizerSet}; | 
| @@ -83,6 +87,67 @@ impl<'a, T: Eq + Hash + Copy + 'a> Extend<&'a T> for ExpectedValues<T> { | 
| 83 | 87 |     } | 
| 84 | 88 | } | 
| 85 | 89 | 
 | 
|  | 90 | +/// Disallow builtin cfgs from the CLI. | 
|  | 91 | +pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) { | 
|  | 92 | +    let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| { | 
|  | 93 | +        let cfg_name = cfg.0; | 
|  | 94 | +        let cfg = if let Some(value) = cfg.1 { | 
|  | 95 | +            format!(r#"{}="{}""#, cfg_name, value) | 
|  | 96 | +        } else { | 
|  | 97 | +            format!("{}", cfg_name) | 
|  | 98 | +        }; | 
|  | 99 | +        sess.psess.opt_span_buffer_lint( | 
|  | 100 | +            UNEXPECTED_BUILTIN_CFGS, | 
|  | 101 | +            None, | 
|  | 102 | +            ast::CRATE_NODE_ID, | 
|  | 103 | +            BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }, | 
|  | 104 | +        ) | 
|  | 105 | +    }; | 
|  | 106 | + | 
|  | 107 | +    // We want to restrict setting builtin cfgs that will produce incoherent behavior | 
|  | 108 | +    // between the cfg and the rustc cli flag that sets it. | 
|  | 109 | +    // | 
|  | 110 | +    // The tests are in tests/ui/cfg/disallowed-cli-cfgs.rs. | 
|  | 111 | + | 
|  | 112 | +    // By-default all builtin cfgs are disallowed, only those are allowed: | 
|  | 113 | +    //  - test: as it makes sense to the have the `test` cfg active without the builtin | 
|  | 114 | +    //          test harness. See Cargo `harness = false` config. | 
|  | 115 | +    // | 
|  | 116 | +    // Cargo `--cfg test`: https://github.com/rust-lang/cargo/blob/bc89bffa5987d4af8f71011c7557119b39e44a65/src/cargo/core/compiler/mod.rs#L1124 | 
|  | 117 | + | 
|  | 118 | +    for cfg in user_cfgs { | 
|  | 119 | +        match cfg { | 
|  | 120 | +            (sym::overflow_checks, None) => disallow(cfg, "-C overflow-checks"), | 
|  | 121 | +            (sym::debug_assertions, None) => disallow(cfg, "-C debug-assertions"), | 
|  | 122 | +            (sym::ub_checks, None) => disallow(cfg, "-Z ub-checks"), | 
|  | 123 | +            (sym::sanitize, None | Some(_)) => disallow(cfg, "-Z sanitizer"), | 
|  | 124 | +            ( | 
|  | 125 | +                sym::sanitizer_cfi_generalize_pointers | sym::sanitizer_cfi_normalize_integers, | 
|  | 126 | +                None | Some(_), | 
|  | 127 | +            ) => disallow(cfg, "-Z sanitizer=cfi"), | 
|  | 128 | +            (sym::proc_macro, None) => disallow(cfg, "--crate-type proc-macro"), | 
|  | 129 | +            (sym::panic, Some(sym::abort | sym::unwind)) => disallow(cfg, "-C panic"), | 
|  | 130 | +            (sym::target_feature, Some(_)) => disallow(cfg, "-C target-feature"), | 
|  | 131 | +            (sym::unix, None) | 
|  | 132 | +            | (sym::windows, None) | 
|  | 133 | +            | (sym::relocation_model, Some(_)) | 
|  | 134 | +            | (sym::target_abi, None | Some(_)) | 
|  | 135 | +            | (sym::target_arch, Some(_)) | 
|  | 136 | +            | (sym::target_endian, Some(_)) | 
|  | 137 | +            | (sym::target_env, None | Some(_)) | 
|  | 138 | +            | (sym::target_family, Some(_)) | 
|  | 139 | +            | (sym::target_os, Some(_)) | 
|  | 140 | +            | (sym::target_pointer_width, Some(_)) | 
|  | 141 | +            | (sym::target_vendor, None | Some(_)) | 
|  | 142 | +            | (sym::target_has_atomic, Some(_)) | 
|  | 143 | +            | (sym::target_has_atomic_equal_alignment, Some(_)) | 
|  | 144 | +            | (sym::target_has_atomic_load_store, Some(_)) | 
|  | 145 | +            | (sym::target_thread_local, None) => disallow(cfg, "--target"), | 
|  | 146 | +            _ => {} | 
|  | 147 | +        } | 
|  | 148 | +    } | 
|  | 149 | +} | 
|  | 150 | + | 
| 86 | 151 | /// Generate the default configs for a given session | 
| 87 | 152 | pub(crate) fn default_configuration(sess: &Session) -> Cfg { | 
| 88 | 153 |     let mut ret = Cfg::default(); | 
|  | 
0 commit comments