From b65729897d876a4c364dbb47d0ec575d5ac57f99 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Fri, 21 Nov 2025 18:51:59 +0100 Subject: [PATCH 1/3] Move `check_cfg` to `rustc_attr_parsing` --- Cargo.lock | 1 + compiler/rustc_attr_parsing/Cargo.toml | 1 + compiler/rustc_attr_parsing/messages.ftl | 43 +++ .../rustc_attr_parsing/src/attributes/cfg.rs | 2 + .../src/attributes/cfg}/check_cfg.rs | 8 +- compiler/rustc_attr_parsing/src/lib.rs | 1 + .../src/session_diagnostics.rs | 315 ++++++++++++++++++ compiler/rustc_lint/messages.ftl | 44 --- compiler/rustc_lint/src/early/diagnostics.rs | 6 +- compiler/rustc_lint/src/lints.rs | 315 ------------------ triagebot.toml | 2 +- 11 files changed, 370 insertions(+), 368 deletions(-) rename compiler/{rustc_lint/src/early/diagnostics => rustc_attr_parsing/src/attributes/cfg}/check_cfg.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 39ccb0b1e67aa..b8295bdf455d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3533,6 +3533,7 @@ dependencies = [ "rustc_hir", "rustc_lexer", "rustc_macros", + "rustc_middle", "rustc_parse", "rustc_session", "rustc_span", diff --git a/compiler/rustc_attr_parsing/Cargo.toml b/compiler/rustc_attr_parsing/Cargo.toml index 79193f394fe43..7cb7fa02b8e20 100644 --- a/compiler/rustc_attr_parsing/Cargo.toml +++ b/compiler/rustc_attr_parsing/Cargo.toml @@ -14,6 +14,7 @@ rustc_fluent_macro = { path = "../rustc_fluent_macro" } rustc_hir = { path = "../rustc_hir" } rustc_lexer = { path = "../rustc_lexer" } rustc_macros = { path = "../rustc_macros" } +rustc_middle = { path = "../rustc_middle" } rustc_parse = { path = "../rustc_parse" } rustc_session = { path = "../rustc_session" } rustc_span = { path = "../rustc_span" } diff --git a/compiler/rustc_attr_parsing/messages.ftl b/compiler/rustc_attr_parsing/messages.ftl index a2a5f8ab14236..d90f1f129bed2 100644 --- a/compiler/rustc_attr_parsing/messages.ftl +++ b/compiler/rustc_attr_parsing/messages.ftl @@ -219,6 +219,49 @@ attr_parsing_stability_outside_std = stability attributes may not be used outsid attr_parsing_suffixed_literal_in_attribute = suffixed literals are not allowed in attributes .help = instead of using a suffixed literal (`1u8`, `1.0f32`, etc.), use an unsuffixed version (`1`, `1.0`, etc.) +attr_parsing_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_println}` to the top of the `build.rs` +attr_parsing_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead +attr_parsing_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} +attr_parsing_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}` +attr_parsing_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}` + +attr_parsing_unexpected_cfg_define_features = consider defining some features in `Cargo.toml` +attr_parsing_unexpected_cfg_doc_cargo = see for more information about checking conditional configuration +attr_parsing_unexpected_cfg_doc_rustc = see for more information about checking conditional configuration + +attr_parsing_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 +attr_parsing_unexpected_cfg_from_external_macro_refer = try referring to `{$macro_name}` crate for guidance on how handle this unexpected cfg +attr_parsing_unexpected_cfg_name = unexpected `cfg` condition name: `{$name}` +attr_parsing_unexpected_cfg_name_expected_names = expected names are: {$possibilities}{$and_more -> + [0] {""} + *[other] {" "}and {$and_more} more + } +attr_parsing_unexpected_cfg_name_expected_values = expected values for `{$best_match}` are: {$possibilities} +attr_parsing_unexpected_cfg_name_similar_name = there is a config with a similar name +attr_parsing_unexpected_cfg_name_similar_name_different_values = there is a config with a similar name and different values +attr_parsing_unexpected_cfg_name_similar_name_no_value = there is a config with a similar name and no value +attr_parsing_unexpected_cfg_name_similar_name_value = there is a config with a similar name and value +attr_parsing_unexpected_cfg_name_version_syntax = there is a similar config predicate: `version("..")` +attr_parsing_unexpected_cfg_name_with_similar_value = found config with similar value + +attr_parsing_unexpected_cfg_value = unexpected `cfg` condition value: {$has_value -> + [true] `{$value}` + *[false] (none) + } +attr_parsing_unexpected_cfg_value_add_feature = consider adding `{$value}` as a feature in `Cargo.toml` +attr_parsing_unexpected_cfg_value_expected_values = expected values for `{$name}` are: {$have_none_possibility -> + [true] {"(none), "} + *[false] {""} + }{$possibilities}{$and_more -> + [0] {""} + *[other] {" "}and {$and_more} more + } +attr_parsing_unexpected_cfg_value_no_expected_value = no expected value for `{$name}` +attr_parsing_unexpected_cfg_value_no_expected_values = no expected values for `{$name}` +attr_parsing_unexpected_cfg_value_remove_condition = remove the condition +attr_parsing_unexpected_cfg_value_remove_value = remove the value +attr_parsing_unexpected_cfg_value_similar_name = there is a expected value with a similar name +attr_parsing_unexpected_cfg_value_specify_value = specify a config value attr_parsing_unknown_meta_item = unknown meta item '{$item}' .label = expected one of {$expected} diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg.rs index a8aa63bd05ee6..e72ebe7805e3d 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg.rs @@ -1,3 +1,5 @@ +pub(crate) mod check_cfg; + use rustc_ast::token::Delimiter; use rustc_ast::tokenstream::DelimSpan; use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, NodeId, ast, token}; diff --git a/compiler/rustc_lint/src/early/diagnostics/check_cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs similarity index 99% rename from compiler/rustc_lint/src/early/diagnostics/check_cfg.rs rename to compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs index e2f5dd315d570..2c385b9f3e6bd 100644 --- a/compiler/rustc_lint/src/early/diagnostics/check_cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs @@ -6,7 +6,7 @@ use rustc_session::config::ExpectedValues; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::{ExpnKind, Ident, Span, Symbol, sym}; -use crate::lints; +use crate::session_diagnostics as lints; const MAX_CHECK_CFG_NAMES_OR_VALUES: usize = 35; @@ -109,7 +109,7 @@ fn cargo_macro_help( } } -pub(super) fn unexpected_cfg_name( +pub fn unexpected_cfg_name( sess: &Session, tcx: Option>, (name, name_span): (Symbol, Span), @@ -266,7 +266,7 @@ pub(super) fn unexpected_cfg_name( lints::UnexpectedCfgName { code_sugg, invocation_help, name } } -pub(super) fn unexpected_cfg_value( +pub fn unexpected_cfg_value( sess: &Session, tcx: Option>, (name, name_span): (Symbol, Span), @@ -353,7 +353,7 @@ pub(super) fn unexpected_cfg_value( // basic heuristic, we use the "cheat" unstable feature enable method and the // non-ui-testing enabled option. || (matches!(sess.psess.unstable_features, rustc_feature::UnstableFeatures::Cheat) - && !sess.opts.unstable_opts.ui_testing); + && !sess.opts.unstable_opts.ui_testing); let inst = |escape_quotes| { to_check_cfg_arg(Ident::new(name, name_span), value.map(|(v, _s)| v), escape_quotes) diff --git a/compiler/rustc_attr_parsing/src/lib.rs b/compiler/rustc_attr_parsing/src/lib.rs index 046cca4c742b0..d294040c3012d 100644 --- a/compiler/rustc_attr_parsing/src/lib.rs +++ b/compiler/rustc_attr_parsing/src/lib.rs @@ -102,6 +102,7 @@ mod session_diagnostics; mod target_checking; pub mod validate_attr; +pub use attributes::cfg::check_cfg::{unexpected_cfg_name, unexpected_cfg_value}; pub use attributes::cfg::{ CFG_TEMPLATE, EvalConfigResult, eval_config_entry, parse_cfg, parse_cfg_attr, parse_cfg_entry, }; diff --git a/compiler/rustc_attr_parsing/src/session_diagnostics.rs b/compiler/rustc_attr_parsing/src/session_diagnostics.rs index 8d783503f7be0..5ce3b83622643 100644 --- a/compiler/rustc_attr_parsing/src/session_diagnostics.rs +++ b/compiler/rustc_attr_parsing/src/session_diagnostics.rs @@ -994,3 +994,318 @@ pub(crate) struct CfgAttrBadDelim { #[subdiagnostic] pub sugg: MetaBadDelimSugg, } + +#[derive(Subdiagnostic)] +pub enum UnexpectedCfgCargoHelp { + #[help(attr_parsing_unexpected_cfg_add_cargo_feature)] + #[help(attr_parsing_unexpected_cfg_add_cargo_toml_lint_cfg)] + LintCfg { cargo_toml_lint_cfg: String }, + #[help(attr_parsing_unexpected_cfg_add_cargo_feature)] + #[help(attr_parsing_unexpected_cfg_add_cargo_toml_lint_cfg)] + #[help(attr_parsing_unexpected_cfg_add_build_rs_println)] + LintCfgAndBuildRs { cargo_toml_lint_cfg: String, build_rs_println: String }, +} + +impl UnexpectedCfgCargoHelp { + fn cargo_toml_lint_cfg(unescaped: &str) -> String { + format!( + "\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}" + ) + } + + pub fn lint_cfg(unescaped: &str) -> Self { + UnexpectedCfgCargoHelp::LintCfg { + cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped), + } + } + + pub fn lint_cfg_and_build_rs(unescaped: &str, escaped: &str) -> Self { + UnexpectedCfgCargoHelp::LintCfgAndBuildRs { + cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped), + build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");"), + } + } +} + +#[derive(Subdiagnostic)] +#[help(attr_parsing_unexpected_cfg_add_cmdline_arg)] +pub struct UnexpectedCfgRustcHelp { + pub cmdline_arg: String, +} + +impl UnexpectedCfgRustcHelp { + pub fn new(unescaped: &str) -> Self { + Self { cmdline_arg: format!("--check-cfg={unescaped}") } + } +} + +#[derive(Subdiagnostic)] +#[note(attr_parsing_unexpected_cfg_from_external_macro_origin)] +#[help(attr_parsing_unexpected_cfg_from_external_macro_refer)] +pub struct UnexpectedCfgRustcMacroHelp { + pub macro_kind: &'static str, + pub macro_name: Symbol, +} + +#[derive(Subdiagnostic)] +#[note(attr_parsing_unexpected_cfg_from_external_macro_origin)] +#[help(attr_parsing_unexpected_cfg_from_external_macro_refer)] +#[help(attr_parsing_unexpected_cfg_cargo_update)] +pub struct UnexpectedCfgCargoMacroHelp { + pub macro_kind: &'static str, + pub macro_name: Symbol, + pub crate_name: Symbol, +} + +#[derive(LintDiagnostic)] +#[diag(attr_parsing_unexpected_cfg_name)] +pub struct UnexpectedCfgName { + #[subdiagnostic] + pub code_sugg: unexpected_cfg_name::CodeSuggestion, + #[subdiagnostic] + pub invocation_help: unexpected_cfg_name::InvocationHelp, + + pub name: Symbol, +} + +pub(super) mod unexpected_cfg_name { + use rustc_errors::DiagSymbolList; + use rustc_macros::Subdiagnostic; + use rustc_span::{Ident, Span, Symbol}; + + #[derive(Subdiagnostic)] + pub enum CodeSuggestion { + #[help(attr_parsing_unexpected_cfg_define_features)] + DefineFeatures, + #[multipart_suggestion( + attr_parsing_unexpected_cfg_name_version_syntax, + applicability = "machine-applicable" + )] + VersionSyntax { + #[suggestion_part(code = "(")] + between_name_and_value: Span, + #[suggestion_part(code = ")")] + after_value: Span, + }, + #[suggestion( + attr_parsing_unexpected_cfg_name_similar_name_value, + applicability = "maybe-incorrect", + code = "{code}" + )] + SimilarNameAndValue { + #[primary_span] + span: Span, + code: String, + }, + #[suggestion( + attr_parsing_unexpected_cfg_name_similar_name_no_value, + applicability = "maybe-incorrect", + code = "{code}" + )] + SimilarNameNoValue { + #[primary_span] + span: Span, + code: String, + }, + #[suggestion( + attr_parsing_unexpected_cfg_name_similar_name_different_values, + applicability = "maybe-incorrect", + code = "{code}" + )] + SimilarNameDifferentValues { + #[primary_span] + span: Span, + code: String, + #[subdiagnostic] + expected: Option, + }, + #[suggestion( + attr_parsing_unexpected_cfg_name_similar_name, + applicability = "maybe-incorrect", + code = "{code}" + )] + SimilarName { + #[primary_span] + span: Span, + code: String, + #[subdiagnostic] + expected: Option, + }, + SimilarValues { + #[subdiagnostic] + with_similar_values: Vec, + #[subdiagnostic] + expected_names: Option, + }, + } + + #[derive(Subdiagnostic)] + #[help(attr_parsing_unexpected_cfg_name_expected_values)] + pub struct ExpectedValues { + pub best_match: Symbol, + pub possibilities: DiagSymbolList, + } + + #[derive(Subdiagnostic)] + #[suggestion( + attr_parsing_unexpected_cfg_name_with_similar_value, + applicability = "maybe-incorrect", + code = "{code}" + )] + pub struct FoundWithSimilarValue { + #[primary_span] + pub span: Span, + pub code: String, + } + + #[derive(Subdiagnostic)] + #[help_once(attr_parsing_unexpected_cfg_name_expected_names)] + pub struct ExpectedNames { + pub possibilities: DiagSymbolList, + pub and_more: usize, + } + + #[derive(Subdiagnostic)] + pub enum InvocationHelp { + #[note(attr_parsing_unexpected_cfg_doc_cargo)] + Cargo { + #[subdiagnostic] + macro_help: Option, + #[subdiagnostic] + help: Option, + }, + #[note(attr_parsing_unexpected_cfg_doc_rustc)] + Rustc { + #[subdiagnostic] + macro_help: Option, + #[subdiagnostic] + help: super::UnexpectedCfgRustcHelp, + }, + } +} + +#[derive(LintDiagnostic)] +#[diag(attr_parsing_unexpected_cfg_value)] +pub struct UnexpectedCfgValue { + #[subdiagnostic] + pub code_sugg: unexpected_cfg_value::CodeSuggestion, + #[subdiagnostic] + pub invocation_help: unexpected_cfg_value::InvocationHelp, + + pub has_value: bool, + pub value: String, +} + +pub(super) mod unexpected_cfg_value { + use rustc_errors::DiagSymbolList; + use rustc_macros::Subdiagnostic; + use rustc_span::{Span, Symbol}; + + #[derive(Subdiagnostic)] + pub enum CodeSuggestion { + ChangeValue { + #[subdiagnostic] + expected_values: ExpectedValues, + #[subdiagnostic] + suggestion: Option, + }, + #[note(attr_parsing_unexpected_cfg_value_no_expected_value)] + RemoveValue { + #[subdiagnostic] + suggestion: Option, + + name: Symbol, + }, + #[note(attr_parsing_unexpected_cfg_value_no_expected_values)] + RemoveCondition { + #[subdiagnostic] + suggestion: RemoveConditionSuggestion, + + name: Symbol, + }, + } + + #[derive(Subdiagnostic)] + pub enum ChangeValueSuggestion { + #[suggestion( + attr_parsing_unexpected_cfg_value_similar_name, + code = r#""{best_match}""#, + applicability = "maybe-incorrect" + )] + SimilarName { + #[primary_span] + span: Span, + best_match: Symbol, + }, + #[suggestion( + attr_parsing_unexpected_cfg_value_specify_value, + code = r#" = "{first_possibility}""#, + applicability = "maybe-incorrect" + )] + SpecifyValue { + #[primary_span] + span: Span, + first_possibility: Symbol, + }, + } + + #[derive(Subdiagnostic)] + #[suggestion( + attr_parsing_unexpected_cfg_value_remove_value, + code = "", + applicability = "maybe-incorrect" + )] + pub struct RemoveValueSuggestion { + #[primary_span] + pub span: Span, + } + + #[derive(Subdiagnostic)] + #[suggestion( + attr_parsing_unexpected_cfg_value_remove_condition, + code = "", + applicability = "maybe-incorrect" + )] + pub struct RemoveConditionSuggestion { + #[primary_span] + pub span: Span, + } + + #[derive(Subdiagnostic)] + #[note(attr_parsing_unexpected_cfg_value_expected_values)] + pub struct ExpectedValues { + pub name: Symbol, + pub have_none_possibility: bool, + pub possibilities: DiagSymbolList, + pub and_more: usize, + } + + #[derive(Subdiagnostic)] + pub enum InvocationHelp { + #[note(attr_parsing_unexpected_cfg_doc_cargo)] + Cargo { + #[subdiagnostic] + help: Option, + #[subdiagnostic] + macro_help: Option, + }, + #[note(attr_parsing_unexpected_cfg_doc_rustc)] + Rustc { + #[subdiagnostic] + help: Option, + #[subdiagnostic] + macro_help: Option, + }, + } + + #[derive(Subdiagnostic)] + pub enum CargoHelp { + #[help(attr_parsing_unexpected_cfg_value_add_feature)] + AddFeature { + value: Symbol, + }, + #[help(attr_parsing_unexpected_cfg_define_features)] + DefineFeatures, + Other(#[subdiagnostic] super::UnexpectedCfgCargoHelp), + } +} diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index c996181354b92..14e78dd736659 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -810,50 +810,6 @@ lint_undropped_manually_drops = calls to `std::mem::drop` with `std::mem::Manual .label = argument has type `{$arg_ty}` .suggestion = use `std::mem::ManuallyDrop::into_inner` to get the inner value -lint_unexpected_cfg_add_build_rs_println = or consider adding `{$build_rs_println}` to the top of the `build.rs` -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 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 for more information about checking conditional configuration -lint_unexpected_cfg_doc_rustc = see for more information about checking conditional configuration - -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 -lint_unexpected_cfg_from_external_macro_refer = try referring to `{$macro_name}` crate for guidance on how handle this unexpected cfg -lint_unexpected_cfg_name = unexpected `cfg` condition name: `{$name}` -lint_unexpected_cfg_name_expected_names = expected names are: {$possibilities}{$and_more -> - [0] {""} - *[other] {" "}and {$and_more} more - } -lint_unexpected_cfg_name_expected_values = expected values for `{$best_match}` are: {$possibilities} -lint_unexpected_cfg_name_similar_name = there is a config with a similar name -lint_unexpected_cfg_name_similar_name_different_values = there is a config with a similar name and different values -lint_unexpected_cfg_name_similar_name_no_value = there is a config with a similar name and no value -lint_unexpected_cfg_name_similar_name_value = there is a config with a similar name and value -lint_unexpected_cfg_name_version_syntax = there is a similar config predicate: `version("..")` -lint_unexpected_cfg_name_with_similar_value = found config with similar value - -lint_unexpected_cfg_value = unexpected `cfg` condition value: {$has_value -> - [true] `{$value}` - *[false] (none) - } -lint_unexpected_cfg_value_add_feature = consider adding `{$value}` as a feature in `Cargo.toml` -lint_unexpected_cfg_value_expected_values = expected values for `{$name}` are: {$have_none_possibility -> - [true] {"(none), "} - *[false] {""} - }{$possibilities}{$and_more -> - [0] {""} - *[other] {" "}and {$and_more} more - } -lint_unexpected_cfg_value_no_expected_value = no expected value for `{$name}` -lint_unexpected_cfg_value_no_expected_values = no expected values for `{$name}` -lint_unexpected_cfg_value_remove_condition = remove the condition -lint_unexpected_cfg_value_remove_value = remove the value -lint_unexpected_cfg_value_similar_name = there is a expected value with a similar name -lint_unexpected_cfg_value_specify_value = specify a config value - lint_ungated_async_fn_track_caller = `#[track_caller]` on async functions is a no-op .label = this function will not propagate the caller location diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index 9029ee0447110..a29b15e424e74 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -13,8 +13,6 @@ use tracing::debug; use crate::lints; -mod check_cfg; - pub fn decorate_builtin_lint( sess: &Session, tcx: Option>, @@ -169,10 +167,10 @@ pub fn decorate_builtin_lint( .decorate_lint(diag); } BuiltinLintDiag::UnexpectedCfgName(name, value) => { - check_cfg::unexpected_cfg_name(sess, tcx, name, value).decorate_lint(diag); + rustc_attr_parsing::unexpected_cfg_name(sess, tcx, name, value).decorate_lint(diag); } BuiltinLintDiag::UnexpectedCfgValue(name, value) => { - check_cfg::unexpected_cfg_value(sess, tcx, name, value).decorate_lint(diag); + rustc_attr_parsing::unexpected_cfg_value(sess, tcx, name, value).decorate_lint(diag); } BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => { let suggestion = match sugg { diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index dc50f61f741d6..d0dc9e8225a2d 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2227,321 +2227,6 @@ pub(crate) enum InvalidAsmLabel { }, } -#[derive(Subdiagnostic)] -pub(crate) enum UnexpectedCfgCargoHelp { - #[help(lint_unexpected_cfg_add_cargo_feature)] - #[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)] - LintCfg { cargo_toml_lint_cfg: String }, - #[help(lint_unexpected_cfg_add_cargo_feature)] - #[help(lint_unexpected_cfg_add_cargo_toml_lint_cfg)] - #[help(lint_unexpected_cfg_add_build_rs_println)] - LintCfgAndBuildRs { cargo_toml_lint_cfg: String, build_rs_println: String }, -} - -impl UnexpectedCfgCargoHelp { - fn cargo_toml_lint_cfg(unescaped: &str) -> String { - format!( - "\n [lints.rust]\n unexpected_cfgs = {{ level = \"warn\", check-cfg = ['{unescaped}'] }}" - ) - } - - pub(crate) fn lint_cfg(unescaped: &str) -> Self { - UnexpectedCfgCargoHelp::LintCfg { - cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped), - } - } - - pub(crate) fn lint_cfg_and_build_rs(unescaped: &str, escaped: &str) -> Self { - UnexpectedCfgCargoHelp::LintCfgAndBuildRs { - cargo_toml_lint_cfg: Self::cargo_toml_lint_cfg(unescaped), - build_rs_println: format!("println!(\"cargo::rustc-check-cfg={escaped}\");"), - } - } -} - -#[derive(Subdiagnostic)] -#[help(lint_unexpected_cfg_add_cmdline_arg)] -pub(crate) struct UnexpectedCfgRustcHelp { - pub cmdline_arg: String, -} - -impl UnexpectedCfgRustcHelp { - pub(crate) fn new(unescaped: &str) -> Self { - Self { cmdline_arg: format!("--check-cfg={unescaped}") } - } -} - -#[derive(Subdiagnostic)] -#[note(lint_unexpected_cfg_from_external_macro_origin)] -#[help(lint_unexpected_cfg_from_external_macro_refer)] -pub(crate) struct UnexpectedCfgRustcMacroHelp { - pub macro_kind: &'static str, - pub macro_name: Symbol, -} - -#[derive(Subdiagnostic)] -#[note(lint_unexpected_cfg_from_external_macro_origin)] -#[help(lint_unexpected_cfg_from_external_macro_refer)] -#[help(lint_unexpected_cfg_cargo_update)] -pub(crate) struct UnexpectedCfgCargoMacroHelp { - pub macro_kind: &'static str, - pub macro_name: Symbol, - pub crate_name: Symbol, -} - -#[derive(LintDiagnostic)] -#[diag(lint_unexpected_cfg_name)] -pub(crate) struct UnexpectedCfgName { - #[subdiagnostic] - pub code_sugg: unexpected_cfg_name::CodeSuggestion, - #[subdiagnostic] - pub invocation_help: unexpected_cfg_name::InvocationHelp, - - pub name: Symbol, -} - -pub(crate) mod unexpected_cfg_name { - use rustc_errors::DiagSymbolList; - use rustc_macros::Subdiagnostic; - use rustc_span::{Ident, Span, Symbol}; - - #[derive(Subdiagnostic)] - pub(crate) enum CodeSuggestion { - #[help(lint_unexpected_cfg_define_features)] - DefineFeatures, - #[multipart_suggestion( - lint_unexpected_cfg_name_version_syntax, - applicability = "machine-applicable" - )] - VersionSyntax { - #[suggestion_part(code = "(")] - between_name_and_value: Span, - #[suggestion_part(code = ")")] - after_value: Span, - }, - #[suggestion( - lint_unexpected_cfg_name_similar_name_value, - applicability = "maybe-incorrect", - code = "{code}" - )] - SimilarNameAndValue { - #[primary_span] - span: Span, - code: String, - }, - #[suggestion( - lint_unexpected_cfg_name_similar_name_no_value, - applicability = "maybe-incorrect", - code = "{code}" - )] - SimilarNameNoValue { - #[primary_span] - span: Span, - code: String, - }, - #[suggestion( - lint_unexpected_cfg_name_similar_name_different_values, - applicability = "maybe-incorrect", - code = "{code}" - )] - SimilarNameDifferentValues { - #[primary_span] - span: Span, - code: String, - #[subdiagnostic] - expected: Option, - }, - #[suggestion( - lint_unexpected_cfg_name_similar_name, - applicability = "maybe-incorrect", - code = "{code}" - )] - SimilarName { - #[primary_span] - span: Span, - code: String, - #[subdiagnostic] - expected: Option, - }, - SimilarValues { - #[subdiagnostic] - with_similar_values: Vec, - #[subdiagnostic] - expected_names: Option, - }, - } - - #[derive(Subdiagnostic)] - #[help(lint_unexpected_cfg_name_expected_values)] - pub(crate) struct ExpectedValues { - pub best_match: Symbol, - pub possibilities: DiagSymbolList, - } - - #[derive(Subdiagnostic)] - #[suggestion( - lint_unexpected_cfg_name_with_similar_value, - applicability = "maybe-incorrect", - code = "{code}" - )] - pub(crate) struct FoundWithSimilarValue { - #[primary_span] - pub span: Span, - pub code: String, - } - - #[derive(Subdiagnostic)] - #[help_once(lint_unexpected_cfg_name_expected_names)] - pub(crate) struct ExpectedNames { - pub possibilities: DiagSymbolList, - pub and_more: usize, - } - - #[derive(Subdiagnostic)] - pub(crate) enum InvocationHelp { - #[note(lint_unexpected_cfg_doc_cargo)] - Cargo { - #[subdiagnostic] - macro_help: Option, - #[subdiagnostic] - help: Option, - }, - #[note(lint_unexpected_cfg_doc_rustc)] - Rustc { - #[subdiagnostic] - macro_help: Option, - #[subdiagnostic] - help: super::UnexpectedCfgRustcHelp, - }, - } -} - -#[derive(LintDiagnostic)] -#[diag(lint_unexpected_cfg_value)] -pub(crate) struct UnexpectedCfgValue { - #[subdiagnostic] - pub code_sugg: unexpected_cfg_value::CodeSuggestion, - #[subdiagnostic] - pub invocation_help: unexpected_cfg_value::InvocationHelp, - - pub has_value: bool, - pub value: String, -} - -pub(crate) mod unexpected_cfg_value { - use rustc_errors::DiagSymbolList; - use rustc_macros::Subdiagnostic; - use rustc_span::{Span, Symbol}; - - #[derive(Subdiagnostic)] - pub(crate) enum CodeSuggestion { - ChangeValue { - #[subdiagnostic] - expected_values: ExpectedValues, - #[subdiagnostic] - suggestion: Option, - }, - #[note(lint_unexpected_cfg_value_no_expected_value)] - RemoveValue { - #[subdiagnostic] - suggestion: Option, - - name: Symbol, - }, - #[note(lint_unexpected_cfg_value_no_expected_values)] - RemoveCondition { - #[subdiagnostic] - suggestion: RemoveConditionSuggestion, - - name: Symbol, - }, - } - - #[derive(Subdiagnostic)] - pub(crate) enum ChangeValueSuggestion { - #[suggestion( - lint_unexpected_cfg_value_similar_name, - code = r#""{best_match}""#, - applicability = "maybe-incorrect" - )] - SimilarName { - #[primary_span] - span: Span, - best_match: Symbol, - }, - #[suggestion( - lint_unexpected_cfg_value_specify_value, - code = r#" = "{first_possibility}""#, - applicability = "maybe-incorrect" - )] - SpecifyValue { - #[primary_span] - span: Span, - first_possibility: Symbol, - }, - } - - #[derive(Subdiagnostic)] - #[suggestion( - lint_unexpected_cfg_value_remove_value, - code = "", - applicability = "maybe-incorrect" - )] - pub(crate) struct RemoveValueSuggestion { - #[primary_span] - pub span: Span, - } - - #[derive(Subdiagnostic)] - #[suggestion( - lint_unexpected_cfg_value_remove_condition, - code = "", - applicability = "maybe-incorrect" - )] - pub(crate) struct RemoveConditionSuggestion { - #[primary_span] - pub span: Span, - } - - #[derive(Subdiagnostic)] - #[note(lint_unexpected_cfg_value_expected_values)] - pub(crate) struct ExpectedValues { - pub name: Symbol, - pub have_none_possibility: bool, - pub possibilities: DiagSymbolList, - pub and_more: usize, - } - - #[derive(Subdiagnostic)] - pub(crate) enum InvocationHelp { - #[note(lint_unexpected_cfg_doc_cargo)] - Cargo { - #[subdiagnostic] - help: Option, - #[subdiagnostic] - macro_help: Option, - }, - #[note(lint_unexpected_cfg_doc_rustc)] - Rustc { - #[subdiagnostic] - help: Option, - #[subdiagnostic] - macro_help: Option, - }, - } - - #[derive(Subdiagnostic)] - pub(crate) enum CargoHelp { - #[help(lint_unexpected_cfg_value_add_feature)] - AddFeature { - value: Symbol, - }, - #[help(lint_unexpected_cfg_define_features)] - DefineFeatures, - Other(#[subdiagnostic] super::UnexpectedCfgCargoHelp), - } -} - #[derive(LintDiagnostic)] #[diag(lint_private_extern_crate_reexport, code = E0365)] pub(crate) struct PrivateExternCrateReexport { diff --git a/triagebot.toml b/triagebot.toml index f901dc7efc1e6..172e8884ff917 100644 --- a/triagebot.toml +++ b/triagebot.toml @@ -1012,7 +1012,7 @@ cc = ["@Nadrieril"] message = "Some changes occurred in cfg and check-cfg configuration" cc = ["@Urgau"] -[mentions."compiler/rustc_lint/src/early/diagnostics/check_cfg.rs"] +[mentions."compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs"] message = "Some changes occurred in check-cfg diagnostics" cc = ["@Urgau"] From 02be364f6efbde3b02b76bbf66abce75edc74540 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Fri, 21 Nov 2025 20:46:11 +0100 Subject: [PATCH 2/3] Move lints for check-cfg to attribute parsing --- .../rustc_attr_parsing/src/attributes/cfg.rs | 75 ++++++------------- compiler/rustc_attr_parsing/src/interface.rs | 4 +- compiler/rustc_attr_parsing/src/lints.rs | 24 +++++- compiler/rustc_builtin_macros/src/cfg.rs | 8 +- .../rustc_builtin_macros/src/cfg_select.rs | 9 +-- compiler/rustc_codegen_ssa/src/back/link.rs | 7 +- compiler/rustc_expand/src/config.rs | 32 ++------ compiler/rustc_expand/src/expand.rs | 4 +- compiler/rustc_hir/src/lints.rs | 12 ++- compiler/rustc_hir_analysis/src/lib.rs | 2 +- compiler/rustc_metadata/src/native_libs.rs | 7 +- .../ui/feature-gates/feature-gate-link_cfg.rs | 2 +- .../feature-gate-link_cfg.stderr | 4 +- .../link-cfg-works-transitive-dylib.rs | 2 +- .../link-cfg-works-transitive-rlib.rs | 2 +- tests/ui/link-native-libs/link-cfg-works.rs | 2 +- tests/ui/macros/cfg.rs | 3 +- tests/ui/macros/cfg.stderr | 17 ++++- tests/ui/macros/cfg_select.rs | 2 + tests/ui/macros/cfg_select.stderr | 24 +++++- 20 files changed, 121 insertions(+), 121 deletions(-) diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg.rs index e72ebe7805e3d..279d1a12cb12d 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg.rs @@ -2,17 +2,16 @@ pub(crate) mod check_cfg; use rustc_ast::token::Delimiter; use rustc_ast::tokenstream::DelimSpan; -use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, NodeId, ast, token}; +use rustc_ast::{AttrItem, Attribute, CRATE_NODE_ID, LitKind, ast, token}; use rustc_errors::{Applicability, PResult}; use rustc_feature::{AttrSuggestionStyle, AttributeTemplate, Features, template}; use rustc_hir::attrs::CfgEntry; +use rustc_hir::lints::AttributeLintKind; use rustc_hir::{AttrPath, RustcVersion}; use rustc_parse::parser::{ForceCollect, Parser}; use rustc_parse::{exp, parse_in}; use rustc_session::Session; use rustc_session::config::ExpectedValues; -use rustc_session::lint::BuiltinLintDiag; -use rustc_session::lint::builtin::UNEXPECTED_CFGS; use rustc_session::parse::{ParseSess, feature_err}; use rustc_span::{ErrorGuaranteed, Span, Symbol, sym}; use thin_vec::ThinVec; @@ -23,10 +22,7 @@ use crate::session_diagnostics::{ AttributeParseError, AttributeParseErrorReason, CfgAttrBadDelim, MetaBadDelimSugg, ParsedDescription, }; -use crate::{ - AttributeParser, CfgMatchesLintEmitter, fluent_generated, parse_version, session_diagnostics, - try_gate_cfg, -}; +use crate::{AttributeParser, fluent_generated, parse_version, session_diagnostics, try_gate_cfg}; pub const CFG_TEMPLATE: AttributeTemplate = template!( List: &["predicate"], @@ -195,43 +191,40 @@ fn parse_name_value( } }; + match cx.sess.psess.check_config.expecteds.get(&name) { + Some(ExpectedValues::Some(values)) if !values.contains(&value.map(|(v, _)| v)) => { + cx.emit_lint(AttributeLintKind::UnexpectedCfgValue { name, name_span, value }, span); + } + None if cx.sess.psess.check_config.exhaustive_names => { + cx.emit_lint(AttributeLintKind::UnexpectedCfgName { name, name_span, value }, span); + } + _ => { /* not unexpected */ } + } + Ok(CfgEntry::NameValue { name, name_span, value, span }) } -pub fn eval_config_entry( - sess: &Session, - cfg_entry: &CfgEntry, - id: NodeId, - emit_lints: ShouldEmit, -) -> EvalConfigResult { +pub fn eval_config_entry(sess: &Session, cfg_entry: &CfgEntry) -> EvalConfigResult { match cfg_entry { CfgEntry::All(subs, ..) => { - let mut all = None; for sub in subs { - let res = eval_config_entry(sess, sub, id, emit_lints); - // We cannot short-circuit because `eval_config_entry` emits some lints + let res = eval_config_entry(sess, sub); if !res.as_bool() { - all.get_or_insert(res); + return res; } } - all.unwrap_or_else(|| EvalConfigResult::True) + EvalConfigResult::True } CfgEntry::Any(subs, span) => { - let mut any = None; for sub in subs { - let res = eval_config_entry(sess, sub, id, emit_lints); - // We cannot short-circuit because `eval_config_entry` emits some lints - if res.as_bool() { - any.get_or_insert(res); + if eval_config_entry(sess, sub).as_bool() { + return EvalConfigResult::True; } } - any.unwrap_or_else(|| EvalConfigResult::False { - reason: cfg_entry.clone(), - reason_span: *span, - }) + EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span } } CfgEntry::Not(sub, span) => { - if eval_config_entry(sess, sub, id, emit_lints).as_bool() { + if eval_config_entry(sess, sub).as_bool() { EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span } } else { EvalConfigResult::True @@ -244,31 +237,7 @@ pub fn eval_config_entry( EvalConfigResult::False { reason: cfg_entry.clone(), reason_span: *span } } } - CfgEntry::NameValue { name, name_span, value, span } => { - if let ShouldEmit::ErrorsAndLints = emit_lints { - match sess.psess.check_config.expecteds.get(name) { - Some(ExpectedValues::Some(values)) - if !values.contains(&value.map(|(v, _)| v)) => - { - id.emit_span_lint( - sess, - UNEXPECTED_CFGS, - *span, - BuiltinLintDiag::UnexpectedCfgValue((*name, *name_span), *value), - ); - } - None if sess.psess.check_config.exhaustive_names => { - id.emit_span_lint( - sess, - UNEXPECTED_CFGS, - *span, - BuiltinLintDiag::UnexpectedCfgName((*name, *name_span), *value), - ); - } - _ => { /* not unexpected */ } - } - } - + CfgEntry::NameValue { name, name_span: _, value, span } => { if sess.psess.config.contains(&(*name, value.map(|(v, _)| v))) { EvalConfigResult::True } else { diff --git a/compiler/rustc_attr_parsing/src/interface.rs b/compiler/rustc_attr_parsing/src/interface.rs index b7a6a1ef6d667..ea4df13cc68b3 100644 --- a/compiler/rustc_attr_parsing/src/interface.rs +++ b/compiler/rustc_attr_parsing/src/interface.rs @@ -115,7 +115,7 @@ impl<'sess> AttributeParser<'sess, Early> { OmitDoc::Skip, std::convert::identity, |lint| { - crate::lints::emit_attribute_lint(&lint, sess); + crate::lints::emit_attribute_lint(&lint, sess, sess); }, ) } @@ -187,7 +187,7 @@ impl<'sess> AttributeParser<'sess, Early> { target_span, target_id: target_node_id, emit_lint: &mut |lint| { - crate::lints::emit_attribute_lint(&lint, sess); + crate::lints::emit_attribute_lint(&lint, sess, sess); }, }, attr_span, diff --git a/compiler/rustc_attr_parsing/src/lints.rs b/compiler/rustc_attr_parsing/src/lints.rs index 3a2a370466961..7fb5dc57d28dd 100644 --- a/compiler/rustc_attr_parsing/src/lints.rs +++ b/compiler/rustc_attr_parsing/src/lints.rs @@ -3,11 +3,17 @@ use std::borrow::Cow; use rustc_errors::{DiagArgValue, LintEmitter}; use rustc_hir::Target; use rustc_hir::lints::{AttributeLint, AttributeLintKind}; +use rustc_session::Session; use rustc_span::sym; -use crate::session_diagnostics; +use crate::{session_diagnostics, unexpected_cfg_name, unexpected_cfg_value}; -pub fn emit_attribute_lint(lint: &AttributeLint, lint_emitter: L) { +//tcx: TyCtxt<'_> +pub fn emit_attribute_lint( + lint: &AttributeLint, + lint_emitter: L, + sess: &Session, +) { let AttributeLint { id, span, kind } = lint; match kind { @@ -98,5 +104,19 @@ pub fn emit_attribute_lint(lint: &AttributeLint, lint_emi }, ) } + &AttributeLintKind::UnexpectedCfgName { name, name_span, value } => lint_emitter + .emit_node_span_lint( + rustc_session::lint::builtin::UNEXPECTED_CFGS, + *id, + *span, + unexpected_cfg_name(sess, None, (name, name_span), value), + ), + &AttributeLintKind::UnexpectedCfgValue { name, name_span, value } => lint_emitter + .emit_node_span_lint( + rustc_session::lint::builtin::UNEXPECTED_CFGS, + *id, + *span, + unexpected_cfg_value(sess, None, (name, name_span), value), + ), } } diff --git a/compiler/rustc_builtin_macros/src/cfg.rs b/compiler/rustc_builtin_macros/src/cfg.rs index 5ca4fff948788..7395ee96fab4d 100644 --- a/compiler/rustc_builtin_macros/src/cfg.rs +++ b/compiler/rustc_builtin_macros/src/cfg.rs @@ -26,13 +26,7 @@ pub(crate) fn expand_cfg( ExpandResult::Ready(match parse_cfg(cx, sp, tts) { Ok(cfg) => { - let matches_cfg = attr::eval_config_entry( - cx.sess, - &cfg, - cx.current_expansion.lint_node_id, - ShouldEmit::ErrorsAndLints, - ) - .as_bool(); + let matches_cfg = attr::eval_config_entry(cx.sess, &cfg).as_bool(); MacEager::expr(cx.expr_bool(sp, matches_cfg)) } diff --git a/compiler/rustc_builtin_macros/src/cfg_select.rs b/compiler/rustc_builtin_macros/src/cfg_select.rs index f2e454c3d4373..dc8077b2a1ffb 100644 --- a/compiler/rustc_builtin_macros/src/cfg_select.rs +++ b/compiler/rustc_builtin_macros/src/cfg_select.rs @@ -1,7 +1,7 @@ use rustc_ast::tokenstream::TokenStream; use rustc_attr_parsing as attr; use rustc_attr_parsing::{ - CfgSelectBranches, CfgSelectPredicate, EvalConfigResult, ShouldEmit, parse_cfg_select, + CfgSelectBranches, CfgSelectPredicate, EvalConfigResult, parse_cfg_select, }; use rustc_expand::base::{DummyResult, ExpandResult, ExtCtxt, MacroExpanderResult}; use rustc_span::{Ident, Span, sym}; @@ -11,12 +11,7 @@ use crate::errors::{CfgSelectNoMatches, CfgSelectUnreachable}; /// Selects the first arm whose predicate evaluates to true. fn select_arm(ecx: &ExtCtxt<'_>, branches: CfgSelectBranches) -> Option<(TokenStream, Span)> { for (cfg, tt, arm_span) in branches.reachable { - if let EvalConfigResult::True = attr::eval_config_entry( - &ecx.sess, - &cfg, - ecx.current_expansion.lint_node_id, - ShouldEmit::ErrorsAndLints, - ) { + if let EvalConfigResult::True = attr::eval_config_entry(&ecx.sess, &cfg) { return Some((tt, arm_span)); } } diff --git a/compiler/rustc_codegen_ssa/src/back/link.rs b/compiler/rustc_codegen_ssa/src/back/link.rs index 820f7ba4a6f23..f0cd58fea3105 100644 --- a/compiler/rustc_codegen_ssa/src/back/link.rs +++ b/compiler/rustc_codegen_ssa/src/back/link.rs @@ -13,8 +13,7 @@ use find_msvc_tools; use itertools::Itertools; use regex::Regex; use rustc_arena::TypedArena; -use rustc_ast::CRATE_NODE_ID; -use rustc_attr_parsing::{ShouldEmit, eval_config_entry}; +use rustc_attr_parsing::eval_config_entry; use rustc_data_structures::fx::FxIndexSet; use rustc_data_structures::memmap::Mmap; use rustc_data_structures::temp_dir::MaybeTempDir; @@ -3033,9 +3032,7 @@ fn add_dynamic_crate(cmd: &mut dyn Linker, sess: &Session, cratepath: &Path) { fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool { match lib.cfg { - Some(ref cfg) => { - eval_config_entry(sess, cfg, CRATE_NODE_ID, ShouldEmit::ErrorsAndLints).as_bool() - } + Some(ref cfg) => eval_config_entry(sess, cfg).as_bool(), None => true, } } diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index e93d9702774e1..10dc1cace2fc2 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -165,10 +165,7 @@ pub fn pre_configure_attrs(sess: &Session, attrs: &[Attribute]) -> ast::AttrVec .iter() .flat_map(|attr| strip_unconfigured.process_cfg_attr(attr)) .take_while(|attr| { - !is_cfg(attr) - || strip_unconfigured - .cfg_true(attr, strip_unconfigured.lint_node_id, ShouldEmit::Nothing) - .as_bool() + !is_cfg(attr) || strip_unconfigured.cfg_true(attr, ShouldEmit::Nothing).as_bool() }) .collect() } @@ -318,14 +315,7 @@ impl<'a> StripUnconfigured<'a> { ); } - if !attr::eval_config_entry( - self.sess, - &cfg_predicate, - ast::CRATE_NODE_ID, - ShouldEmit::ErrorsAndLints, - ) - .as_bool() - { + if !attr::eval_config_entry(self.sess, &cfg_predicate).as_bool() { return vec![trace_attr]; } @@ -409,18 +399,12 @@ impl<'a> StripUnconfigured<'a> { /// Determines if a node with the given attributes should be included in this configuration. fn in_cfg(&self, attrs: &[Attribute]) -> bool { - attrs.iter().all(|attr| { - !is_cfg(attr) - || self.cfg_true(attr, self.lint_node_id, ShouldEmit::ErrorsAndLints).as_bool() - }) + attrs + .iter() + .all(|attr| !is_cfg(attr) || self.cfg_true(attr, ShouldEmit::ErrorsAndLints).as_bool()) } - pub(crate) fn cfg_true( - &self, - attr: &Attribute, - node: NodeId, - emit_errors: ShouldEmit, - ) -> EvalConfigResult { + pub(crate) fn cfg_true(&self, attr: &Attribute, emit_errors: ShouldEmit) -> EvalConfigResult { // Unsafety check needs to be done explicitly here because this attribute will be removed before the normal check deny_builtin_meta_unsafety( self.sess.dcx(), @@ -432,7 +416,7 @@ impl<'a> StripUnconfigured<'a> { self.sess, attr, attr.span, - node, + self.lint_node_id, self.features, emit_errors, parse_cfg, @@ -442,7 +426,7 @@ impl<'a> StripUnconfigured<'a> { return EvalConfigResult::True; }; - eval_config_entry(self.sess, &cfg, self.lint_node_id, emit_errors) + eval_config_entry(self.sess, &cfg) } /// If attributes are not allowed on expressions, emit an error for `attr` diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 6d048c120a211..8507dffb880fb 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -2213,11 +2213,11 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { fn expand_cfg_true( &mut self, - node: &mut (impl HasAttrs + HasNodeId), + node: &mut impl HasAttrs, attr: ast::Attribute, pos: usize, ) -> EvalConfigResult { - let res = self.cfg().cfg_true(&attr, node.node_id(), ShouldEmit::ErrorsAndLints); + let res = self.cfg().cfg_true(&attr, ShouldEmit::ErrorsAndLints); if res.as_bool() { // A trace attribute left in AST in place of the original `cfg` attribute. // It can later be used by lints or other diagnostics. diff --git a/compiler/rustc_hir/src/lints.rs b/compiler/rustc_hir/src/lints.rs index c9de6f6b5d526..a8696aa44f3ff 100644 --- a/compiler/rustc_hir/src/lints.rs +++ b/compiler/rustc_hir/src/lints.rs @@ -1,6 +1,6 @@ use rustc_data_structures::fingerprint::Fingerprint; use rustc_macros::HashStable_Generic; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use crate::{AttrPath, HirId, Target}; @@ -62,4 +62,14 @@ pub enum AttributeLintKind { target: Target, target_span: Span, }, + UnexpectedCfgName { + name: Symbol, + name_span: Span, + value: Option<(Symbol, Span)>, + }, + UnexpectedCfgValue { + name: Symbol, + name_span: Span, + value: Option<(Symbol, Span)>, + }, } diff --git a/compiler/rustc_hir_analysis/src/lib.rs b/compiler/rustc_hir_analysis/src/lib.rs index 1d2a456b555e7..8d79a113ae997 100644 --- a/compiler/rustc_hir_analysis/src/lib.rs +++ b/compiler/rustc_hir_analysis/src/lib.rs @@ -158,7 +158,7 @@ pub fn provide(providers: &mut Providers) { fn emit_delayed_lint(lint: &DelayedLint, tcx: TyCtxt<'_>) { match lint { DelayedLint::AttributeParsing(attribute_lint) => { - rustc_attr_parsing::emit_attribute_lint(attribute_lint, tcx) + rustc_attr_parsing::emit_attribute_lint(attribute_lint, tcx, tcx.sess) } } } diff --git a/compiler/rustc_metadata/src/native_libs.rs b/compiler/rustc_metadata/src/native_libs.rs index 250657bc6806b..e08460c3d4c9a 100644 --- a/compiler/rustc_metadata/src/native_libs.rs +++ b/compiler/rustc_metadata/src/native_libs.rs @@ -2,8 +2,7 @@ use std::ops::ControlFlow; use std::path::{Path, PathBuf}; use rustc_abi::ExternAbi; -use rustc_ast::CRATE_NODE_ID; -use rustc_attr_parsing::{ShouldEmit, eval_config_entry}; +use rustc_attr_parsing::eval_config_entry; use rustc_data_structures::fx::FxHashSet; use rustc_hir::attrs::{AttributeKind, NativeLibKind, PeImportNameType}; use rustc_hir::find_attr; @@ -188,9 +187,7 @@ pub(crate) fn collect(tcx: TyCtxt<'_>, LocalCrate: LocalCrate) -> Vec pub(crate) fn relevant_lib(sess: &Session, lib: &NativeLib) -> bool { match lib.cfg { - Some(ref cfg) => { - eval_config_entry(sess, cfg, CRATE_NODE_ID, ShouldEmit::ErrorsAndLints).as_bool() - } + Some(ref cfg) => eval_config_entry(sess, cfg).as_bool(), None => true, } } diff --git a/tests/ui/feature-gates/feature-gate-link_cfg.rs b/tests/ui/feature-gates/feature-gate-link_cfg.rs index d30ee3bcfdb14..286c3d0a4977d 100644 --- a/tests/ui/feature-gates/feature-gate-link_cfg.rs +++ b/tests/ui/feature-gates/feature-gate-link_cfg.rs @@ -1,4 +1,4 @@ -#[link(name = "foo", cfg(foo))] +#[link(name = "foo", cfg(false))] //~^ ERROR: is unstable extern "C" {} diff --git a/tests/ui/feature-gates/feature-gate-link_cfg.stderr b/tests/ui/feature-gates/feature-gate-link_cfg.stderr index bfe7f74a92137..0e9707c6c9620 100644 --- a/tests/ui/feature-gates/feature-gate-link_cfg.stderr +++ b/tests/ui/feature-gates/feature-gate-link_cfg.stderr @@ -1,8 +1,8 @@ error[E0658]: link cfg is unstable --> $DIR/feature-gate-link_cfg.rs:1:22 | -LL | #[link(name = "foo", cfg(foo))] - | ^^^^^^^^ +LL | #[link(name = "foo", cfg(false))] + | ^^^^^^^^^^ | = help: add `#![feature(link_cfg)]` to the crate attributes to enable = note: this compiler was built on YYYY-MM-DD; consider upgrading it if it is out of date diff --git a/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-dylib.rs b/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-dylib.rs index 0d927117d81d3..55aa7a0a8caaa 100644 --- a/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-dylib.rs +++ b/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-dylib.rs @@ -1,4 +1,4 @@ #![feature(link_cfg)] -#[link(name = "foo", cfg(foo))] +#[link(name = "foo", cfg(false))] extern "C" {} diff --git a/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-rlib.rs b/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-rlib.rs index 49a46b202e4b1..46a383a08f9d1 100644 --- a/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-rlib.rs +++ b/tests/ui/link-native-libs/auxiliary/link-cfg-works-transitive-rlib.rs @@ -3,5 +3,5 @@ #![feature(link_cfg)] #![crate_type = "rlib"] -#[link(name = "foo", cfg(foo))] +#[link(name = "foo", cfg(false))] extern "C" {} diff --git a/tests/ui/link-native-libs/link-cfg-works.rs b/tests/ui/link-native-libs/link-cfg-works.rs index 7b936bc43b1a0..0e02933ce01c2 100644 --- a/tests/ui/link-native-libs/link-cfg-works.rs +++ b/tests/ui/link-native-libs/link-cfg-works.rs @@ -7,7 +7,7 @@ extern crate link_cfg_works_transitive_dylib; extern crate link_cfg_works_transitive_rlib; -#[link(name = "foo", cfg(foo))] +#[link(name = "foo", cfg(false))] extern "C" {} fn main() {} diff --git a/tests/ui/macros/cfg.rs b/tests/ui/macros/cfg.rs index d992ec82e2fd2..f0c51b2942f16 100644 --- a/tests/ui/macros/cfg.rs +++ b/tests/ui/macros/cfg.rs @@ -2,5 +2,6 @@ fn main() { cfg!(); //~ ERROR macro requires a cfg-pattern cfg!(123); //~ ERROR malformed `cfg` macro input cfg!(foo = 123); //~ ERROR malformed `cfg` macro input - cfg!(foo, bar); //~ ERROR expected 1 cfg-pattern + cfg!(false, false); //~ ERROR expected 1 cfg-pattern + cfg!(foo); //~ WARN unexpected `cfg` condition name: `foo` } diff --git a/tests/ui/macros/cfg.stderr b/tests/ui/macros/cfg.stderr index 9a4c187f37b27..06529a5b7a61f 100644 --- a/tests/ui/macros/cfg.stderr +++ b/tests/ui/macros/cfg.stderr @@ -29,9 +29,20 @@ LL | cfg!(foo = 123); error: expected 1 cfg-pattern --> $DIR/cfg.rs:5:5 | -LL | cfg!(foo, bar); - | ^^^^^^^^^^^^^^ +LL | cfg!(false, false); + | ^^^^^^^^^^^^^^^^^^ -error: aborting due to 4 previous errors +warning: unexpected `cfg` condition name: `foo` + --> $DIR/cfg.rs:6:10 + | +LL | cfg!(foo); + | ^^^ + | + = help: expected names are: `FALSE` and `test` and 31 more + = help: to expect this configuration use `--check-cfg=cfg(foo)` + = note: see for more information about checking conditional configuration + = note: `#[warn(unexpected_cfgs)]` on by default + +error: aborting due to 4 previous errors; 1 warning emitted For more information about this error, try `rustc --explain E0539`. diff --git a/tests/ui/macros/cfg_select.rs b/tests/ui/macros/cfg_select.rs index 2a627cc05b93b..a54c385747fa6 100644 --- a/tests/ui/macros/cfg_select.rs +++ b/tests/ui/macros/cfg_select.rs @@ -89,9 +89,11 @@ cfg_select! { cfg_select! { a + 1 => {} //~^ ERROR expected one of `(`, `::`, `=>`, or `=`, found `+` + //~| WARN unexpected `cfg` condition name } cfg_select! { cfg!() => {} //~^ ERROR expected one of `(`, `::`, `=>`, or `=`, found `!` + //~| WARN unexpected `cfg` condition name } diff --git a/tests/ui/macros/cfg_select.stderr b/tests/ui/macros/cfg_select.stderr index 3a5d2b0a1e1ee..d79e1b9b5c10a 100644 --- a/tests/ui/macros/cfg_select.stderr +++ b/tests/ui/macros/cfg_select.stderr @@ -60,12 +60,32 @@ LL | a + 1 => {} | ^ expected one of `(`, `::`, `=>`, or `=` error: expected one of `(`, `::`, `=>`, or `=`, found `!` - --> $DIR/cfg_select.rs:95:8 + --> $DIR/cfg_select.rs:96:8 | LL | cfg!() => {} | ^ expected one of `(`, `::`, `=>`, or `=` -error: aborting due to 9 previous errors; 1 warning emitted +warning: unexpected `cfg` condition name: `a` + --> $DIR/cfg_select.rs:90:5 + | +LL | a + 1 => {} + | ^ help: found config with similar value: `target_feature = "a"` + | + = help: expected names are: `FALSE` and `test` and 31 more + = help: to expect this configuration use `--check-cfg=cfg(a)` + = note: see for more information about checking conditional configuration + = note: `#[warn(unexpected_cfgs)]` on by default + +warning: unexpected `cfg` condition name: `cfg` + --> $DIR/cfg_select.rs:96:5 + | +LL | cfg!() => {} + | ^^^ + | + = help: to expect this configuration use `--check-cfg=cfg(cfg)` + = note: see for more information about checking conditional configuration + +error: aborting due to 9 previous errors; 3 warnings emitted Some errors have detailed explanations: E0537, E0539. For more information about an error, try `rustc --explain E0537`. From f5e9e9327382587f706c6aed6a51b04edd64e4a1 Mon Sep 17 00:00:00 2001 From: Jonathan Brouwer Date: Sat, 22 Nov 2025 15:19:50 +0100 Subject: [PATCH 3/3] Remove `attr_parsing_unexpected_cfg_cargo_update` because it is sadly no longer triggered --- compiler/rustc_attr_parsing/messages.ftl | 1 - .../src/attributes/cfg/check_cfg.rs | 19 +++---------- compiler/rustc_attr_parsing/src/lints.rs | 4 +-- .../src/session_diagnostics.rs | 2 -- compiler/rustc_interface/src/passes.rs | 2 -- compiler/rustc_lint/src/early.rs | 27 ++++++++----------- compiler/rustc_lint/src/early/diagnostics.rs | 12 +++------ .../report-in-external-macros.cargo.stderr | 3 --- 8 files changed, 20 insertions(+), 50 deletions(-) diff --git a/compiler/rustc_attr_parsing/messages.ftl b/compiler/rustc_attr_parsing/messages.ftl index d90f1f129bed2..01f89688579f4 100644 --- a/compiler/rustc_attr_parsing/messages.ftl +++ b/compiler/rustc_attr_parsing/messages.ftl @@ -223,7 +223,6 @@ attr_parsing_unexpected_cfg_add_build_rs_println = or consider adding `{$build_r attr_parsing_unexpected_cfg_add_cargo_feature = consider using a Cargo feature instead attr_parsing_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} attr_parsing_unexpected_cfg_add_cmdline_arg = to expect this configuration use `{$cmdline_arg}` -attr_parsing_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}` attr_parsing_unexpected_cfg_define_features = consider defining some features in `Cargo.toml` attr_parsing_unexpected_cfg_doc_cargo = see for more information about checking conditional configuration diff --git a/compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs b/compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs index 2c385b9f3e6bd..89f07a7077192 100644 --- a/compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs +++ b/compiler/rustc_attr_parsing/src/attributes/cfg/check_cfg.rs @@ -1,6 +1,5 @@ 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; @@ -89,21 +88,13 @@ fn rustc_macro_help(span: Span) -> Option { } } -fn cargo_macro_help( - tcx: Option>, - span: Span, -) -> Option { +fn cargo_macro_help(span: Span) -> Option { 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, - crate_name: tcx.crate_name(def_id.krate), - }) + Some(lints::UnexpectedCfgCargoMacroHelp { macro_kind: macro_kind.descr(), macro_name }) } else { None } @@ -111,7 +102,6 @@ fn cargo_macro_help( pub fn unexpected_cfg_name( sess: &Session, - tcx: Option>, (name, name_span): (Symbol, Span), value: Option<(Symbol, Span)>, ) -> lints::UnexpectedCfgName { @@ -253,7 +243,7 @@ pub fn unexpected_cfg_name( }; lints::unexpected_cfg_name::InvocationHelp::Cargo { help, - macro_help: cargo_macro_help(tcx, name_span), + macro_help: cargo_macro_help(name_span), } } else { let help = lints::UnexpectedCfgRustcHelp::new(&inst(EscapeQuotes::No)); @@ -268,7 +258,6 @@ pub fn unexpected_cfg_name( pub fn unexpected_cfg_value( sess: &Session, - tcx: Option>, (name, name_span): (Symbol, Span), value: Option<(Symbol, Span)>, ) -> lints::UnexpectedCfgValue { @@ -373,7 +362,7 @@ pub fn unexpected_cfg_value( }; lints::unexpected_cfg_value::InvocationHelp::Cargo { help, - macro_help: cargo_macro_help(tcx, name_span), + macro_help: cargo_macro_help(name_span), } } else { let help = if can_suggest_adding_value { diff --git a/compiler/rustc_attr_parsing/src/lints.rs b/compiler/rustc_attr_parsing/src/lints.rs index 7fb5dc57d28dd..35f2c325bf0fb 100644 --- a/compiler/rustc_attr_parsing/src/lints.rs +++ b/compiler/rustc_attr_parsing/src/lints.rs @@ -109,14 +109,14 @@ pub fn emit_attribute_lint( rustc_session::lint::builtin::UNEXPECTED_CFGS, *id, *span, - unexpected_cfg_name(sess, None, (name, name_span), value), + unexpected_cfg_name(sess, (name, name_span), value), ), &AttributeLintKind::UnexpectedCfgValue { name, name_span, value } => lint_emitter .emit_node_span_lint( rustc_session::lint::builtin::UNEXPECTED_CFGS, *id, *span, - unexpected_cfg_value(sess, None, (name, name_span), value), + unexpected_cfg_value(sess, (name, name_span), value), ), } } diff --git a/compiler/rustc_attr_parsing/src/session_diagnostics.rs b/compiler/rustc_attr_parsing/src/session_diagnostics.rs index 5ce3b83622643..95709dd392232 100644 --- a/compiler/rustc_attr_parsing/src/session_diagnostics.rs +++ b/compiler/rustc_attr_parsing/src/session_diagnostics.rs @@ -1050,11 +1050,9 @@ pub struct UnexpectedCfgRustcMacroHelp { #[derive(Subdiagnostic)] #[note(attr_parsing_unexpected_cfg_from_external_macro_origin)] #[help(attr_parsing_unexpected_cfg_from_external_macro_refer)] -#[help(attr_parsing_unexpected_cfg_cargo_update)] pub struct UnexpectedCfgCargoMacroHelp { pub macro_kind: &'static str, pub macro_name: Symbol, - pub crate_name: Symbol, } #[derive(LintDiagnostic)] diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index ddfec9f886a6a..66ce81c8849be 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -94,7 +94,6 @@ fn pre_expansion_lint<'a>( || { rustc_lint::check_ast_node( sess, - None, features, true, lint_store, @@ -470,7 +469,6 @@ fn early_lint_checks(tcx: TyCtxt<'_>, (): ()) { let lint_store = unerased_lint_store(tcx.sess); rustc_lint::check_ast_node( sess, - Some(tcx), tcx.features(), false, lint_store, diff --git a/compiler/rustc_lint/src/early.rs b/compiler/rustc_lint/src/early.rs index 6cb5aea798407..2c2af01c78cef 100644 --- a/compiler/rustc_lint/src/early.rs +++ b/compiler/rustc_lint/src/early.rs @@ -9,7 +9,7 @@ use rustc_ast::{self as ast, AttrVec, HasAttrs}; use rustc_data_structures::stack::ensure_sufficient_stack; use rustc_errors::{BufferedEarlyLint, DecorateDiagCompat, LintBuffer}; use rustc_feature::Features; -use rustc_middle::ty::{RegisteredTools, TyCtxt}; +use rustc_middle::ty::RegisteredTools; use rustc_session::Session; use rustc_session::lint::LintPass; use rustc_span::{Ident, Span}; @@ -26,20 +26,19 @@ macro_rules! lint_callback { ($cx:expr, $f:ident, $($args:expr),*) => ({ /// Implements the AST traversal for early lint passes. `T` provides the /// `check_*` methods. -pub struct EarlyContextAndPass<'ecx, 'tcx, T: EarlyLintPass> { +pub struct EarlyContextAndPass<'ecx, T: EarlyLintPass> { context: EarlyContext<'ecx>, - tcx: Option>, pass: T, } -impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> { +impl<'ecx, T: EarlyLintPass> EarlyContextAndPass<'ecx, T> { #[allow(rustc::diagnostic_outside_of_impl)] fn check_id(&mut self, id: ast::NodeId) { for early_lint in self.context.buffered.take(id) { let BufferedEarlyLint { span, node_id: _, lint_id, diagnostic } = early_lint; self.context.opt_span_lint(lint_id.lint, span, |diag| match diagnostic { DecorateDiagCompat::Builtin(b) => { - diagnostics::decorate_builtin_lint(self.context.sess(), self.tcx, b, diag); + diagnostics::decorate_builtin_lint(self.context.sess(), b, diag); } DecorateDiagCompat::Dynamic(d) => d.decorate_lint_box(diag), }); @@ -66,9 +65,7 @@ impl<'ecx, 'tcx, T: EarlyLintPass> EarlyContextAndPass<'ecx, 'tcx, T> { } } -impl<'ast, 'ecx, 'tcx, T: EarlyLintPass> ast_visit::Visitor<'ast> - for EarlyContextAndPass<'ecx, 'tcx, T> -{ +impl<'ast, 'ecx, T: EarlyLintPass> ast_visit::Visitor<'ast> for EarlyContextAndPass<'ecx, T> { fn visit_id(&mut self, id: rustc_ast::NodeId) { self.check_id(id); } @@ -283,7 +280,7 @@ crate::early_lint_methods!(impl_early_lint_pass, []); pub trait EarlyCheckNode<'a>: Copy { fn id(self) -> ast::NodeId; fn attrs(self) -> &'a [ast::Attribute]; - fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>); + fn check<'ecx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, T>); } impl<'a> EarlyCheckNode<'a> for (&'a ast::Crate, &'a [ast::Attribute]) { @@ -293,7 +290,7 @@ impl<'a> EarlyCheckNode<'a> for (&'a ast::Crate, &'a [ast::Attribute]) { fn attrs(self) -> &'a [ast::Attribute] { self.1 } - fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) { + fn check<'ecx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, T>) { lint_callback!(cx, check_crate, self.0); ast_visit::walk_crate(cx, self.0); lint_callback!(cx, check_crate_post, self.0); @@ -307,7 +304,7 @@ impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [Box &'a [ast::Attribute] { self.1 } - fn check<'ecx, 'tcx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, 'tcx, T>) { + fn check<'ecx, T: EarlyLintPass>(self, cx: &mut EarlyContextAndPass<'ecx, T>) { walk_list!(cx, visit_attribute, self.1); walk_list!(cx, visit_item, self.2); } @@ -315,7 +312,6 @@ impl<'a> EarlyCheckNode<'a> for (ast::NodeId, &'a [ast::Attribute], &'a [Box( sess: &Session, - tcx: Option>, features: &Features, pre_expansion: bool, lint_store: &LintStore, @@ -339,23 +335,22 @@ pub fn check_ast_node<'a>( let passes = if pre_expansion { &lint_store.pre_expansion_passes } else { &lint_store.early_passes }; if passes.is_empty() { - check_ast_node_inner(sess, tcx, check_node, context, builtin_lints); + check_ast_node_inner(sess, check_node, context, builtin_lints); } else { let mut passes: Vec<_> = passes.iter().map(|mk_pass| (mk_pass)()).collect(); passes.push(Box::new(builtin_lints)); let pass = RuntimeCombinedEarlyLintPass { passes: &mut passes[..] }; - check_ast_node_inner(sess, tcx, check_node, context, pass); + check_ast_node_inner(sess, check_node, context, pass); } } fn check_ast_node_inner<'a, T: EarlyLintPass>( sess: &Session, - tcx: Option>, check_node: impl EarlyCheckNode<'a>, context: EarlyContext<'_>, pass: T, ) { - let mut cx = EarlyContextAndPass { context, tcx, pass }; + let mut cx = EarlyContextAndPass { context, pass }; cx.with_lint_attrs(check_node.id(), check_node.attrs(), |cx| check_node.check(cx)); diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index a29b15e424e74..1dc4483888bc1 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -5,7 +5,6 @@ use rustc_errors::{ Applicability, Diag, DiagArgValue, LintDiagnostic, elided_lifetime_in_path_suggestion, }; use rustc_middle::middle::stability; -use rustc_middle::ty::TyCtxt; use rustc_session::Session; use rustc_session::lint::BuiltinLintDiag; use rustc_span::BytePos; @@ -13,12 +12,7 @@ use tracing::debug; use crate::lints; -pub fn decorate_builtin_lint( - sess: &Session, - tcx: Option>, - diagnostic: BuiltinLintDiag, - diag: &mut Diag<'_, ()>, -) { +pub fn decorate_builtin_lint(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) { match diagnostic { BuiltinLintDiag::UnicodeTextFlow(comment_span, content) => { let spans: Vec<_> = content @@ -167,10 +161,10 @@ pub fn decorate_builtin_lint( .decorate_lint(diag); } BuiltinLintDiag::UnexpectedCfgName(name, value) => { - rustc_attr_parsing::unexpected_cfg_name(sess, tcx, name, value).decorate_lint(diag); + rustc_attr_parsing::unexpected_cfg_name(sess, name, value).decorate_lint(diag); } BuiltinLintDiag::UnexpectedCfgValue(name, value) => { - rustc_attr_parsing::unexpected_cfg_value(sess, tcx, name, value).decorate_lint(diag); + rustc_attr_parsing::unexpected_cfg_value(sess, name, value).decorate_lint(diag); } BuiltinLintDiag::DeprecatedWhereclauseLocation(left_sp, sugg) => { let suggestion = match sugg { diff --git a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr index 4b5fc91c7eb91..28dc1e8a201f0 100644 --- a/tests/ui/check-cfg/report-in-external-macros.cargo.stderr +++ b/tests/ui/check-cfg/report-in-external-macros.cargo.stderr @@ -7,7 +7,6 @@ LL | cfg_macro::my_lib_macro!(); = help: expected names are: `feature` and 31 more = 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 referring 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 the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro` = note: see 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) @@ -21,7 +20,6 @@ LL | cfg_macro::my_lib_macro_value!(); = note: expected values for `panic` are: `abort`, `immediate-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 referring 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 the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro` = note: see 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) @@ -34,7 +32,6 @@ 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 referring 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 the `cfg_macro` crate, try updating your dependency with `cargo update -p cfg_macro` = note: see 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)