Skip to content

Commit f96fdab

Browse files
authored
Rollup merge of #133221 - Urgau:check-cfg-macro-diag, r=jieyouxu
Add external macros specific diagnostics for check-cfg This PR adds specific check-cfg diagnostics for unexpected cfg in external macros. As well as hiding the some of the Cargo specific help/suggestions as they distraction for external macros and are generally not the right solution. Follow-up to #132577 `@rustbot` label +L-unexpected_cfgs r? compiler
2 parents f5079d0 + e2fbeec commit f96fdab

File tree

8 files changed

+211
-29
lines changed

8 files changed

+211
-29
lines changed

compiler/rustc_lint/messages.ftl

+4
Original file line numberDiff line numberDiff line change
@@ -806,10 +806,14 @@ lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_printl
806806
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
807807
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}
808808
lint_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}`
809+
lint_unexpected_cfg_cargo_update = the {$macro_kind} `{$macro_name}` may come from an old version of it's defining crate, try updating your dependencies with `cargo update`
810+
809811
lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml`
810812
lint_unexpected_cfg_doc_cargo = see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
811813
lint_unexpected_cfg_doc_rustc = see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
812814
815+
lint_unexpected_cfg_from_external_macro_origin = using a cfg inside a {$macro_kind} will use the cfgs from the destination crate and not the ones from the defining crate
816+
lint_unexpected_cfg_from_external_macro_refer = try refering to `{$macro_name}` crate for guidance on how handle this unexpected cfg
813817
lint_unexpected_cfg_name = unexpected `cfg` condition name: `{$name}`
814818
lint_unexpected_cfg_name_expected_names = expected names are: {$possibilities}{$and_more ->
815819
[0] {""}

compiler/rustc_lint/src/context/diagnostics/check_cfg.rs

+58-10
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1+
use rustc_hir::def_id::LOCAL_CRATE;
12
use rustc_middle::bug;
23
use rustc_session::Session;
34
use rustc_session::config::ExpectedValues;
45
use rustc_span::edit_distance::find_best_match_for_name;
56
use rustc_span::symbol::Ident;
6-
use rustc_span::{Span, Symbol, sym};
7+
use rustc_span::{ExpnKind, Span, Symbol, sym};
78

89
use crate::lints;
910

@@ -60,6 +61,35 @@ fn cargo_help_sub(
6061
}
6162
}
6263

64+
fn rustc_macro_help(span: Span) -> Option<lints::UnexpectedCfgRustcMacroHelp> {
65+
let oexpn = span.ctxt().outer_expn_data();
66+
if let Some(def_id) = oexpn.macro_def_id
67+
&& let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
68+
&& def_id.krate != LOCAL_CRATE
69+
{
70+
Some(lints::UnexpectedCfgRustcMacroHelp { macro_kind: macro_kind.descr(), macro_name })
71+
} else {
72+
None
73+
}
74+
}
75+
76+
fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
77+
let oexpn = span.ctxt().outer_expn_data();
78+
if let Some(def_id) = oexpn.macro_def_id
79+
&& let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
80+
&& def_id.krate != LOCAL_CRATE
81+
{
82+
Some(lints::UnexpectedCfgCargoMacroHelp {
83+
macro_kind: macro_kind.descr(),
84+
macro_name,
85+
// FIXME: Get access to a `TyCtxt` from an `EarlyContext`
86+
// crate_name: cx.tcx.crate_name(def_id.krate),
87+
})
88+
} else {
89+
None
90+
}
91+
}
92+
6393
pub(super) fn unexpected_cfg_name(
6494
sess: &Session,
6595
(name, name_span): (Symbol, Span),
@@ -85,6 +115,7 @@ pub(super) fn unexpected_cfg_name(
85115
};
86116

87117
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
118+
let is_from_external_macro = rustc_middle::lint::in_external_macro(sess, name_span);
88119
let mut is_feature_cfg = name == sym::feature;
89120

90121
let code_sugg = if is_feature_cfg && is_from_cargo {
@@ -185,12 +216,21 @@ pub(super) fn unexpected_cfg_name(
185216
};
186217

187218
let invocation_help = if is_from_cargo {
188-
let sub = if !is_feature_cfg { Some(cargo_help_sub(sess, &inst)) } else { None };
189-
lints::unexpected_cfg_name::InvocationHelp::Cargo { sub }
219+
let help = if !is_feature_cfg && !is_from_external_macro {
220+
Some(cargo_help_sub(sess, &inst))
221+
} else {
222+
None
223+
};
224+
lints::unexpected_cfg_name::InvocationHelp::Cargo {
225+
help,
226+
macro_help: cargo_macro_help(name_span),
227+
}
190228
} else {
191-
lints::unexpected_cfg_name::InvocationHelp::Rustc(lints::UnexpectedCfgRustcHelp::new(
192-
&inst(EscapeQuotes::No),
193-
))
229+
let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No));
230+
lints::unexpected_cfg_name::InvocationHelp::Rustc {
231+
help,
232+
macro_help: rustc_macro_help(name_span),
233+
}
194234
};
195235

196236
lints::UnexpectedCfgName { code_sugg, invocation_help, name }
@@ -216,7 +256,9 @@ pub(super) fn unexpected_cfg_value(
216256
.copied()
217257
.flatten()
218258
.collect();
259+
219260
let is_from_cargo = rustc_session::utils::was_invoked_from_cargo();
261+
let is_from_external_macro = rustc_middle::lint::in_external_macro(sess, name_span);
220262

221263
// Show the full list if all possible values for a given name, but don't do it
222264
// for names as the possibilities could be very long
@@ -284,25 +326,31 @@ pub(super) fn unexpected_cfg_value(
284326
};
285327

286328
let invocation_help = if is_from_cargo {
287-
let help = if name == sym::feature {
329+
let help = if name == sym::feature && !is_from_external_macro {
288330
if let Some((value, _value_span)) = value {
289331
Some(lints::unexpected_cfg_value::CargoHelp::AddFeature { value })
290332
} else {
291333
Some(lints::unexpected_cfg_value::CargoHelp::DefineFeatures)
292334
}
293-
} else if can_suggest_adding_value {
335+
} else if can_suggest_adding_value && !is_from_external_macro {
294336
Some(lints::unexpected_cfg_value::CargoHelp::Other(cargo_help_sub(sess, &inst)))
295337
} else {
296338
None
297339
};
298-
lints::unexpected_cfg_value::InvocationHelp::Cargo(help)
340+
lints::unexpected_cfg_value::InvocationHelp::Cargo {
341+
help,
342+
macro_help: cargo_macro_help(name_span),
343+
}
299344
} else {
300345
let help = if can_suggest_adding_value {
301346
Some(lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)))
302347
} else {
303348
None
304349
};
305-
lints::unexpected_cfg_value::InvocationHelp::Rustc(help)
350+
lints::unexpected_cfg_value::InvocationHelp::Rustc {
351+
help,
352+
macro_help: rustc_macro_help(name_span),
353+
}
306354
};
307355

308356
lints::UnexpectedCfgValue {

compiler/rustc_lint/src/lints.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -2172,6 +2172,25 @@ impl UnexpectedCfgRustcHelp {
21722172
}
21732173
}
21742174

2175+
#[derive(Subdiagnostic)]
2176+
#[note(lint_unexpected_cfg_from_external_macro_origin)]
2177+
#[help(lint_unexpected_cfg_from_external_macro_refer)]
2178+
pub(crate) struct UnexpectedCfgRustcMacroHelp {
2179+
pub macro_kind: &'static str,
2180+
pub macro_name: Symbol,
2181+
}
2182+
2183+
#[derive(Subdiagnostic)]
2184+
#[note(lint_unexpected_cfg_from_external_macro_origin)]
2185+
#[help(lint_unexpected_cfg_from_external_macro_refer)]
2186+
#[help(lint_unexpected_cfg_cargo_update)]
2187+
pub(crate) struct UnexpectedCfgCargoMacroHelp {
2188+
pub macro_kind: &'static str,
2189+
pub macro_name: Symbol,
2190+
// FIXME: Figure out a way to get the crate name
2191+
// crate_name: String,
2192+
}
2193+
21752194
#[derive(LintDiagnostic)]
21762195
#[diag(lint_unexpected_cfg_name)]
21772196
pub(crate) struct UnexpectedCfgName {
@@ -2276,10 +2295,17 @@ pub(crate) mod unexpected_cfg_name {
22762295
#[note(lint_unexpected_cfg_doc_cargo)]
22772296
Cargo {
22782297
#[subdiagnostic]
2279-
sub: Option<super::UnexpectedCfgCargoHelp>,
2298+
macro_help: Option<super::UnexpectedCfgCargoMacroHelp>,
2299+
#[subdiagnostic]
2300+
help: Option<super::UnexpectedCfgCargoHelp>,
22802301
},
22812302
#[note(lint_unexpected_cfg_doc_rustc)]
2282-
Rustc(#[subdiagnostic] super::UnexpectedCfgRustcHelp),
2303+
Rustc {
2304+
#[subdiagnostic]
2305+
macro_help: Option<super::UnexpectedCfgRustcMacroHelp>,
2306+
#[subdiagnostic]
2307+
help: super::UnexpectedCfgRustcHelp,
2308+
},
22832309
}
22842310
}
22852311

@@ -2382,9 +2408,19 @@ pub(crate) mod unexpected_cfg_value {
23822408
#[derive(Subdiagnostic)]
23832409
pub(crate) enum InvocationHelp {
23842410
#[note(lint_unexpected_cfg_doc_cargo)]
2385-
Cargo(#[subdiagnostic] Option<CargoHelp>),
2411+
Cargo {
2412+
#[subdiagnostic]
2413+
help: Option<CargoHelp>,
2414+
#[subdiagnostic]
2415+
macro_help: Option<super::UnexpectedCfgCargoMacroHelp>,
2416+
},
23862417
#[note(lint_unexpected_cfg_doc_rustc)]
2387-
Rustc(#[subdiagnostic] Option<super::UnexpectedCfgRustcHelp>),
2418+
Rustc {
2419+
#[subdiagnostic]
2420+
help: Option<super::UnexpectedCfgRustcHelp>,
2421+
#[subdiagnostic]
2422+
macro_help: Option<super::UnexpectedCfgRustcMacroHelp>,
2423+
},
23882424
}
23892425

23902426
#[derive(Subdiagnostic)]

tests/ui/check-cfg/auxiliary/cfg_macro.rs

+16
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,19 @@ macro_rules! my_lib_macro {
99
$crate::my_lib_func()
1010
};
1111
}
12+
13+
#[macro_export]
14+
macro_rules! my_lib_macro_value {
15+
() => {
16+
#[cfg(panic = "UNEXPECTED_VALUE")]
17+
$crate::my_lib_func()
18+
};
19+
}
20+
21+
#[macro_export]
22+
macro_rules! my_lib_macro_feature {
23+
() => {
24+
#[cfg(feature = "UNEXPECTED_FEATURE")]
25+
$crate::my_lib_func()
26+
};
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
warning: unexpected `cfg` condition name: `my_lib_cfg`
2+
--> $DIR/report-in-external-macros.rs:13:5
3+
|
4+
LL | cfg_macro::my_lib_macro!();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`
8+
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
9+
= help: try refering to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg
10+
= help: the macro `cfg_macro::my_lib_macro` may come from an old version of it's defining crate, try updating your dependencies with `cargo update`
11+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
12+
= note: `#[warn(unexpected_cfgs)]` on by default
13+
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
warning: unexpected `cfg` condition value: `UNEXPECTED_VALUE`
16+
--> $DIR/report-in-external-macros.rs:16:5
17+
|
18+
LL | cfg_macro::my_lib_macro_value!();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= note: expected values for `panic` are: `abort` and `unwind`
22+
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
23+
= help: try refering to `cfg_macro::my_lib_macro_value` crate for guidance on how handle this unexpected cfg
24+
= help: the macro `cfg_macro::my_lib_macro_value` may come from an old version of it's defining crate, try updating your dependencies with `cargo update`
25+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
26+
= note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info)
27+
28+
warning: unexpected `cfg` condition value: `UNEXPECTED_FEATURE`
29+
--> $DIR/report-in-external-macros.rs:19:5
30+
|
31+
LL | cfg_macro::my_lib_macro_feature!();
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
33+
|
34+
= note: no expected values for `feature`
35+
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
36+
= help: try refering to `cfg_macro::my_lib_macro_feature` crate for guidance on how handle this unexpected cfg
37+
= help: the macro `cfg_macro::my_lib_macro_feature` may come from an old version of it's defining crate, try updating your dependencies with `cargo update`
38+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
39+
= note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info)
40+
41+
warning: 3 warnings emitted
42+

tests/ui/check-cfg/report-in-external-macros.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,19 @@
33

44
//@ check-pass
55
//@ no-auto-check-cfg
6+
//@ revisions: cargo rustc
7+
//@ [rustc]unset-rustc-env:CARGO_CRATE_NAME
8+
//@ [cargo]rustc-env:CARGO_CRATE_NAME=foo
69
//@ aux-crate: cfg_macro=cfg_macro.rs
7-
//@ compile-flags: --check-cfg=cfg()
10+
//@ compile-flags: --check-cfg=cfg(feature,values())
811

912
fn main() {
1013
cfg_macro::my_lib_macro!();
1114
//~^ WARNING unexpected `cfg` condition name
15+
16+
cfg_macro::my_lib_macro_value!();
17+
//~^ WARNING unexpected `cfg` condition value
18+
19+
cfg_macro::my_lib_macro_feature!();
20+
//~^ WARNING unexpected `cfg` condition value
1221
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
warning: unexpected `cfg` condition name: `my_lib_cfg`
2+
--> $DIR/report-in-external-macros.rs:13:5
3+
|
4+
LL | cfg_macro::my_lib_macro!();
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
|
7+
= help: expected names are: `clippy`, `debug_assertions`, `doc`, `doctest`, `feature`, `fmt_debug`, `miri`, `overflow_checks`, `panic`, `proc_macro`, `relocation_model`, `rustfmt`, `sanitize`, `sanitizer_cfi_generalize_pointers`, `sanitizer_cfi_normalize_integers`, `target_abi`, `target_arch`, `target_endian`, `target_env`, `target_family`, `target_feature`, `target_has_atomic`, `target_has_atomic_equal_alignment`, `target_has_atomic_load_store`, `target_os`, `target_pointer_width`, `target_thread_local`, `target_vendor`, `test`, `ub_checks`, `unix`, and `windows`
8+
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
9+
= help: try refering to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg
10+
= help: to expect this configuration use `--check-cfg=cfg(my_lib_cfg)`
11+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
12+
= note: `#[warn(unexpected_cfgs)]` on by default
13+
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
14+
15+
warning: unexpected `cfg` condition value: `UNEXPECTED_VALUE`
16+
--> $DIR/report-in-external-macros.rs:16:5
17+
|
18+
LL | cfg_macro::my_lib_macro_value!();
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
20+
|
21+
= note: expected values for `panic` are: `abort` and `unwind`
22+
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
23+
= help: try refering to `cfg_macro::my_lib_macro_value` crate for guidance on how handle this unexpected cfg
24+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
25+
= note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info)
26+
27+
warning: unexpected `cfg` condition value: `UNEXPECTED_FEATURE`
28+
--> $DIR/report-in-external-macros.rs:19:5
29+
|
30+
LL | cfg_macro::my_lib_macro_feature!();
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
32+
|
33+
= note: no expected values for `feature`
34+
= help: to expect this configuration use `--check-cfg=cfg(feature, values("UNEXPECTED_FEATURE"))`
35+
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
36+
= help: try refering to `cfg_macro::my_lib_macro_feature` crate for guidance on how handle this unexpected cfg
37+
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg.html> for more information about checking conditional configuration
38+
= note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info)
39+
40+
warning: 3 warnings emitted
41+

tests/ui/check-cfg/report-in-external-macros.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)