Skip to content

Commit 291c519

Browse files
committed
Improve check-cfg Cargo macro diagnostic with crate name
1 parent e3e5bd9 commit 291c519

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

compiler/rustc_lint/messages.ftl

+1-1
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ 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`
809+
lint_unexpected_cfg_cargo_update = the {$macro_kind} `{$macro_name}` may come from an old version of the `{$crate_name}` crate, try updating your dependency with `cargo update -p {$crate_name}`
810810
811811
lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml`
812812
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

compiler/rustc_lint/src/early/diagnostics.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ mod check_cfg;
2121

2222
pub(super) fn decorate_lint(
2323
sess: &Session,
24-
_tcx: Option<TyCtxt<'_>>,
24+
tcx: Option<TyCtxt<'_>>,
2525
diagnostic: BuiltinLintDiag,
2626
diag: &mut Diag<'_, ()>,
2727
) {
@@ -205,10 +205,10 @@ pub(super) fn decorate_lint(
205205
.decorate_lint(diag);
206206
}
207207
BuiltinLintDiag::UnexpectedCfgName(name, value) => {
208-
check_cfg::unexpected_cfg_name(sess, name, value).decorate_lint(diag);
208+
check_cfg::unexpected_cfg_name(sess, tcx, name, value).decorate_lint(diag);
209209
}
210210
BuiltinLintDiag::UnexpectedCfgValue(name, value) => {
211-
check_cfg::unexpected_cfg_value(sess, name, value).decorate_lint(diag);
211+
check_cfg::unexpected_cfg_value(sess, tcx, name, value).decorate_lint(diag);
212212
}
213213
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
214214
let suggestion = match sugg {

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use rustc_hir::def_id::LOCAL_CRATE;
22
use rustc_middle::bug;
3+
use rustc_middle::ty::TyCtxt;
34
use rustc_session::Session;
45
use rustc_session::config::ExpectedValues;
56
use rustc_span::edit_distance::find_best_match_for_name;
@@ -73,17 +74,20 @@ fn rustc_macro_help(span: Span) -> Option<lints::UnexpectedCfgRustcMacroHelp> {
7374
}
7475
}
7576

76-
fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
77+
fn cargo_macro_help(
78+
tcx: Option<TyCtxt<'_>>,
79+
span: Span,
80+
) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
7781
let oexpn = span.ctxt().outer_expn_data();
7882
if let Some(def_id) = oexpn.macro_def_id
7983
&& let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
8084
&& def_id.krate != LOCAL_CRATE
85+
&& let Some(tcx) = tcx
8186
{
8287
Some(lints::UnexpectedCfgCargoMacroHelp {
8388
macro_kind: macro_kind.descr(),
8489
macro_name,
85-
// FIXME: Get access to a `TyCtxt` from an `EarlyContext`
86-
// crate_name: cx.tcx.crate_name(def_id.krate),
90+
crate_name: tcx.crate_name(def_id.krate),
8791
})
8892
} else {
8993
None
@@ -92,6 +96,7 @@ fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
9296

9397
pub(super) fn unexpected_cfg_name(
9498
sess: &Session,
99+
tcx: Option<TyCtxt<'_>>,
95100
(name, name_span): (Symbol, Span),
96101
value: Option<(Symbol, Span)>,
97102
) -> lints::UnexpectedCfgName {
@@ -223,7 +228,7 @@ pub(super) fn unexpected_cfg_name(
223228
};
224229
lints::unexpected_cfg_name::InvocationHelp::Cargo {
225230
help,
226-
macro_help: cargo_macro_help(name_span),
231+
macro_help: cargo_macro_help(tcx, name_span),
227232
}
228233
} else {
229234
let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No));
@@ -238,6 +243,7 @@ pub(super) fn unexpected_cfg_name(
238243

239244
pub(super) fn unexpected_cfg_value(
240245
sess: &Session,
246+
tcx: Option<TyCtxt<'_>>,
241247
(name, name_span): (Symbol, Span),
242248
value: Option<(Symbol, Span)>,
243249
) -> lints::UnexpectedCfgValue {
@@ -339,7 +345,7 @@ pub(super) fn unexpected_cfg_value(
339345
};
340346
lints::unexpected_cfg_value::InvocationHelp::Cargo {
341347
help,
342-
macro_help: cargo_macro_help(name_span),
348+
macro_help: cargo_macro_help(tcx, name_span),
343349
}
344350
} else {
345351
let help = if can_suggest_adding_value {

compiler/rustc_lint/src/lints.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2187,8 +2187,7 @@ pub(crate) struct UnexpectedCfgRustcMacroHelp {
21872187
pub(crate) struct UnexpectedCfgCargoMacroHelp {
21882188
pub macro_kind: &'static str,
21892189
pub macro_name: Symbol,
2190-
// FIXME: Figure out a way to get the crate name
2191-
// crate_name: String,
2190+
pub crate_name: Symbol,
21922191
}
21932192

21942193
#[derive(LintDiagnostic)]

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | cfg_macro::my_lib_macro!();
77
= 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`
88
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
99
= 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`
10+
= help: the macro `cfg_macro::my_lib_macro` may come from an old version of the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro`
1111
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
1212
= note: `#[warn(unexpected_cfgs)]` on by default
1313
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
@@ -21,7 +21,7 @@ LL | cfg_macro::my_lib_macro_value!();
2121
= note: expected values for `panic` are: `abort` and `unwind`
2222
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
2323
= 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`
24+
= help: the macro `cfg_macro::my_lib_macro_value` may come from an old version of the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro`
2525
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
2626
= note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info)
2727

@@ -34,7 +34,7 @@ LL | cfg_macro::my_lib_macro_feature!();
3434
= note: no expected values for `feature`
3535
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
3636
= 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`
37+
= help: the macro `cfg_macro::my_lib_macro_feature` may come from an old version of the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro`
3838
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
3939
= note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info)
4040

0 commit comments

Comments
 (0)