|
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};
|
@@ -379,3 +383,69 @@ impl CheckCfg {
|
379 | 383 | ins!(sym::windows, no_values);
|
380 | 384 | }
|
381 | 385 | }
|
| 386 | + |
| 387 | +pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) { |
| 388 | + let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| { |
| 389 | + let cfg_name = cfg.0; |
| 390 | + let cfg = if let Some(value) = cfg.1 { |
| 391 | + format!(r#"{}="{}""#, cfg_name, value) |
| 392 | + } else { |
| 393 | + format!("{}", cfg_name) |
| 394 | + }; |
| 395 | + sess.psess.opt_span_buffer_lint( |
| 396 | + UNEXPECTED_BUILTIN_CFGS, |
| 397 | + None, |
| 398 | + ast::CRATE_NODE_ID, |
| 399 | + BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }, |
| 400 | + ) |
| 401 | + }; |
| 402 | + |
| 403 | + // We want to restrict setting cfgs that will produce "incoherent" behavior between |
| 404 | + // the cfg and the "real" flag that sets it. |
| 405 | + // |
| 406 | + // Not all "well known cfgs" can be disallowed, as they are set by external tools via the |
| 407 | + // CLI, we therefore don't lint on them: |
| 408 | + // |
| 409 | + // - clippy |
| 410 | + // - doc |
| 411 | + // - doctest |
| 412 | + // - miri |
| 413 | + // - rustfmt |
| 414 | + // |
| 415 | + // doc/doctest: https://github.com/rust-lang/rust/blob/d03d6c0fead582c98c6446ec92456ca8fd03ff65/src/librustdoc/doctest.rs#L134-L135 |
| 416 | + // clippy: https://github.com/rust-lang/rust/blob/f1b0d54ca9a5ea43950c279985a6e12dcf387de8/src/tools/clippy/src/driver.rs#L276 |
| 417 | + // miri: https://github.com/rust-lang/rust/blob/d03d6c0fead582c98c6446ec92456ca8fd03ff65/src/tools/miri/src/lib.rs#L161 |
| 418 | + |
| 419 | + for cfg in user_cfgs { |
| 420 | + match cfg { |
| 421 | + (sym::test, None) => disallow(cfg, "--test"), |
| 422 | + (sym::overflow_checks, None) => disallow(cfg, "-C overflow-checks"), |
| 423 | + (sym::debug_assertions, None) => disallow(cfg, "-C debug-assertions"), |
| 424 | + (sym::ub_checks, None) => disallow(cfg, "-Z ub-checks"), |
| 425 | + (sym::sanitize, None | Some(_)) => disallow(cfg, "-Z sanitizer"), |
| 426 | + ( |
| 427 | + sym::sanitizer_cfi_generalize_pointers | sym::sanitizer_cfi_normalize_integers, |
| 428 | + None | Some(_), |
| 429 | + ) => disallow(cfg, "-Z sanitizer=cfi"), |
| 430 | + (sym::proc_macro, None) => disallow(cfg, "--crate-type proc-macro"), |
| 431 | + (sym::panic, Some(sym::abort | sym::unwind)) => disallow(cfg, "-C panic"), |
| 432 | + (sym::target_feature, Some(_)) => disallow(cfg, "-C target-feature"), |
| 433 | + (sym::unix, None) |
| 434 | + | (sym::windows, None) |
| 435 | + | (sym::relocation_model, Some(_)) |
| 436 | + | (sym::target_abi, None | Some(_)) |
| 437 | + | (sym::target_arch, Some(_)) |
| 438 | + | (sym::target_endian, Some(_)) |
| 439 | + | (sym::target_env, None | Some(_)) |
| 440 | + | (sym::target_family, Some(_)) |
| 441 | + | (sym::target_os, Some(_)) |
| 442 | + | (sym::target_pointer_width, Some(_)) |
| 443 | + | (sym::target_vendor, None | Some(_)) |
| 444 | + | (sym::target_has_atomic, Some(_)) |
| 445 | + | (sym::target_has_atomic_equal_alignment, Some(_)) |
| 446 | + | (sym::target_has_atomic_load_store, Some(_)) |
| 447 | + | (sym::target_thread_local, None) => disallow(cfg, "--target"), |
| 448 | + _ => {} |
| 449 | + } |
| 450 | + } |
| 451 | +} |
0 commit comments