Skip to content

Commit

Permalink
Improve check-cfg Cargo macro diagnostic with crate name
Browse files Browse the repository at this point in the history
  • Loading branch information
Urgau committed Dec 15, 2024
1 parent e3e5bd9 commit 291c519
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_lint/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,7 @@ lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_printl
lint_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead
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}
lint_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}`
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`
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}`
lint_unexpected_cfg_define_features = consider defining some features in `Cargo.toml`
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
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_lint/src/early/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod check_cfg;

pub(super) fn decorate_lint(
sess: &Session,
_tcx: Option<TyCtxt<'_>>,
tcx: Option<TyCtxt<'_>>,
diagnostic: BuiltinLintDiag,
diag: &mut Diag<'_, ()>,
) {
Expand Down Expand Up @@ -205,10 +205,10 @@ pub(super) fn decorate_lint(
.decorate_lint(diag);
}
BuiltinLintDiag::UnexpectedCfgName(name, value) => {
check_cfg::unexpected_cfg_name(sess, name, value).decorate_lint(diag);
check_cfg::unexpected_cfg_name(sess, tcx, name, value).decorate_lint(diag);
}
BuiltinLintDiag::UnexpectedCfgValue(name, value) => {
check_cfg::unexpected_cfg_value(sess, name, value).decorate_lint(diag);
check_cfg::unexpected_cfg_value(sess, tcx, name, value).decorate_lint(diag);
}
BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => {
let suggestion = match sugg {
Expand Down
16 changes: 11 additions & 5 deletions compiler/rustc_lint/src/early/diagnostics/check_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use rustc_hir::def_id::LOCAL_CRATE;
use rustc_middle::bug;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_session::config::ExpectedValues;
use rustc_span::edit_distance::find_best_match_for_name;
Expand Down Expand Up @@ -73,17 +74,20 @@ fn rustc_macro_help(span: Span) -> Option<lints::UnexpectedCfgRustcMacroHelp> {
}
}

fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
fn cargo_macro_help(
tcx: Option<TyCtxt<'_>>,
span: Span,
) -> Option<lints::UnexpectedCfgCargoMacroHelp> {
let oexpn = span.ctxt().outer_expn_data();
if let Some(def_id) = oexpn.macro_def_id
&& let ExpnKind::Macro(macro_kind, macro_name) = oexpn.kind
&& def_id.krate != LOCAL_CRATE
&& let Some(tcx) = tcx
{
Some(lints::UnexpectedCfgCargoMacroHelp {
macro_kind: macro_kind.descr(),
macro_name,
// FIXME: Get access to a `TyCtxt` from an `EarlyContext`
// crate_name: cx.tcx.crate_name(def_id.krate),
crate_name: tcx.crate_name(def_id.krate),
})
} else {
None
Expand All @@ -92,6 +96,7 @@ fn cargo_macro_help(span: Span) -> Option<lints::UnexpectedCfgCargoMacroHelp> {

pub(super) fn unexpected_cfg_name(
sess: &Session,
tcx: Option<TyCtxt<'_>>,
(name, name_span): (Symbol, Span),
value: Option<(Symbol, Span)>,
) -> lints::UnexpectedCfgName {
Expand Down Expand Up @@ -223,7 +228,7 @@ pub(super) fn unexpected_cfg_name(
};
lints::unexpected_cfg_name::InvocationHelp::Cargo {
help,
macro_help: cargo_macro_help(name_span),
macro_help: cargo_macro_help(tcx, name_span),
}
} else {
let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No));
Expand All @@ -238,6 +243,7 @@ pub(super) fn unexpected_cfg_name(

pub(super) fn unexpected_cfg_value(
sess: &Session,
tcx: Option<TyCtxt<'_>>,
(name, name_span): (Symbol, Span),
value: Option<(Symbol, Span)>,
) -> lints::UnexpectedCfgValue {
Expand Down Expand Up @@ -339,7 +345,7 @@ pub(super) fn unexpected_cfg_value(
};
lints::unexpected_cfg_value::InvocationHelp::Cargo {
help,
macro_help: cargo_macro_help(name_span),
macro_help: cargo_macro_help(tcx, name_span),
}
} else {
let help = if can_suggest_adding_value {
Expand Down
3 changes: 1 addition & 2 deletions compiler/rustc_lint/src/lints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2187,8 +2187,7 @@ pub(crate) struct UnexpectedCfgRustcMacroHelp {
pub(crate) struct UnexpectedCfgCargoMacroHelp {
pub macro_kind: &'static str,
pub macro_name: Symbol,
// FIXME: Figure out a way to get the crate name
// crate_name: String,
pub crate_name: Symbol,
}

#[derive(LintDiagnostic)]
Expand Down
6 changes: 3 additions & 3 deletions tests/ui/check-cfg/report-in-external-macros.cargo.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | cfg_macro::my_lib_macro!();
= 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`
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
= help: try refering to `cfg_macro::my_lib_macro` crate for guidance on how handle this unexpected cfg
= 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`
= 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`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
= note: `#[warn(unexpected_cfgs)]` on by default
= note: this warning originates in the macro `cfg_macro::my_lib_macro` (in Nightly builds, run with -Z macro-backtrace for more info)
Expand All @@ -21,7 +21,7 @@ LL | cfg_macro::my_lib_macro_value!();
= note: expected values for `panic` are: `abort` and `unwind`
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
= help: try refering to `cfg_macro::my_lib_macro_value` crate for guidance on how handle this unexpected cfg
= 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`
= 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`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
= note: this warning originates in the macro `cfg_macro::my_lib_macro_value` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand All @@ -34,7 +34,7 @@ LL | cfg_macro::my_lib_macro_feature!();
= note: no expected values for `feature`
= note: using a cfg inside a macro will use the cfgs from the destination crate and not the ones from the defining crate
= help: try refering to `cfg_macro::my_lib_macro_feature` crate for guidance on how handle this unexpected cfg
= 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`
= 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`
= note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
= note: this warning originates in the macro `cfg_macro::my_lib_macro_feature` (in Nightly builds, run with -Z macro-backtrace for more info)

Expand Down

0 comments on commit 291c519

Please sign in to comment.