Skip to content

Commit 7b0a2b3

Browse files
committed
Add has_cfg_version cfg
1 parent 01fe331 commit 7b0a2b3

File tree

7 files changed

+29
-5
lines changed

7 files changed

+29
-5
lines changed

compiler/rustc_lint/messages.ftl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,9 +810,10 @@ lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::Manual
810810
.suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value
811811
812812
lint_unexpected_builtin_cfg = unexpected `--cfg {$cfg}` flag
813-
.controlled_by = config `{$cfg_name}` is only supposed to be controlled by `{$controlled_by}`
814813
.incoherent = manually setting a built-in cfg can and does create incoherent behaviors
815814
815+
lint_unexpected_builtin_cfg_controlled_by = config `{$cfg_name}` is only supposed to be controlled by `{$controlled_by}`
816+
816817
lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_println}` to the top of the `build.rs`
817818
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
818819
lint_unexpected_cfg_add_cargo_toml_lint_cfg = or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:{$cargo_toml_lint_cfg}

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,13 @@ pub(super) fn decorate_lint(
448448
lints::OutOfScopeMacroCalls { span, path, location }.decorate_lint(diag)
449449
}
450450
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by } => {
451-
lints::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by }.decorate_lint(diag)
451+
lints::UnexpectedBuiltinCfg {
452+
cfg,
453+
cfg_name,
454+
controlled_by: controlled_by
455+
.map(|controlled_by| lints::ControlledBy { controlled_by }),
456+
}
457+
.decorate_lint(diag)
452458
}
453459
BuiltinLintDiag::ElidedNamedLifetimes { elided: (span, kind), resolution } => {
454460
match resolution {

compiler/rustc_lint/src/lints.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2498,11 +2498,17 @@ pub(crate) mod unexpected_cfg_value {
24982498

24992499
#[derive(LintDiagnostic)]
25002500
#[diag(lint_unexpected_builtin_cfg)]
2501-
#[note(lint_controlled_by)]
25022501
#[note(lint_incoherent)]
25032502
pub(crate) struct UnexpectedBuiltinCfg {
25042503
pub(crate) cfg: String,
25052504
pub(crate) cfg_name: Symbol,
2505+
#[subdiagnostic]
2506+
pub(crate) controlled_by: Option<ControlledBy>,
2507+
}
2508+
2509+
#[derive(Subdiagnostic)]
2510+
#[note(lint_unexpected_builtin_cfg)]
2511+
pub(crate) struct ControlledBy {
25062512
pub(crate) controlled_by: &'static str,
25072513
}
25082514

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ pub enum BuiltinLintDiag {
827827
UnexpectedBuiltinCfg {
828828
cfg: String,
829829
cfg_name: Symbol,
830-
controlled_by: &'static str,
830+
controlled_by: Option<&'static str>,
831831
},
832832
}
833833

compiler/rustc_session/src/config/cfg.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ impl<'a, T: Eq + Hash + Copy + 'a> Extend<&'a T> for ExpectedValues<T> {
8888

8989
/// Disallow builtin cfgs from the CLI.
9090
pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
91-
let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| {
91+
let disallow_controlled_by = |cfg: &(Symbol, Option<Symbol>), controlled_by| {
9292
let cfg_name = cfg.0;
9393
let cfg = if let Some(value) = cfg.1 {
9494
format!(r#"{}="{}""#, cfg_name, value)
@@ -102,6 +102,9 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
102102
BuiltinLintDiag::UnexpectedBuiltinCfg { cfg, cfg_name, controlled_by },
103103
)
104104
};
105+
let disallow = |cfg: &(Symbol, Option<Symbol>), controlled_by| {
106+
disallow_controlled_by(cfg, Some(controlled_by));
107+
};
105108

106109
// We want to restrict setting builtin cfgs that will produce incoherent behavior
107110
// between the cfg and the rustc cli flag that sets it.
@@ -147,6 +150,7 @@ pub(crate) fn disallow_cfgs(sess: &Session, user_cfgs: &Cfg) {
147150
| (sym::target_has_reliable_f128, None | Some(_))
148151
| (sym::target_has_reliable_f128_math, None | Some(_))
149152
| (sym::target_thread_local, None) => disallow(cfg, "--target"),
153+
(sym::has_cfg_version, None) => disallow_controlled_by(cfg, None),
150154
(sym::fmt_debug, None | Some(_)) => disallow(cfg, "-Z fmt-debug"),
151155
(sym::emscripten_wasm_eh, None | Some(_)) => disallow(cfg, "-Z emscripten_wasm_eh"),
152156
_ => {}
@@ -310,6 +314,8 @@ pub(crate) fn default_configuration(sess: &Session) -> Cfg {
310314
ins_none!(sym::contract_checks);
311315
}
312316

317+
ins_none!(sym::has_cfg_version);
318+
313319
ret
314320
}
315321

@@ -472,6 +478,8 @@ impl CheckCfg {
472478
ins!(sym::ub_checks, no_values);
473479
ins!(sym::contract_checks, no_values);
474480

481+
ins!(sym::has_cfg_version, no_values);
482+
475483
ins!(sym::unix, no_values);
476484
ins!(sym::windows, no_values);
477485
}

compiler/rustc_span/src/symbol.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ symbols! {
10891089
guard_patterns,
10901090
half_open_range_patterns,
10911091
half_open_range_patterns_in_slices,
1092+
has_cfg_version,
10921093
hash,
10931094
hashmap_contains_key,
10941095
hashmap_drain_ty,

tests/ui/cfg/disallowed-cli-cfgs.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
//@ revisions: fmt_debug_
1010
//@ revisions: emscripten_wasm_eh_
1111
//@ revisions: reliable_f16_ reliable_f16_math_ reliable_f128_ reliable_f128_math_
12+
//@ revisions: has_cfg_version_
1213

1314
//@ [overflow_checks_]compile-flags: --cfg overflow_checks
1415
//@ [debug_assertions_]compile-flags: --cfg debug_assertions
@@ -40,6 +41,7 @@
4041
//@ [reliable_f16_math_]compile-flags: --cfg target_has_reliable_f16_math
4142
//@ [reliable_f128_]compile-flags: --cfg target_has_reliable_f128
4243
//@ [reliable_f128_math_]compile-flags: --cfg target_has_reliable_f128_math
44+
//@ [has_cfg_version_]compile-flags: --cfg has_cfg_version
4345

4446
fn main() {}
4547

0 commit comments

Comments
 (0)