From 2e816736efaebf1f4666efac1817bcccd78a3e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Tue, 26 Aug 2025 14:24:22 +0200 Subject: [PATCH 1/5] Move more early buffered lints to dyn lint diagnostics (1/N) --- compiler/rustc_builtin_macros/messages.ftl | 11 ++++ compiler/rustc_builtin_macros/src/asm.rs | 5 +- compiler/rustc_builtin_macros/src/errors.rs | 22 ++++++- .../rustc_builtin_macros/src/source_util.rs | 3 +- .../rustc_builtin_macros/src/test_harness.rs | 3 +- compiler/rustc_builtin_macros/src/util.rs | 3 +- compiler/rustc_expand/messages.ftl | 14 ++++- compiler/rustc_expand/src/config.rs | 3 +- compiler/rustc_expand/src/errors.rs | 42 ++++++++++++- compiler/rustc_expand/src/mbe/macro_check.rs | 45 +++++++++----- compiler/rustc_expand/src/mbe/transcribe.rs | 6 +- compiler/rustc_lint/messages.ftl | 31 ---------- compiler/rustc_lint/src/early/diagnostics.rs | 41 ------------ compiler/rustc_lint/src/lints.rs | 62 +------------------ compiler/rustc_lint_defs/src/lib.rs | 18 +----- compiler/rustc_resolve/messages.ftl | 6 ++ compiler/rustc_resolve/src/diagnostics.rs | 2 +- compiler/rustc_resolve/src/errors.rs | 18 +++++- compiler/rustc_resolve/src/ident.rs | 3 +- .../issue-61053-duplicate-binder.stderr | 4 +- 20 files changed, 150 insertions(+), 192 deletions(-) diff --git a/compiler/rustc_builtin_macros/messages.ftl b/compiler/rustc_builtin_macros/messages.ftl index 358c0d3db460f..7c1a5f44e165d 100644 --- a/compiler/rustc_builtin_macros/messages.ftl +++ b/compiler/rustc_builtin_macros/messages.ftl @@ -64,6 +64,11 @@ builtin_macros_autodiff_ty_activity = {$act} can not be used for this type builtin_macros_autodiff_unknown_activity = did not recognize Activity: `{$act}` builtin_macros_autodiff_width = autodiff width must fit u32, but is {$width} + +builtin_macros_avoid_att_syntax = avoid using `.att_syntax`, prefer using `options(att_syntax)` instead + +builtin_macros_avoid_intel_syntax = avoid using `.intel_syntax`, Intel syntax is the default + builtin_macros_bad_derive_target = `derive` may only be applied to `struct`s, `enum`s and `union`s .label = not applicable here .label2 = not a `struct`, `enum` or `union` @@ -138,6 +143,8 @@ builtin_macros_derive_path_args_list = traits in `#[derive(...)]` don't accept a builtin_macros_derive_path_args_value = traits in `#[derive(...)]` don't accept values .suggestion = remove the value +builtin_macros_duplicate_macro_attribute = duplicated attribute + builtin_macros_env_not_defined = environment variable `{$var}` not defined at compile time .cargo = Cargo sets build script variables at run time. Use `std::env::var({$var_expr})` instead .custom = use `std::env::var({$var_expr})` to read the variable at run time @@ -231,6 +238,8 @@ builtin_macros_derive_from_wrong_field_count = `#[derive(From)]` used on a struc builtin_macros_derive_from_usage_note = `#[derive(From)]` can only be used on structs with exactly one field +builtin_macros_incomplete_include = include macro expected single expression in source + builtin_macros_multiple_default_attrs = multiple `#[default]` attributes .note = only one `#[default]` attribute is needed .label = `#[default]` used here @@ -294,3 +303,5 @@ builtin_macros_unexpected_lit = expected path to a trait, found literal .label = not a trait .str_lit = try using `#[derive({$sym})]` .other = for example, write `#[derive(Debug)]` for `Debug` + +builtin_macros_unnameable_test_items = cannot test inner items diff --git a/compiler/rustc_builtin_macros/src/asm.rs b/compiler/rustc_builtin_macros/src/asm.rs index 86b8e1ff8dbb4..ae62b5ea2a097 100644 --- a/compiler/rustc_builtin_macros/src/asm.rs +++ b/compiler/rustc_builtin_macros/src/asm.rs @@ -1,4 +1,3 @@ -use lint::BuiltinLintDiag; use rustc_ast::tokenstream::TokenStream; use rustc_ast::{AsmMacro, token}; use rustc_data_structures::fx::{FxHashMap, FxIndexMap}; @@ -352,7 +351,7 @@ fn expand_preparsed_asm( lint::builtin::BAD_ASM_STYLE, find_span(".intel_syntax"), ecx.current_expansion.lint_node_id, - BuiltinLintDiag::AvoidUsingIntelSyntax, + errors::AvoidIntelSyntax, ); } if template_str.contains(".att_syntax") { @@ -360,7 +359,7 @@ fn expand_preparsed_asm( lint::builtin::BAD_ASM_STYLE, find_span(".att_syntax"), ecx.current_expansion.lint_node_id, - BuiltinLintDiag::AvoidUsingAttSyntax, + errors::AvoidAttSyntax, ); } } diff --git a/compiler/rustc_builtin_macros/src/errors.rs b/compiler/rustc_builtin_macros/src/errors.rs index 54e8f7503377f..0993fdc5be453 100644 --- a/compiler/rustc_builtin_macros/src/errors.rs +++ b/compiler/rustc_builtin_macros/src/errors.rs @@ -3,9 +3,29 @@ use rustc_errors::{ Diag, DiagCtxtHandle, Diagnostic, EmissionGuarantee, Level, MultiSpan, SingleLabelManySpans, Subdiagnostic, }; -use rustc_macros::{Diagnostic, Subdiagnostic}; +use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_span::{Ident, Span, Symbol}; +#[derive(LintDiagnostic)] +#[diag(builtin_macros_avoid_intel_syntax)] +pub(crate) struct AvoidIntelSyntax; + +#[derive(LintDiagnostic)] +#[diag(builtin_macros_avoid_att_syntax)] +pub(crate) struct AvoidAttSyntax; + +#[derive(LintDiagnostic)] +#[diag(builtin_macros_incomplete_include)] +pub(crate) struct IncompleteInclude; + +#[derive(LintDiagnostic)] +#[diag(builtin_macros_unnameable_test_items)] +pub(crate) struct UnnameableTestItems; + +#[derive(LintDiagnostic)] +#[diag(builtin_macros_duplicate_macro_attribute)] +pub(crate) struct DuplicateMacroAttribute; + #[derive(Diagnostic)] #[diag(builtin_macros_requires_cfg_pattern)] pub(crate) struct RequiresCfgPattern { diff --git a/compiler/rustc_builtin_macros/src/source_util.rs b/compiler/rustc_builtin_macros/src/source_util.rs index 11b868f81a976..16adaab15c525 100644 --- a/compiler/rustc_builtin_macros/src/source_util.rs +++ b/compiler/rustc_builtin_macros/src/source_util.rs @@ -12,7 +12,6 @@ use rustc_expand::base::{ DummyResult, ExpandResult, ExtCtxt, MacEager, MacResult, MacroExpanderResult, resolve_path, }; use rustc_expand::module::DirOwnership; -use rustc_lint_defs::BuiltinLintDiag; use rustc_parse::lexer::StripTokens; use rustc_parse::parser::ForceCollect; use rustc_parse::{new_parser_from_file, unwrap_or_emit_fatal, utf8_error}; @@ -159,7 +158,7 @@ pub(crate) fn expand_include<'cx>( INCOMPLETE_INCLUDE, p.token.span, self.node_id, - BuiltinLintDiag::IncompleteInclude, + errors::IncompleteInclude, ); } Some(expr) diff --git a/compiler/rustc_builtin_macros/src/test_harness.rs b/compiler/rustc_builtin_macros/src/test_harness.rs index a9d91f77560a7..51089e5a1d3f1 100644 --- a/compiler/rustc_builtin_macros/src/test_harness.rs +++ b/compiler/rustc_builtin_macros/src/test_harness.rs @@ -11,7 +11,6 @@ use rustc_errors::DiagCtxtHandle; use rustc_expand::base::{ExtCtxt, ResolverExpand}; use rustc_expand::expand::{AstFragment, ExpansionConfig}; use rustc_feature::Features; -use rustc_lint_defs::BuiltinLintDiag; use rustc_session::Session; use rustc_session::lint::builtin::UNNAMEABLE_TEST_ITEMS; use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency}; @@ -165,7 +164,7 @@ impl<'a> Visitor<'a> for InnerItemLinter<'_> { UNNAMEABLE_TEST_ITEMS, attr.span, i.id, - BuiltinLintDiag::UnnameableTestItems, + errors::UnnameableTestItems, ); } } diff --git a/compiler/rustc_builtin_macros/src/util.rs b/compiler/rustc_builtin_macros/src/util.rs index 3a4585d5be9d1..e26f31dce67bd 100644 --- a/compiler/rustc_builtin_macros/src/util.rs +++ b/compiler/rustc_builtin_macros/src/util.rs @@ -5,7 +5,6 @@ use rustc_errors::{Applicability, Diag, ErrorGuaranteed}; use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt}; use rustc_expand::expand::AstFragment; use rustc_feature::AttributeTemplate; -use rustc_lint_defs::BuiltinLintDiag; use rustc_lint_defs::builtin::DUPLICATE_MACRO_ATTRIBUTES; use rustc_parse::{exp, parser}; use rustc_session::errors::report_lit_error; @@ -49,7 +48,7 @@ pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable, DUPLICATE_MACRO_ATTRIBUTES, attr.span, ecx.current_expansion.lint_node_id, - BuiltinLintDiag::DuplicateMacroAttribute, + errors::DuplicateMacroAttribute, ); } } diff --git a/compiler/rustc_expand/messages.ftl b/compiler/rustc_expand/messages.ftl index 61ba716d082b0..fc50ac6e3bfeb 100644 --- a/compiler/rustc_expand/messages.ftl +++ b/compiler/rustc_expand/messages.ftl @@ -3,6 +3,8 @@ expand_attributes_on_expressions_experimental = .help_outer_doc = `///` is used for outer documentation comments; for a plain comment, use `//` .help_inner_doc = `//!` is used for inner documentation comments; for a plain comment, use `//` by removing the `!` or inserting a space in between them: `// !` +expand_cfg_attr_no_attributes = `#[cfg_attr]` does not expand to any attributes + expand_collapse_debuginfo_illegal = illegal value for attribute #[collapse_debuginfo(no|external|yes)] @@ -89,6 +91,13 @@ expand_malformed_feature_attribute = malformed `feature` attribute input .expected = expected just one word +expand_metavar_still_repeating = variable `{$ident}` is still repeating at this depth + .label = expected repetition + +expand_metavariable_wrong_operator = meta-variable repeats with different Kleene operator + .binder_label = expected repetition + .occurrence_label = conflicting repetition + expand_meta_var_dif_seq_matchers = {$msg} expand_missing_fragment_specifier = missing fragment specifier @@ -176,11 +185,10 @@ expand_resolve_relative_path = expand_trace_macro = trace_macro +expand_unknown_macro_variable = unknown macro variable `{$name}` + expand_unsupported_key_value = key-value macro attributes are not supported -expand_var_still_repeating = - variable `{$ident}` is still repeating at this depth - expand_wrong_fragment_kind = non-{$kind} macro in {$kind} position: {$name} diff --git a/compiler/rustc_expand/src/config.rs b/compiler/rustc_expand/src/config.rs index 15419ab7423f4..2925e337071ca 100644 --- a/compiler/rustc_expand/src/config.rs +++ b/compiler/rustc_expand/src/config.rs @@ -21,7 +21,6 @@ use rustc_feature::{ ACCEPTED_LANG_FEATURES, AttributeSafety, EnabledLangFeature, EnabledLibFeature, Features, REMOVED_LANG_FEATURES, UNSTABLE_LANG_FEATURES, }; -use rustc_lint_defs::BuiltinLintDiag; use rustc_session::Session; use rustc_session::parse::feature_err; use rustc_span::{STDLIB_STABLE_CRATES, Span, Symbol, sym}; @@ -315,7 +314,7 @@ impl<'a> StripUnconfigured<'a> { rustc_lint_defs::builtin::UNUSED_ATTRIBUTES, cfg_attr.span, ast::CRATE_NODE_ID, - BuiltinLintDiag::CfgAttrNoAttributes, + crate::errors::CfgAttrNoAttributes, ); } diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index 6eb5cd65846eb..a939dadaf2fcd 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -3,9 +3,13 @@ use std::borrow::Cow; use rustc_ast::ast; use rustc_errors::codes::*; use rustc_hir::limit::Limit; -use rustc_macros::{Diagnostic, Subdiagnostic}; +use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol}; +#[derive(LintDiagnostic)] +#[diag(expand_cfg_attr_no_attributes)] +pub(crate) struct CfgAttrNoAttributes; + #[derive(Diagnostic)] #[diag(expand_expr_repeat_no_syntax_vars)] pub(crate) struct NoSyntaxVarsExprRepeat { @@ -28,13 +32,30 @@ pub(crate) struct CountRepetitionMisplaced { } #[derive(Diagnostic)] -#[diag(expand_var_still_repeating)] -pub(crate) struct VarStillRepeating { +#[diag(expand_metavar_still_repeating)] +pub(crate) struct MacroVarStillRepeating { #[primary_span] pub span: Span, pub ident: MacroRulesNormalizedIdent, } +#[derive(LintDiagnostic)] +#[diag(expand_metavar_still_repeating)] +pub(crate) struct MetaVarStillRepeatingLint { + #[label] + pub label: Span, + pub ident: MacroRulesNormalizedIdent, +} + +#[derive(LintDiagnostic)] +#[diag(expand_metavariable_wrong_operator)] +pub(crate) struct MetaVariableWrongOperator { + #[label(expand_binder_label)] + pub binder: Span, + #[label(expand_occurrence_label)] + pub occurrence: Span, +} + #[derive(Diagnostic)] #[diag(expand_meta_var_dif_seq_matchers)] pub(crate) struct MetaVarsDifSeqMatchers { @@ -43,6 +64,12 @@ pub(crate) struct MetaVarsDifSeqMatchers { pub msg: String, } +#[derive(LintDiagnostic)] +#[diag(expand_unknown_macro_variable)] +pub(crate) struct UnknownMacroVariable { + pub name: MacroRulesNormalizedIdent, +} + #[derive(Diagnostic)] #[diag(expand_resolve_relative_path)] pub(crate) struct ResolveRelativePath { @@ -345,6 +372,15 @@ pub(crate) struct DuplicateMatcherBinding { pub prev: Span, } +#[derive(LintDiagnostic)] +#[diag(expand_duplicate_matcher_binding)] +pub(crate) struct DuplicateMatcherBindingLint { + #[label] + pub span: Span, + #[label(expand_label2)] + pub prev: Span, +} + #[derive(Diagnostic)] #[diag(expand_missing_fragment_specifier)] #[note] diff --git a/compiler/rustc_expand/src/mbe/macro_check.rs b/compiler/rustc_expand/src/mbe/macro_check.rs index faeae1f494e65..ebd6e887f7d28 100644 --- a/compiler/rustc_expand/src/mbe/macro_check.rs +++ b/compiler/rustc_expand/src/mbe/macro_check.rs @@ -108,8 +108,7 @@ use rustc_ast::token::{Delimiter, IdentIsRaw, Token, TokenKind}; use rustc_ast::{DUMMY_NODE_ID, NodeId}; use rustc_data_structures::fx::FxHashMap; -use rustc_errors::MultiSpan; -use rustc_lint_defs::BuiltinLintDiag; +use rustc_errors::DecorateDiagCompat; use rustc_session::lint::builtin::META_VARIABLE_MISUSE; use rustc_session::parse::ParseSess; use rustc_span::{ErrorGuaranteed, MacroRulesNormalizedIdent, Span, kw}; @@ -245,9 +244,12 @@ fn check_binders( // There are 3 possibilities: if let Some(prev_info) = binders.get(&name) { // 1. The meta-variable is already bound in the current LHS: This is an error. - let mut span = MultiSpan::from_span(span); - span.push_span_label(prev_info.span, "previous declaration"); - buffer_lint(psess, span, node_id, BuiltinLintDiag::DuplicateMatcherBinding); + buffer_lint( + psess, + span, + node_id, + errors::DuplicateMatcherBindingLint { span, prev: prev_info.span }, + ); } else if get_binder_info(macros, binders, name).is_none() { // 2. The meta-variable is free: This is a binder. binders.insert(name, BinderInfo { span, ops: ops.into() }); @@ -579,7 +581,7 @@ fn check_ops_is_prefix( return; } } - buffer_lint(psess, span.into(), node_id, BuiltinLintDiag::UnknownMacroVariable(name)); + buffer_lint(psess, span, node_id, errors::UnknownMacroVariable { name }); } /// Returns whether `binder_ops` is a prefix of `occurrence_ops`. @@ -604,29 +606,42 @@ fn ops_is_prefix( psess: &ParseSess, node_id: NodeId, span: Span, - name: MacroRulesNormalizedIdent, + ident: MacroRulesNormalizedIdent, binder_ops: &[KleeneToken], occurrence_ops: &[KleeneToken], ) { for (i, binder) in binder_ops.iter().enumerate() { if i >= occurrence_ops.len() { - let mut span = MultiSpan::from_span(span); - span.push_span_label(binder.span, "expected repetition"); - buffer_lint(psess, span, node_id, BuiltinLintDiag::MetaVariableStillRepeating(name)); + buffer_lint( + psess, + span, + node_id, + errors::MetaVarStillRepeatingLint { label: binder.span, ident }, + ); return; } let occurrence = &occurrence_ops[i]; if occurrence.op != binder.op { - let mut span = MultiSpan::from_span(span); - span.push_span_label(binder.span, "expected repetition"); - span.push_span_label(occurrence.span, "conflicting repetition"); - buffer_lint(psess, span, node_id, BuiltinLintDiag::MetaVariableWrongOperator); + buffer_lint( + psess, + span, + node_id, + errors::MetaVariableWrongOperator { + binder: binder.span, + occurrence: occurrence.span, + }, + ); return; } } } -fn buffer_lint(psess: &ParseSess, span: MultiSpan, node_id: NodeId, diag: BuiltinLintDiag) { +fn buffer_lint( + psess: &ParseSess, + span: Span, + node_id: NodeId, + diag: impl Into, +) { // Macros loaded from other crates have dummy node ids. if node_id != DUMMY_NODE_ID { psess.buffer_lint(META_VARIABLE_MISUSE, span, node_id, diag); diff --git a/compiler/rustc_expand/src/mbe/transcribe.rs b/compiler/rustc_expand/src/mbe/transcribe.rs index ed8aa71d59dda..6a3f1f62c91e1 100644 --- a/compiler/rustc_expand/src/mbe/transcribe.rs +++ b/compiler/rustc_expand/src/mbe/transcribe.rs @@ -17,8 +17,8 @@ use rustc_span::{ use smallvec::{SmallVec, smallvec}; use crate::errors::{ - CountRepetitionMisplaced, MetaVarsDifSeqMatchers, MustRepeatOnce, MveUnrecognizedVar, - NoSyntaxVarsExprRepeat, VarStillRepeating, + CountRepetitionMisplaced, MacroVarStillRepeating, MetaVarsDifSeqMatchers, MustRepeatOnce, + MveUnrecognizedVar, NoSyntaxVarsExprRepeat, }; use crate::mbe::macro_parser::NamedMatch; use crate::mbe::macro_parser::NamedMatch::*; @@ -483,7 +483,7 @@ fn transcribe_metavar<'tx>( } MatchedSeq(..) => { // We were unable to descend far enough. This is an error. - return Err(dcx.create_err(VarStillRepeating { span: sp, ident })); + return Err(dcx.create_err(MacroVarStillRepeating { span: sp, ident })); } }; diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 417e5a97069c3..5a60a4f48d9db 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -40,12 +40,6 @@ lint_atomic_ordering_load = atomic loads cannot have `Release` or `AcqRel` order lint_atomic_ordering_store = atomic stores cannot have `Acquire` or `AcqRel` ordering .help = consider using ordering modes `Release`, `SeqCst` or `Relaxed` -lint_avoid_att_syntax = - avoid using `.att_syntax`, prefer using `options(att_syntax)` instead - -lint_avoid_intel_syntax = - avoid using `.intel_syntax`, Intel syntax is the default - lint_bad_attribute_argument = bad attribute argument lint_bad_opt_access = {$msg} @@ -190,9 +184,6 @@ lint_builtin_while_true = denote infinite loops with `loop {"{"} ... {"}"}` lint_byte_slice_in_packed_struct_with_derive = {$ty} slice in a packed struct that derives a built-in trait .help = consider implementing the trait by hand, or remove the `packed` attribute -lint_cfg_attr_no_attributes = - `#[cfg_attr]` does not expand to any attributes - lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}` lint_closure_returning_async_block = closure returning async block can be made into an async closure @@ -249,11 +240,6 @@ lint_dropping_copy_types = calls to `std::mem::drop` with a value that implement lint_dropping_references = calls to `std::mem::drop` with a reference instead of an owned value does nothing .label = argument has type `{$arg_ty}` -lint_duplicate_macro_attribute = - duplicated attribute - -lint_duplicate_matcher_binding = duplicate matcher binding - lint_enum_intrinsics_mem_discriminant = the return value of `mem::discriminant` is unspecified when called with a non-enum type .note = the argument to `discriminant` should be a reference to an enum, but it was passed a reference to a `{$ty_param}`, which is not an enum @@ -409,9 +395,6 @@ lint_improper_ctypes_union_layout_help = consider adding a `#[repr(C)]` or `#[re lint_improper_ctypes_union_layout_reason = this union has unspecified layout lint_improper_ctypes_union_non_exhaustive = this union is non-exhaustive -lint_incomplete_include = - include macro expected single expression in source - lint_invalid_asm_label_binary = avoid using labels containing only the digits `0` and `1` in inline assembly .label = use a different label that doesn't start with `0` or `1` .help = start numbering with `2` instead @@ -476,9 +459,6 @@ lint_legacy_derive_helpers = derive helper attribute is used before it is introd lint_lintpass_by_hand = implementing `LintPass` by hand .help = try using `declare_lint_pass!` or `impl_lint_pass!` instead -lint_macro_expanded_macro_exports_accessed_by_absolute_paths = macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths - .note = the macro is defined here - lint_macro_expr_fragment_specifier_2024_migration = the `expr` fragment specifier will accept more expressions in the 2024 edition .suggestion = to keep the existing behavior, use the `expr_2021` fragment specifier @@ -499,10 +479,6 @@ lint_map_unit_fn = `Iterator::map` call that discard the iterator's values .map_label = after this call to map, the resulting iterator is `impl Iterator`, which means the only information carried by the iterator is the number of items .suggestion = you might have meant to use `Iterator::for_each` -lint_metavariable_still_repeating = variable `{$name}` is still repeating at this depth - -lint_metavariable_wrong_operator = meta-variable repeats with different Kleene operator - lint_mismatched_lifetime_syntaxes_eliding_while_named = eliding a lifetime that's named elsewhere is confusing @@ -719,9 +695,6 @@ lint_pattern_in_foreign = patterns aren't allowed in foreign function declaratio lint_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported .suggestion = consider making the `extern crate` item publicly accessible -lint_proc_macro_derive_resolution_fallback = cannot find {$ns_descr} `{$ident}` in this scope - .label = names from parent modules are not accessible without an explicit import - lint_query_instability = using `{$query}` can result in unstable query results .note = if you believe this case to be fine, allow this lint and add a comment explaining your rationale @@ -935,13 +908,9 @@ lint_unknown_lint = *[false] did you mean: `{$replace}` } -lint_unknown_macro_variable = unknown macro variable `{$name}` - lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}` .help = add `#![register_tool({$tool_name})]` to the crate root -lint_unnameable_test_items = cannot test inner items - lint_unnecessary_qualification = unnecessary qualification .suggestion = remove the unnecessary path segments diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index 7300490b838b0..4463586515ec1 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -64,17 +64,6 @@ pub fn decorate_builtin_lint( } .decorate_lint(diag); } - BuiltinLintDiag::ProcMacroDeriveResolutionFallback { - span: macro_span, - ns_descr, - ident, - } => lints::ProcMacroDeriveResolutionFallback { span: macro_span, ns_descr, ident } - .decorate_lint(diag), - BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def) => { - lints::MacroExpandedMacroExportsAccessedByAbsolutePaths { definition: span_def } - .decorate_lint(diag) - } - BuiltinLintDiag::ElidedLifetimesInPaths(n, path_span, incl_angl_brckt, insertion_span) => { lints::ElidedLifetimesInPaths { subdiag: elided_lifetime_in_path_suggestion( @@ -398,36 +387,6 @@ pub fn decorate_builtin_lint( BuiltinLintDiag::UnstableFeature(msg) => { lints::UnstableFeature { msg }.decorate_lint(diag); } - BuiltinLintDiag::AvoidUsingIntelSyntax => { - lints::AvoidIntelSyntax.decorate_lint(diag); - } - BuiltinLintDiag::AvoidUsingAttSyntax => { - lints::AvoidAttSyntax.decorate_lint(diag); - } - BuiltinLintDiag::IncompleteInclude => { - lints::IncompleteInclude.decorate_lint(diag); - } - BuiltinLintDiag::UnnameableTestItems => { - lints::UnnameableTestItems.decorate_lint(diag); - } - BuiltinLintDiag::DuplicateMacroAttribute => { - lints::DuplicateMacroAttribute.decorate_lint(diag); - } - BuiltinLintDiag::CfgAttrNoAttributes => { - lints::CfgAttrNoAttributes.decorate_lint(diag); - } - BuiltinLintDiag::MetaVariableStillRepeating(name) => { - lints::MetaVariableStillRepeating { name }.decorate_lint(diag); - } - BuiltinLintDiag::MetaVariableWrongOperator => { - lints::MetaVariableWrongOperator.decorate_lint(diag); - } - BuiltinLintDiag::DuplicateMatcherBinding => { - lints::DuplicateMatcherBinding.decorate_lint(diag); - } - BuiltinLintDiag::UnknownMacroVariable(name) => { - lints::UnknownMacroVariable { name }.decorate_lint(diag); - } BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => { lints::UnusedCrateDependency { extern_crate, local_crate }.decorate_lint(diag) } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 56d65ed08f9e3..1c13fadeef968 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -17,7 +17,7 @@ use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt}; use rustc_session::Session; use rustc_session::lint::AmbiguityErrorDiag; use rustc_span::edition::Edition; -use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym}; +use rustc_span::{Ident, Span, Symbol, sym}; use crate::builtin::{InitError, ShorthandAssocTyCollector, TypeAliasBounds}; use crate::errors::{OverruledAttributeSub, RequestedLevel}; @@ -2585,50 +2585,6 @@ impl<'a> LintDiagnostic<'a, ()> for UnstableFeature { } } -#[derive(LintDiagnostic)] -#[diag(lint_avoid_intel_syntax)] -pub(crate) struct AvoidIntelSyntax; - -#[derive(LintDiagnostic)] -#[diag(lint_avoid_att_syntax)] -pub(crate) struct AvoidAttSyntax; - -#[derive(LintDiagnostic)] -#[diag(lint_incomplete_include)] -pub(crate) struct IncompleteInclude; - -#[derive(LintDiagnostic)] -#[diag(lint_unnameable_test_items)] -pub(crate) struct UnnameableTestItems; - -#[derive(LintDiagnostic)] -#[diag(lint_duplicate_macro_attribute)] -pub(crate) struct DuplicateMacroAttribute; - -#[derive(LintDiagnostic)] -#[diag(lint_cfg_attr_no_attributes)] -pub(crate) struct CfgAttrNoAttributes; - -#[derive(LintDiagnostic)] -#[diag(lint_metavariable_still_repeating)] -pub(crate) struct MetaVariableStillRepeating { - pub name: MacroRulesNormalizedIdent, -} - -#[derive(LintDiagnostic)] -#[diag(lint_metavariable_wrong_operator)] -pub(crate) struct MetaVariableWrongOperator; - -#[derive(LintDiagnostic)] -#[diag(lint_duplicate_matcher_binding)] -pub(crate) struct DuplicateMatcherBinding; - -#[derive(LintDiagnostic)] -#[diag(lint_unknown_macro_variable)] -pub(crate) struct UnknownMacroVariable { - pub name: MacroRulesNormalizedIdent, -} - #[derive(LintDiagnostic)] #[diag(lint_unused_crate_dependency)] #[help] @@ -2714,22 +2670,6 @@ pub(crate) struct AbsPathWithModuleSugg { pub replacement: String, } -#[derive(LintDiagnostic)] -#[diag(lint_proc_macro_derive_resolution_fallback)] -pub(crate) struct ProcMacroDeriveResolutionFallback { - #[label] - pub span: Span, - pub ns_descr: &'static str, - pub ident: Ident, -} - -#[derive(LintDiagnostic)] -#[diag(lint_macro_expanded_macro_exports_accessed_by_absolute_paths)] -pub(crate) struct MacroExpandedMacroExportsAccessedByAbsolutePaths { - #[note] - pub definition: Span, -} - #[derive(LintDiagnostic)] #[diag(lint_hidden_lifetime_parameters)] pub(crate) struct ElidedLifetimesInPaths { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 2e84233e5a569..77c17c6c1e39e 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -11,7 +11,7 @@ use rustc_hir_id::{HashStableContext, HirId, ItemLocalId}; use rustc_macros::{Decodable, Encodable, HashStable_Generic}; use rustc_span::def_id::DefPathHash; pub use rustc_span::edition::Edition; -use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym}; +use rustc_span::{Ident, Span, Symbol, sym}; use serde::{Deserialize, Serialize}; pub use self::Level::*; @@ -620,12 +620,6 @@ pub enum DeprecatedSinceKind { #[derive(Debug)] pub enum BuiltinLintDiag { AbsPathWithModule(Span), - ProcMacroDeriveResolutionFallback { - span: Span, - ns_descr: &'static str, - ident: Ident, - }, - MacroExpandedMacroExportsAccessedByAbsolutePaths(Span), ElidedLifetimesInPaths(usize, Span, bool, Span), UnknownCrateTypes { span: Span, @@ -774,16 +768,6 @@ pub enum BuiltinLintDiag { UnusedMacroDefinition(Symbol), MacroRuleNeverUsed(usize, Symbol), UnstableFeature(DiagMessage), - AvoidUsingIntelSyntax, - AvoidUsingAttSyntax, - IncompleteInclude, - UnnameableTestItems, - DuplicateMacroAttribute, - CfgAttrNoAttributes, - MetaVariableStillRepeating(MacroRulesNormalizedIdent), - MetaVariableWrongOperator, - DuplicateMatcherBinding, - UnknownMacroVariable(MacroRulesNormalizedIdent), UnusedCrateDependency { extern_crate: Symbol, local_crate: Symbol, diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 0e566e20a124d..1c0e5ad65b3ab 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -260,6 +260,9 @@ resolve_macro_defined_later = resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments = macro-expanded `extern crate` items cannot shadow names passed with `--extern` +resolve_macro_expanded_macro_exports_accessed_by_absolute_paths = macro-expanded `macro_export` macros from the current crate cannot be referred to by absolute paths + .note = the macro is defined here + resolve_macro_expected_found = expected {$expected}, found {$found} `{$macro_path}` .label = not {$article} {$expected} @@ -339,6 +342,9 @@ resolve_param_in_ty_of_const_param = resolve_pattern_doesnt_bind_name = pattern doesn't bind `{$name}` +resolve_proc_macro_derive_resolution_fallback = cannot find {$ns_descr} `{$ident}` in this scope + .label = names from parent modules are not accessible without an explicit import + resolve_proc_macro_same_crate = can't use a procedural macro from the same crate that defines it .help = you can define integration tests in a directory named `tests` diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 689e713ef46dd..236ab1f09d35f 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -138,7 +138,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS, CRATE_NODE_ID, span_use, - BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def), + errors::MacroExpandedMacroExportsAccessedByAbsolutePaths { definition: span_def }, ); } diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 72be94e58db93..fb77b9ca1d31f 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -3,7 +3,7 @@ use rustc_errors::{ Applicability, Diag, ElidedLifetimeInPathSubdiag, EmissionGuarantee, IntoDiagArg, MultiSpan, Subdiagnostic, }; -use rustc_macros::{Diagnostic, Subdiagnostic}; +use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_span::{Ident, Span, Symbol}; use crate::late::PatternSource; @@ -566,6 +566,22 @@ pub(crate) struct ProcMacroSameCrate { pub(crate) is_test: bool, } +#[derive(LintDiagnostic)] +#[diag(resolve_proc_macro_derive_resolution_fallback)] +pub(crate) struct ProcMacroDeriveResolutionFallback { + #[label] + pub span: Span, + pub ns_descr: &'static str, + pub ident: Ident, +} + +#[derive(LintDiagnostic)] +#[diag(resolve_macro_expanded_macro_exports_accessed_by_absolute_paths)] +pub(crate) struct MacroExpandedMacroExportsAccessedByAbsolutePaths { + #[note] + pub definition: Span, +} + #[derive(Diagnostic)] #[diag(resolve_imported_crate)] pub(crate) struct CrateImported { diff --git a/compiler/rustc_resolve/src/ident.rs b/compiler/rustc_resolve/src/ident.rs index bc06a22757113..35051675fd8dc 100644 --- a/compiler/rustc_resolve/src/ident.rs +++ b/compiler/rustc_resolve/src/ident.rs @@ -4,7 +4,6 @@ use rustc_ast::{self as ast, NodeId}; use rustc_errors::ErrorGuaranteed; use rustc_hir::def::{DefKind, MacroKinds, Namespace, NonMacroAttrKind, PartialRes, PerNS}; use rustc_middle::bug; -use rustc_session::lint::BuiltinLintDiag; use rustc_session::lint::builtin::PROC_MACRO_DERIVE_RESOLUTION_FALLBACK; use rustc_session::parse::feature_err; use rustc_span::hygiene::{ExpnId, ExpnKind, LocalExpnId, MacroKind, SyntaxContext}; @@ -520,7 +519,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { PROC_MACRO_DERIVE_RESOLUTION_FALLBACK, lint_id, orig_ident.span, - BuiltinLintDiag::ProcMacroDeriveResolutionFallback { + errors::ProcMacroDeriveResolutionFallback { span: orig_ident.span, ns_descr: ns.descr(), ident, diff --git a/tests/ui/macros/issue-61053-duplicate-binder.stderr b/tests/ui/macros/issue-61053-duplicate-binder.stderr index 7c7cb26b4071e..1ecbc3f86d0de 100644 --- a/tests/ui/macros/issue-61053-duplicate-binder.stderr +++ b/tests/ui/macros/issue-61053-duplicate-binder.stderr @@ -2,9 +2,9 @@ error: duplicate matcher binding --> $DIR/issue-61053-duplicate-binder.rs:7:20 | LL | ($x:tt $x:tt) => { $x }; - | -- ^^ + | -- ^^ duplicate binding | | - | previous declaration + | previous binding | note: the lint level is defined here --> $DIR/issue-61053-duplicate-binder.rs:1:9 From 31c0d96cb604ead17afeb09062d10cc019de9560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 27 Aug 2025 14:11:32 +0200 Subject: [PATCH 2/5] Move more early buffered lints to dyn lint diagnostics (2/N) --- compiler/rustc_ast_passes/messages.ftl | 4 +++ .../rustc_ast_passes/src/ast_validation.rs | 2 +- compiler/rustc_ast_passes/src/errors.rs | 7 +++++ compiler/rustc_expand/messages.ftl | 4 +++ compiler/rustc_expand/src/errors.rs | 8 +++++ compiler/rustc_expand/src/expand.rs | 2 +- compiler/rustc_lint/messages.ftl | 14 --------- compiler/rustc_lint/src/early/diagnostics.rs | 13 -------- compiler/rustc_lint/src/lints.rs | 31 ------------------- compiler/rustc_lint_defs/src/lib.rs | 9 ------ compiler/rustc_resolve/messages.ftl | 7 +++++ compiler/rustc_resolve/src/check_unused.rs | 2 +- compiler/rustc_resolve/src/errors.rs | 16 ++++++++++ compiler/rustc_resolve/src/macros.rs | 2 +- 14 files changed, 50 insertions(+), 71 deletions(-) diff --git a/compiler/rustc_ast_passes/messages.ftl b/compiler/rustc_ast_passes/messages.ftl index 8dcf3e3aa3882..e5405a7ad910c 100644 --- a/compiler/rustc_ast_passes/messages.ftl +++ b/compiler/rustc_ast_passes/messages.ftl @@ -216,6 +216,10 @@ ast_passes_match_arm_with_no_body = .suggestion = add a body after the pattern ast_passes_missing_unsafe_on_extern = extern blocks must be unsafe + .suggestion = needs `unsafe` before the extern keyword + +ast_passes_missing_unsafe_on_extern_lint = extern blocks should be unsafe + .suggestion = needs `unsafe` before the extern keyword ast_passes_module_nonascii = trying to load file for module `{$name}` with non-ascii identifier name .help = consider using the `#[path]` attribute to specify filesystem path diff --git a/compiler/rustc_ast_passes/src/ast_validation.rs b/compiler/rustc_ast_passes/src/ast_validation.rs index a6ef89b553db4..dc221c2fb1abc 100644 --- a/compiler/rustc_ast_passes/src/ast_validation.rs +++ b/compiler/rustc_ast_passes/src/ast_validation.rs @@ -1131,7 +1131,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> { MISSING_UNSAFE_ON_EXTERN, item.id, item.span, - BuiltinLintDiag::MissingUnsafeOnExtern { + errors::MissingUnsafeOnExternLint { suggestion: item.span.shrink_to_lo(), }, ); diff --git a/compiler/rustc_ast_passes/src/errors.rs b/compiler/rustc_ast_passes/src/errors.rs index ae805042c549f..e09ca5b81c80c 100644 --- a/compiler/rustc_ast_passes/src/errors.rs +++ b/compiler/rustc_ast_passes/src/errors.rs @@ -523,6 +523,13 @@ pub(crate) struct MissingUnsafeOnExtern { pub span: Span, } +#[derive(LintDiagnostic)] +#[diag(ast_passes_missing_unsafe_on_extern_lint)] +pub(crate) struct MissingUnsafeOnExternLint { + #[suggestion(code = "unsafe ", applicability = "machine-applicable")] + pub suggestion: Span, +} + #[derive(Diagnostic)] #[diag(ast_passes_fieldless_union)] pub(crate) struct FieldlessUnion { diff --git a/compiler/rustc_expand/messages.ftl b/compiler/rustc_expand/messages.ftl index fc50ac6e3bfeb..f82ef96e9c75f 100644 --- a/compiler/rustc_expand/messages.ftl +++ b/compiler/rustc_expand/messages.ftl @@ -80,6 +80,10 @@ expand_macro_body_stability = .label = invalid body stability attribute .label2 = body stability attribute affects this macro +expand_macro_call_unused_doc_comment = unused doc comment + .label = rustdoc does not generate documentation for macro invocations + .help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion + expand_macro_const_stability = macros cannot have const stability attributes .label = invalid const stability attribute diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index a939dadaf2fcd..06c029482dfe6 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -537,3 +537,11 @@ pub(crate) struct MacroArgsBadDelimSugg { #[suggestion_part(code = ")")] pub close: Span, } + +#[derive(LintDiagnostic)] +#[diag(expand_macro_call_unused_doc_comment)] +#[help] +pub(crate) struct MacroCallUnusedDocComment { + #[label] + pub span: Span, +} diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index 38e057d2776e4..cda38cd7ae61b 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -2183,7 +2183,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { UNUSED_DOC_COMMENTS, current_span, self.cx.current_expansion.lint_node_id, - BuiltinLintDiag::UnusedDocComment(attr.span), + crate::errors::MacroCallUnusedDocComment { span: attr.span }, ); } else if rustc_attr_parsing::is_builtin_attr(attr) && !AttributeParser::::is_parsed_attribute(&attr.path()) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 5a60a4f48d9db..68bef1772c0e3 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -453,9 +453,6 @@ lint_int_to_ptr_transmutes = transmuting an integer to a pointer creates a point .suggestion_with_exposed_provenance = use `std::ptr::with_exposed_provenance{$suffix}` instead to use a previously exposed provenance .suggestion_without_provenance_mut = if you truly mean to create a pointer without provenance, use `std::ptr::without_provenance_mut` -lint_legacy_derive_helpers = derive helper attribute is used before it is introduced - .label = the attribute is introduced here - lint_lintpass_by_hand = implementing `LintPass` by hand .help = try using `declare_lint_pass!` or `impl_lint_pass!` instead @@ -524,9 +521,6 @@ lint_mismatched_lifetime_syntaxes_suggestion_mixed = lint_mismatched_lifetime_syntaxes_suggestion_mixed_only_paths = use `'_` for type paths -lint_missing_unsafe_on_extern = extern blocks should be unsafe - .suggestion = needs `unsafe` before the extern keyword - lint_mixed_script_confusables = the usage of Script Group `{$set}` in this crate consists solely of mixed script confusables .includes_note = the usage includes {$includes} @@ -962,14 +956,6 @@ lint_unused_def = unused {$pre}`{$def}`{$post} that must be used lint_unused_delim = unnecessary {$delim} around {$item} .suggestion = remove these {$delim} -lint_unused_doc_comment = unused doc comment - .label = rustdoc does not generate documentation for macro invocations - .help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion - -lint_unused_extern_crate = unused extern crate - .label = unused - .suggestion = remove the unused `extern crate` - lint_unused_import_braces = braces around {$node} is unnecessary lint_unused_imports = {$num_snippets -> diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index 4463586515ec1..eb5c89660e1ff 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -135,9 +135,6 @@ pub fn decorate_builtin_lint( stability::Deprecated { sub, kind: "macro".to_owned(), path, note, since_kind } .decorate_lint(diag); } - BuiltinLintDiag::UnusedDocComment(attr_span) => { - lints::UnusedDocComment { span: attr_span }.decorate_lint(diag); - } BuiltinLintDiag::PatternsInFnsWithoutBody { span: remove_span, ident, is_foreign } => { let sub = lints::PatternsInFnsWithoutBodySub { ident, span: remove_span }; if is_foreign { @@ -147,9 +144,6 @@ pub fn decorate_builtin_lint( } .decorate_lint(diag); } - BuiltinLintDiag::LegacyDeriveHelpers(label_span) => { - lints::LegacyDeriveHelpers { span: label_span }.decorate_lint(diag); - } BuiltinLintDiag::OrPatternsBackCompat(suggestion_span, suggestion) => { lints::OrPatternsBackCompat { span: suggestion_span, suggestion }.decorate_lint(diag); } @@ -210,9 +204,6 @@ pub fn decorate_builtin_lint( }; lints::DeprecatedWhereClauseLocation { suggestion }.decorate_lint(diag); } - BuiltinLintDiag::MissingUnsafeOnExtern { suggestion } => { - lints::MissingUnsafeOnExtern { suggestion }.decorate_lint(diag); - } BuiltinLintDiag::SingleUseLifetime { param_span, use_span: Some((use_span, elide)), @@ -242,7 +233,6 @@ pub fn decorate_builtin_lint( .decorate_lint(diag); } BuiltinLintDiag::SingleUseLifetime { use_span: None, deletion_span, ident, .. } => { - debug!(?deletion_span); lints::UnusedLifetime { deletion_span, ident }.decorate_lint(diag); } BuiltinLintDiag::NamedArgumentUsedPositionally { @@ -283,9 +273,6 @@ pub fn decorate_builtin_lint( BuiltinLintDiag::ByteSliceInPackedStructWithDerive { ty } => { lints::ByteSliceInPackedStructWithDerive { ty }.decorate_lint(diag); } - BuiltinLintDiag::UnusedExternCrate { span, removal_span } => { - lints::UnusedExternCrate { span, removal_span }.decorate_lint(diag); - } BuiltinLintDiag::ExternCrateNotIdiomatic { vis_span, ident_span } => { let suggestion_span = vis_span.between(ident_span); let code = if vis_span.is_empty() { "use " } else { " use " }; diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 1c13fadeef968..c084e8605f782 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2749,14 +2749,6 @@ pub(crate) enum RedundantImportSub { DefinedPrelude(#[primary_span] Span), } -#[derive(LintDiagnostic)] -#[diag(lint_unused_doc_comment)] -#[help] -pub(crate) struct UnusedDocComment { - #[label] - pub span: Span, -} - #[derive(LintDiagnostic)] pub(crate) enum PatternsInFnsWithoutBody { #[diag(lint_pattern_in_foreign)] @@ -2780,13 +2772,6 @@ pub(crate) struct PatternsInFnsWithoutBodySub { pub ident: Ident, } -#[derive(LintDiagnostic)] -#[diag(lint_legacy_derive_helpers)] -pub(crate) struct LegacyDeriveHelpers { - #[label] - pub span: Span, -} - #[derive(LintDiagnostic)] #[diag(lint_or_patterns_back_compat)] pub(crate) struct OrPatternsBackCompat { @@ -2878,13 +2863,6 @@ pub(crate) enum DeprecatedWhereClauseLocationSugg { }, } -#[derive(LintDiagnostic)] -#[diag(lint_missing_unsafe_on_extern)] -pub(crate) struct MissingUnsafeOnExtern { - #[suggestion(code = "unsafe ", applicability = "machine-applicable")] - pub suggestion: Span, -} - #[derive(LintDiagnostic)] #[diag(lint_single_use_lifetime)] pub(crate) struct SingleUseLifetime { @@ -2940,15 +2918,6 @@ pub(crate) struct ByteSliceInPackedStructWithDerive { pub ty: String, } -#[derive(LintDiagnostic)] -#[diag(lint_unused_extern_crate)] -pub(crate) struct UnusedExternCrate { - #[label] - pub span: Span, - #[suggestion(code = "", applicability = "machine-applicable", style = "verbose")] - pub removal_span: Span, -} - #[derive(LintDiagnostic)] #[diag(lint_extern_crate_not_idiomatic)] pub(crate) struct ExternCrateNotIdiomatic { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 77c17c6c1e39e..50c3bcf0434bc 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -640,7 +640,6 @@ pub enum BuiltinLintDiag { path: String, since_kind: DeprecatedSinceKind, }, - UnusedDocComment(Span), UnusedBuiltinAttribute { attr_name: Symbol, macro_name: String, @@ -652,7 +651,6 @@ pub enum BuiltinLintDiag { ident: Ident, is_foreign: bool, }, - LegacyDeriveHelpers(Span), OrPatternsBackCompat(Span, String), ReservedPrefix(Span, String), /// `'r#` in edition < 2021. @@ -668,9 +666,6 @@ pub enum BuiltinLintDiag { UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>), UnexpectedCfgValue((Symbol, Span), Option<(Symbol, Span)>), DeprecatedWhereclauseLocation(Span, Option<(Span, String)>), - MissingUnsafeOnExtern { - suggestion: Span, - }, SingleUseLifetime { /// Span of the parameter which declares this lifetime. param_span: Span, @@ -699,10 +694,6 @@ pub enum BuiltinLintDiag { // FIXME: enum of byte/string ty: String, }, - UnusedExternCrate { - span: Span, - removal_span: Span, - }, ExternCrateNotIdiomatic { vis_span: Span, ident_span: Span, diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 1c0e5ad65b3ab..32833f5e8d9b3 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -231,6 +231,9 @@ resolve_item_was_cfg_out = the item is gated here resolve_label_with_similar_name_reachable = a label with a similar name is reachable +resolve_legacy_derive_helpers = derive helper attribute is used before it is introduced + .label = the attribute is introduced here + resolve_lending_iterator_report_error = associated type `Iterator::Item` is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type .note = you can't create an `Iterator` that borrows each `Item` from itself, but you can instead create a new type that borrows your existing type and implement `Iterator` for that new type @@ -471,6 +474,10 @@ resolve_unreachable_label_suggestion_use_similarly_named = resolve_unreachable_label_with_similar_name_exists = a label with a similar name exists but is unreachable +resolve_unused_extern_crate = unused extern crate + .label = unused + .suggestion = remove the unused `extern crate` + resolve_variable_bound_with_different_mode = variable `{$variable_name}` is bound inconsistently across alternatives separated by `|` .label = bound in different ways diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index 50a1ad23a5462..a80cd7540afaf 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -169,7 +169,7 @@ impl<'a, 'ra, 'tcx> UnusedImportCheckVisitor<'a, 'ra, 'tcx> { UNUSED_EXTERN_CRATES, extern_crate.id, span, - BuiltinLintDiag::UnusedExternCrate { + crate::errors::UnusedExternCrate { span: extern_crate.span, removal_span: extern_crate.span_with_attributes, }, diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index fb77b9ca1d31f..6b36906c86532 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -1292,3 +1292,19 @@ pub(crate) struct TraitImplMismatch { #[label(resolve_trait_impl_mismatch_label_item)] pub(crate) trait_item_span: Span, } + +#[derive(LintDiagnostic)] +#[diag(resolve_legacy_derive_helpers)] +pub(crate) struct LegacyDeriveHelpers { + #[label] + pub span: Span, +} + +#[derive(LintDiagnostic)] +#[diag(resolve_unused_extern_crate)] +pub(crate) struct UnusedExternCrate { + #[label] + pub span: Span, + #[suggestion(code = "", applicability = "machine-applicable", style = "verbose")] + pub removal_span: Span, +} diff --git a/compiler/rustc_resolve/src/macros.rs b/compiler/rustc_resolve/src/macros.rs index 5fa87b4cd9b68..d3e98ef839b0b 100644 --- a/compiler/rustc_resolve/src/macros.rs +++ b/compiler/rustc_resolve/src/macros.rs @@ -979,7 +979,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { LEGACY_DERIVE_HELPERS, node_id, ident.span, - BuiltinLintDiag::LegacyDeriveHelpers(binding.span), + errors::LegacyDeriveHelpers { span: binding.span }, ); } } From 379b181fe698108d61ac05bad328e14567739304 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 27 Aug 2025 14:11:41 +0200 Subject: [PATCH 3/5] Remove a dead early lint Dropped in favor a hard error in RUST-127907. --- compiler/rustc_lint/messages.ftl | 3 --- compiler/rustc_lint/src/early/diagnostics.rs | 3 --- compiler/rustc_lint/src/lints.rs | 8 -------- compiler/rustc_lint_defs/src/lib.rs | 4 ---- 4 files changed, 18 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index 68bef1772c0e3..e348ae2c57458 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -181,9 +181,6 @@ lint_builtin_unused_doc_comment = unused doc comment lint_builtin_while_true = denote infinite loops with `loop {"{"} ... {"}"}` .suggestion = use `loop` -lint_byte_slice_in_packed_struct_with_derive = {$ty} slice in a packed struct that derives a built-in trait - .help = consider implementing the trait by hand, or remove the `packed` attribute - lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}` lint_closure_returning_async_block = closure returning async block can be made into an async closure diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index eb5c89660e1ff..4039888993860 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -270,9 +270,6 @@ pub fn decorate_builtin_lint( } .decorate_lint(diag); } - BuiltinLintDiag::ByteSliceInPackedStructWithDerive { ty } => { - lints::ByteSliceInPackedStructWithDerive { ty }.decorate_lint(diag); - } BuiltinLintDiag::ExternCrateNotIdiomatic { vis_span, ident_span } => { let suggestion_span = vis_span.between(ident_span); let code = if vis_span.is_empty() { "use " } else { " use " }; diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index c084e8605f782..64875ee4e8a1f 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2910,14 +2910,6 @@ pub(crate) struct NamedArgumentUsedPositionally { pub named_arg_name: String, } -#[derive(LintDiagnostic)] -#[diag(lint_byte_slice_in_packed_struct_with_derive)] -#[help] -pub(crate) struct ByteSliceInPackedStructWithDerive { - // FIXME: make this translatable - pub ty: String, -} - #[derive(LintDiagnostic)] #[diag(lint_extern_crate_not_idiomatic)] pub(crate) struct ExternCrateNotIdiomatic { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 50c3bcf0434bc..0b4b5015aca5e 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -690,10 +690,6 @@ pub enum BuiltinLintDiag { /// Indicates if the named argument is used as a width/precision for formatting is_formatting_arg: bool, }, - ByteSliceInPackedStructWithDerive { - // FIXME: enum of byte/string - ty: String, - }, ExternCrateNotIdiomatic { vis_span: Span, ident_span: Span, From 27a180a31a10bad8d5116146459a49a16b4f3866 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Wed, 27 Aug 2025 15:07:14 +0200 Subject: [PATCH 4/5] Move more early buffered lints to dyn lint diagnostics (3/N) --- compiler/rustc_lint/messages.ftl | 11 ---------- compiler/rustc_lint/src/early/diagnostics.rs | 8 -------- compiler/rustc_lint/src/lints.rs | 21 -------------------- compiler/rustc_lint_defs/src/lib.rs | 8 -------- compiler/rustc_resolve/messages.ftl | 11 ++++++++++ compiler/rustc_resolve/src/check_unused.rs | 4 ++-- compiler/rustc_resolve/src/errors.rs | 21 ++++++++++++++++++++ compiler/rustc_resolve/src/imports.rs | 6 +++--- compiler/rustc_resolve/src/late.rs | 4 ++-- 9 files changed, 39 insertions(+), 55 deletions(-) diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index e348ae2c57458..c11e59ca05882 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -460,10 +460,6 @@ lint_macro_is_private = macro `{$ident}` is private lint_macro_rule_never_used = rule #{$n} of macro `{$name}` is never used -lint_macro_use_deprecated = - applying the `#[macro_use]` attribute to an `extern crate` item is deprecated - .help = remove it and import macros at use sites with a `use` item instead - lint_malformed_attribute = malformed lint attribute input lint_map_unit_fn = `Iterator::map` call that discard the iterator's values @@ -726,9 +722,6 @@ lint_redundant_semicolons_suggestion = remove {$multiple_semicolons -> *[false] this semicolon } -lint_reexport_private_dependency = - {$kind} `{$name}` from private dependency '{$krate}' is re-exported - lint_remove_mut_from_pattern = remove `mut` from the parameter lint_removed_lint = lint `{$name}` has been removed: {$reason} @@ -966,15 +959,11 @@ lint_unused_imports = {$num_snippets -> } .help = if this is a test module, consider adding a `#[cfg(test)]` to the containing module -lint_unused_label = unused label - lint_unused_lifetime = lifetime parameter `{$ident}` never used .suggestion = elide the unused lifetime lint_unused_macro_definition = unused macro definition: `{$name}` -lint_unused_macro_use = unused `#[macro_use]` import - lint_unused_op = unused {$op} that must be used .label = the {$op} produces a value .suggestion = use `let _ = ...` to ignore the resulting value diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index 4039888993860..e8daf3e46b870 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -308,9 +308,6 @@ pub fn decorate_builtin_lint( } .decorate_lint(diag); } - BuiltinLintDiag::ReexportPrivateDependency { name, kind, krate } => { - lints::ReexportPrivateDependency { name, kind, krate }.decorate_lint(diag); - } BuiltinLintDiag::UnusedQualifications { removal_span } => { lints::UnusedQualifications { removal_span }.decorate_lint(diag); } @@ -350,15 +347,10 @@ pub fn decorate_builtin_lint( }); lints::UnknownDiagnosticAttribute { typo }.decorate_lint(diag); } - BuiltinLintDiag::MacroUseDeprecated => { - lints::MacroUseDeprecated.decorate_lint(diag); - } - BuiltinLintDiag::UnusedMacroUse => lints::UnusedMacroUse.decorate_lint(diag), BuiltinLintDiag::PrivateExternCrateReexport { source: ident, extern_crate_span } => { lints::PrivateExternCrateReexport { ident, sugg: extern_crate_span.shrink_to_lo() } .decorate_lint(diag); } - BuiltinLintDiag::UnusedLabel => lints::UnusedLabel.decorate_lint(diag), BuiltinLintDiag::MacroIsPrivate(ident) => { lints::MacroIsPrivate { ident }.decorate_lint(diag); } diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 64875ee4e8a1f..7c375a98f9e9d 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2535,15 +2535,6 @@ pub(crate) mod unexpected_cfg_value { } } -#[derive(LintDiagnostic)] -#[diag(lint_macro_use_deprecated)] -#[help] -pub(crate) struct MacroUseDeprecated; - -#[derive(LintDiagnostic)] -#[diag(lint_unused_macro_use)] -pub(crate) struct UnusedMacroUse; - #[derive(LintDiagnostic)] #[diag(lint_private_extern_crate_reexport, code = E0365)] pub(crate) struct PrivateExternCrateReexport { @@ -2552,10 +2543,6 @@ pub(crate) struct PrivateExternCrateReexport { pub sugg: Span, } -#[derive(LintDiagnostic)] -#[diag(lint_unused_label)] -pub(crate) struct UnusedLabel; - #[derive(LintDiagnostic)] #[diag(lint_macro_is_private)] pub(crate) struct MacroIsPrivate { @@ -2957,14 +2944,6 @@ pub(crate) struct HiddenGlobReexports { pub namespace: String, } -#[derive(LintDiagnostic)] -#[diag(lint_reexport_private_dependency)] -pub(crate) struct ReexportPrivateDependency { - pub name: String, - pub kind: String, - pub krate: Symbol, -} - #[derive(LintDiagnostic)] #[diag(lint_unnecessary_qualification)] pub(crate) struct UnusedQualifications { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 0b4b5015aca5e..239b03edfaf00 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -717,11 +717,6 @@ pub enum BuiltinLintDiag { /// The local binding that shadows the glob reexport. private_item_span: Span, }, - ReexportPrivateDependency { - name: String, - kind: String, - krate: Symbol, - }, UnusedQualifications { /// The span of the unnecessarily-qualified path to remove. removal_span: Span, @@ -744,13 +739,10 @@ pub enum BuiltinLintDiag { span: Span, typo_name: Option, }, - MacroUseDeprecated, - UnusedMacroUse, PrivateExternCrateReexport { source: Ident, extern_crate_span: Span, }, - UnusedLabel, MacroIsPrivate(Ident), UnusedMacroDefinition(Symbol), MacroRuleNeverUsed(usize, Symbol), diff --git a/compiler/rustc_resolve/messages.ftl b/compiler/rustc_resolve/messages.ftl index 32833f5e8d9b3..cc8fbc18b7377 100644 --- a/compiler/rustc_resolve/messages.ftl +++ b/compiler/rustc_resolve/messages.ftl @@ -274,6 +274,10 @@ resolve_macro_extern_deprecated = `#[macro_escape]` is a deprecated synonym for `#[macro_use]` .help = try an outer attribute: `#[macro_use]` +resolve_macro_use_deprecated = + applying the `#[macro_use]` attribute to an `extern crate` item is deprecated + .help = remove it and import macros at use sites with a `use` item instead + resolve_macro_use_extern_crate_self = `#[macro_use]` is not supported on `extern crate self` resolve_macro_use_name_already_in_use = @@ -357,6 +361,9 @@ resolve_reexport_of_crate_public = resolve_reexport_of_private = re-export of private `{$ident}` +resolve_reexport_private_dependency = + {$kind} `{$name}` from private dependency '{$krate}' is re-exported + resolve_relative_2018 = relative paths are not supported in visibilities in 2018 edition or later .suggestion = try @@ -478,6 +485,10 @@ resolve_unused_extern_crate = unused extern crate .label = unused .suggestion = remove the unused `extern crate` +resolve_unused_label = unused label + +resolve_unused_macro_use = unused `#[macro_use]` import + resolve_variable_bound_with_different_mode = variable `{$variable_name}` is bound inconsistently across alternatives separated by `|` .label = bound in different ways diff --git a/compiler/rustc_resolve/src/check_unused.rs b/compiler/rustc_resolve/src/check_unused.rs index a80cd7540afaf..95f979a3fed33 100644 --- a/compiler/rustc_resolve/src/check_unused.rs +++ b/compiler/rustc_resolve/src/check_unused.rs @@ -406,7 +406,7 @@ impl Resolver<'_, '_> { MACRO_USE_EXTERN_CRATE, import.root_id, import.span, - BuiltinLintDiag::MacroUseDeprecated, + crate::errors::MacroUseDeprecated, ); } } @@ -427,7 +427,7 @@ impl Resolver<'_, '_> { UNUSED_IMPORTS, import.root_id, import.span, - BuiltinLintDiag::UnusedMacroUse, + crate::errors::UnusedMacroUse, ); } _ => {} diff --git a/compiler/rustc_resolve/src/errors.rs b/compiler/rustc_resolve/src/errors.rs index 6b36906c86532..f0ea97ba8a0c0 100644 --- a/compiler/rustc_resolve/src/errors.rs +++ b/compiler/rustc_resolve/src/errors.rs @@ -1308,3 +1308,24 @@ pub(crate) struct UnusedExternCrate { #[suggestion(code = "", applicability = "machine-applicable", style = "verbose")] pub removal_span: Span, } + +#[derive(LintDiagnostic)] +#[diag(resolve_reexport_private_dependency)] +pub(crate) struct ReexportPrivateDependency { + pub name: Symbol, + pub kind: &'static str, + pub krate: Symbol, +} + +#[derive(LintDiagnostic)] +#[diag(resolve_unused_label)] +pub(crate) struct UnusedLabel; + +#[derive(LintDiagnostic)] +#[diag(resolve_unused_macro_use)] +pub(crate) struct UnusedMacroUse; + +#[derive(LintDiagnostic)] +#[diag(resolve_macro_use_deprecated)] +#[help] +pub(crate) struct MacroUseDeprecated; diff --git a/compiler/rustc_resolve/src/imports.rs b/compiler/rustc_resolve/src/imports.rs index d7fc028287ff5..33c2c7436d1b7 100644 --- a/compiler/rustc_resolve/src/imports.rs +++ b/compiler/rustc_resolve/src/imports.rs @@ -720,9 +720,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { EXPORTED_PRIVATE_DEPENDENCIES, binding_id, binding.span, - BuiltinLintDiag::ReexportPrivateDependency { - kind: binding.res().descr().to_string(), - name: key.ident.name.to_string(), + crate::errors::ReexportPrivateDependency { + name: key.ident.name, + kind: binding.res().descr(), krate: self.tcx.crate_name(reexported_def_id.krate), }, ); diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 521fef2b9d432..4d4acc60ca804 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -29,7 +29,7 @@ use rustc_middle::middle::resolve_bound_vars::Set1; use rustc_middle::ty::{DelegationFnSig, Visibility}; use rustc_middle::{bug, span_bug}; use rustc_session::config::{CrateType, ResolveDocLinks}; -use rustc_session::lint::{self, BuiltinLintDiag}; +use rustc_session::lint; use rustc_session::parse::feature_err; use rustc_span::source_map::{Spanned, respan}; use rustc_span::{BytePos, Ident, Span, Symbol, SyntaxContext, kw, sym}; @@ -5225,7 +5225,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> { lint::builtin::UNUSED_LABELS, *id, *span, - BuiltinLintDiag::UnusedLabel, + errors::UnusedLabel, ); } } From ec7ad59789138871418362d9592b1e848bab4053 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Sun, 14 Sep 2025 06:03:22 +0200 Subject: [PATCH 5/5] Move more early buffered lints to dyn lint diagnostics (4/N) --- compiler/rustc_expand/messages.ftl | 11 +++++ compiler/rustc_expand/src/errors.rs | 28 +++++++++++++ compiler/rustc_expand/src/expand.rs | 3 +- compiler/rustc_expand/src/mbe/macro_rules.rs | 5 +-- compiler/rustc_interface/messages.ftl | 3 ++ compiler/rustc_interface/src/errors.rs | 17 +++++++- compiler/rustc_interface/src/util.rs | 8 ++-- compiler/rustc_lint/messages.ftl | 14 ------- compiler/rustc_lint/src/early/diagnostics.rs | 19 --------- compiler/rustc_lint/src/lints.rs | 44 -------------------- compiler/rustc_lint_defs/src/lib.rs | 12 ------ 11 files changed, 66 insertions(+), 98 deletions(-) diff --git a/compiler/rustc_expand/messages.ftl b/compiler/rustc_expand/messages.ftl index f82ef96e9c75f..47c00bff5c950 100644 --- a/compiler/rustc_expand/messages.ftl +++ b/compiler/rustc_expand/messages.ftl @@ -160,6 +160,9 @@ expand_mve_unrecognized_var = expand_non_inline_modules_in_proc_macro_input_are_unstable = non-inline modules in proc macro input are unstable +expand_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro + .suggestion = use pat_param to preserve semantics + expand_proc_macro_back_compat = using an old version of `{$crate_name}` .note = older versions of the `{$crate_name}` crate no longer compile; please update to `{$crate_name}` v{$fixed_version}, or switch to one of the `{$crate_name}` alternatives @@ -189,8 +192,16 @@ expand_resolve_relative_path = expand_trace_macro = trace_macro +expand_trailing_semi_macro = trailing semicolon in macro used in expression position + .note1 = macro invocations at the end of a block are treated as expressions + .note2 = to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}` + expand_unknown_macro_variable = unknown macro variable `{$name}` +expand_unused_builtin_attribute = unused attribute `{$attr_name}` + .note = the built-in attribute `{$attr_name}` will be ignored, since it's applied to the macro invocation `{$macro_name}` + .suggestion = remove the attribute + expand_unsupported_key_value = key-value macro attributes are not supported diff --git a/compiler/rustc_expand/src/errors.rs b/compiler/rustc_expand/src/errors.rs index 06c029482dfe6..c37c2d88d9cd2 100644 --- a/compiler/rustc_expand/src/errors.rs +++ b/compiler/rustc_expand/src/errors.rs @@ -545,3 +545,31 @@ pub(crate) struct MacroCallUnusedDocComment { #[label] pub span: Span, } + +#[derive(LintDiagnostic)] +#[diag(expand_or_patterns_back_compat)] +pub(crate) struct OrPatternsBackCompat { + #[suggestion(code = "{suggestion}", applicability = "machine-applicable")] + pub span: Span, + pub suggestion: String, +} + +#[derive(LintDiagnostic)] +#[diag(expand_trailing_semi_macro)] +pub(crate) struct TrailingMacro { + #[note(expand_note1)] + #[note(expand_note2)] + pub is_trailing: bool, + pub name: Ident, +} + +#[derive(LintDiagnostic)] +#[diag(expand_unused_builtin_attribute)] +pub(crate) struct UnusedBuiltinAttribute { + #[note] + pub invoc_span: Span, + pub attr_name: Symbol, + pub macro_name: String, + #[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")] + pub attr_span: Span, +} diff --git a/compiler/rustc_expand/src/expand.rs b/compiler/rustc_expand/src/expand.rs index cda38cd7ae61b..4c0e0bbfe26d1 100644 --- a/compiler/rustc_expand/src/expand.rs +++ b/compiler/rustc_expand/src/expand.rs @@ -25,7 +25,6 @@ use rustc_parse::parser::{ token_descr, }; use rustc_session::Session; -use rustc_session::lint::BuiltinLintDiag; use rustc_session::lint::builtin::{UNUSED_ATTRIBUTES, UNUSED_DOC_COMMENTS}; use rustc_session::parse::feature_err; use rustc_span::hygiene::SyntaxContext; @@ -2196,7 +2195,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> { UNUSED_ATTRIBUTES, attr.span, self.cx.current_expansion.lint_node_id, - BuiltinLintDiag::UnusedBuiltinAttribute { + crate::errors::UnusedBuiltinAttribute { attr_name, macro_name: pprust::path_to_string(&call.path), invoc_span: call.path.span, diff --git a/compiler/rustc_expand/src/mbe/macro_rules.rs b/compiler/rustc_expand/src/mbe/macro_rules.rs index 8b43f852b2638..946265eba8bdb 100644 --- a/compiler/rustc_expand/src/mbe/macro_rules.rs +++ b/compiler/rustc_expand/src/mbe/macro_rules.rs @@ -17,7 +17,6 @@ use rustc_hir as hir; use rustc_hir::attrs::AttributeKind; use rustc_hir::def::MacroKinds; use rustc_hir::find_attr; -use rustc_lint_defs::BuiltinLintDiag; use rustc_lint_defs::builtin::{ RUST_2021_INCOMPATIBLE_OR_PATTERNS, SEMICOLON_IN_EXPRESSIONS_FROM_MACROS, }; @@ -90,7 +89,7 @@ impl<'a> ParserAnyMacro<'a> { SEMICOLON_IN_EXPRESSIONS_FROM_MACROS, parser.token.span, lint_node_id, - BuiltinLintDiag::TrailingMacro(is_trailing_mac, macro_ident), + errors::TrailingMacro { is_trailing: is_trailing_mac, name: macro_ident }, ); } parser.bump(); @@ -1425,7 +1424,7 @@ fn check_matcher_core<'tt>( RUST_2021_INCOMPATIBLE_OR_PATTERNS, span, ast::CRATE_NODE_ID, - BuiltinLintDiag::OrPatternsBackCompat(span, suggestion), + errors::OrPatternsBackCompat { span, suggestion }, ); } match is_in_follow(next_token, kind) { diff --git a/compiler/rustc_interface/messages.ftl b/compiler/rustc_interface/messages.ftl index d83b18cbc2df6..6f6666f8c76fa 100644 --- a/compiler/rustc_interface/messages.ftl +++ b/compiler/rustc_interface/messages.ftl @@ -30,6 +30,9 @@ interface_ignoring_out_dir = ignoring --out-dir flag due to -o flag interface_input_file_would_be_overwritten = the input file "{$path}" would be overwritten by the generated executable +interface_invalid_crate_type_value = invalid `crate_type` value + .suggestion = did you mean + interface_mixed_bin_crate = cannot mix `bin` crate type with others diff --git a/compiler/rustc_interface/src/errors.rs b/compiler/rustc_interface/src/errors.rs index d1082eaf6173c..727e09c7562b5 100644 --- a/compiler/rustc_interface/src/errors.rs +++ b/compiler/rustc_interface/src/errors.rs @@ -1,7 +1,7 @@ use std::io; use std::path::Path; -use rustc_macros::Diagnostic; +use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic}; use rustc_span::{Span, Symbol}; #[derive(Diagnostic)] @@ -108,3 +108,18 @@ pub(crate) struct AbiRequiredTargetFeature<'a> { pub feature: &'a str, pub enabled: &'a str, } + +#[derive(LintDiagnostic)] +#[diag(interface_invalid_crate_type_value)] +pub(crate) struct UnknownCrateTypes { + #[subdiagnostic] + pub sugg: Option, +} + +#[derive(Subdiagnostic)] +#[suggestion(interface_suggestion, code = r#""{snippet}""#, applicability = "maybe-incorrect")] +pub(crate) struct UnknownCrateTypesSub { + #[primary_span] + pub span: Span, + pub snippet: Symbol, +} diff --git a/compiler/rustc_interface/src/util.rs b/compiler/rustc_interface/src/util.rs index 04006f3e446c1..061c764e619a3 100644 --- a/compiler/rustc_interface/src/util.rs +++ b/compiler/rustc_interface/src/util.rs @@ -13,9 +13,8 @@ use rustc_errors::LintBuffer; use rustc_metadata::{DylibError, load_symbol_from_dylib}; use rustc_middle::ty::CurrentGcx; use rustc_session::config::{Cfg, OutFileName, OutputFilenames, OutputTypes, Sysroot, host_tuple}; -use rustc_session::lint::{self, BuiltinLintDiag}; use rustc_session::output::{CRATE_TYPES, categorize_crate_type}; -use rustc_session::{EarlyDiagCtxt, Session, filesearch}; +use rustc_session::{EarlyDiagCtxt, Session, filesearch, lint}; use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edition::Edition; use rustc_span::source_map::SourceMapInputs; @@ -468,7 +467,10 @@ pub(crate) fn check_attr_crate_type( lint::builtin::UNKNOWN_CRATE_TYPES, ast::CRATE_NODE_ID, span, - BuiltinLintDiag::UnknownCrateTypes { span, candidate }, + errors::UnknownCrateTypes { + sugg: candidate + .map(|cand| errors::UnknownCrateTypesSub { span, snippet: cand }), + }, ); } } else { diff --git a/compiler/rustc_lint/messages.ftl b/compiler/rustc_lint/messages.ftl index c11e59ca05882..5b98c9fafaad4 100644 --- a/compiler/rustc_lint/messages.ftl +++ b/compiler/rustc_lint/messages.ftl @@ -407,9 +407,6 @@ lint_invalid_asm_label_named = avoid using named labels in inline assembly .note = see the asm section of Rust By Example for more information lint_invalid_asm_label_no_span = the label may be declared in the expansion of a macro -lint_invalid_crate_type_value = invalid `crate_type` value - .suggestion = did you mean - # FIXME: we should ordinalize $valid_up_to when we add support for doing so lint_invalid_from_utf8_checked = calls to `{$method}` with an invalid literal always return an error .label = the literal was valid UTF-8 up to the {$valid_up_to} bytes @@ -637,9 +634,6 @@ lint_opaque_hidden_inferred_bound = opaque type `{$ty}` does not satisfy its ass lint_opaque_hidden_inferred_bound_sugg = add this bound -lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro - .suggestion = use pat_param to preserve semantics - lint_out_of_scope_macro_calls = cannot find macro `{$path}` in the current scope when looking from {$location} .label = not found from {$location} .help = import `macro_rules` with `use` to make it callable above its definition @@ -783,10 +777,6 @@ lint_symbol_intern_string_literal = using `Symbol::intern` on a string literal lint_too_large_char_cast = value exceeds maximum `char` value .note = maximum valid `char` value is `0x10FFFF` -lint_trailing_semi_macro = trailing semicolon in macro used in expression position - .note1 = macro invocations at the end of a block are treated as expressions - .note2 = to ignore the value produced by the macro, add a semicolon after the invocation of `{$name}` - lint_ty_qualified = usage of qualified `ty::{$ty}` .suggestion = try importing it and using it unqualified @@ -917,10 +907,6 @@ lint_untranslatable_diag = diagnostics should be created using translatable mess lint_unused_allocation = unnecessary allocation, use `&` instead lint_unused_allocation_mut = unnecessary allocation, use `&mut` instead -lint_unused_builtin_attribute = unused attribute `{$attr_name}` - .note = the built-in attribute `{$attr_name}` will be ignored, since it's applied to the macro invocation `{$macro_name}` - .suggestion = remove the attribute - lint_unused_closure = unused {$pre}{$count -> [one] closure diff --git a/compiler/rustc_lint/src/early/diagnostics.rs b/compiler/rustc_lint/src/early/diagnostics.rs index e8daf3e46b870..9029ee0447110 100644 --- a/compiler/rustc_lint/src/early/diagnostics.rs +++ b/compiler/rustc_lint/src/early/diagnostics.rs @@ -76,10 +76,6 @@ pub fn decorate_builtin_lint( } .decorate_lint(diag); } - BuiltinLintDiag::UnknownCrateTypes { span, candidate } => { - let sugg = candidate.map(|candidate| lints::UnknownCrateTypesSub { span, candidate }); - lints::UnknownCrateTypes { sugg }.decorate_lint(diag); - } BuiltinLintDiag::UnusedImports { remove_whole_use, num_to_remove, @@ -144,9 +140,6 @@ pub fn decorate_builtin_lint( } .decorate_lint(diag); } - BuiltinLintDiag::OrPatternsBackCompat(suggestion_span, suggestion) => { - lints::OrPatternsBackCompat { span: suggestion_span, suggestion }.decorate_lint(diag); - } BuiltinLintDiag::ReservedPrefix(label_span, prefix) => { lints::ReservedPrefix { label: label_span, @@ -166,18 +159,6 @@ pub fn decorate_builtin_lint( lints::ReservedMultihash { suggestion }.decorate_lint(diag); } } - BuiltinLintDiag::UnusedBuiltinAttribute { - attr_name, - macro_name, - invoc_span, - attr_span, - } => { - lints::UnusedBuiltinAttribute { invoc_span, attr_name, macro_name, attr_span } - .decorate_lint(diag); - } - BuiltinLintDiag::TrailingMacro(is_trailing, name) => { - lints::TrailingMacro { is_trailing, name }.decorate_lint(diag); - } BuiltinLintDiag::BreakWithLabelAndLoop(sugg_span) => { lints::BreakWithLabelAndLoop { sub: lints::BreakWithLabelAndLoopSub { diff --git a/compiler/rustc_lint/src/lints.rs b/compiler/rustc_lint/src/lints.rs index 7c375a98f9e9d..b7312484de5cd 100644 --- a/compiler/rustc_lint/src/lints.rs +++ b/compiler/rustc_lint/src/lints.rs @@ -2664,21 +2664,6 @@ pub(crate) struct ElidedLifetimesInPaths { pub subdiag: ElidedLifetimeInPathSubdiag, } -#[derive(LintDiagnostic)] -#[diag(lint_invalid_crate_type_value)] -pub(crate) struct UnknownCrateTypes { - #[subdiagnostic] - pub sugg: Option, -} - -#[derive(Subdiagnostic)] -#[suggestion(lint_suggestion, code = r#""{candidate}""#, applicability = "maybe-incorrect")] -pub(crate) struct UnknownCrateTypesSub { - #[primary_span] - pub span: Span, - pub candidate: Symbol, -} - #[derive(LintDiagnostic)] #[diag(lint_unused_imports)] pub(crate) struct UnusedImports { @@ -2759,14 +2744,6 @@ pub(crate) struct PatternsInFnsWithoutBodySub { pub ident: Ident, } -#[derive(LintDiagnostic)] -#[diag(lint_or_patterns_back_compat)] -pub(crate) struct OrPatternsBackCompat { - #[suggestion(code = "{suggestion}", applicability = "machine-applicable")] - pub span: Span, - pub suggestion: String, -} - #[derive(LintDiagnostic)] #[diag(lint_reserved_prefix)] pub(crate) struct ReservedPrefix { @@ -2787,27 +2764,6 @@ pub(crate) struct RawPrefix { pub suggestion: Span, } -#[derive(LintDiagnostic)] -#[diag(lint_unused_builtin_attribute)] -pub(crate) struct UnusedBuiltinAttribute { - #[note] - pub invoc_span: Span, - pub attr_name: Symbol, - pub macro_name: String, - #[suggestion(code = "", applicability = "machine-applicable", style = "tool-only")] - pub attr_span: Span, -} - -#[derive(LintDiagnostic)] -#[diag(lint_trailing_semi_macro)] -pub(crate) struct TrailingMacro { - #[note(lint_note1)] - #[note(lint_note2)] - pub is_trailing: bool, - - pub name: Ident, -} - #[derive(LintDiagnostic)] #[diag(lint_break_with_label_and_loop)] pub(crate) struct BreakWithLabelAndLoop { diff --git a/compiler/rustc_lint_defs/src/lib.rs b/compiler/rustc_lint_defs/src/lib.rs index 239b03edfaf00..40a818a3c9dc7 100644 --- a/compiler/rustc_lint_defs/src/lib.rs +++ b/compiler/rustc_lint_defs/src/lib.rs @@ -621,10 +621,6 @@ pub enum DeprecatedSinceKind { pub enum BuiltinLintDiag { AbsPathWithModule(Span), ElidedLifetimesInPaths(usize, Span, bool, Span), - UnknownCrateTypes { - span: Span, - candidate: Option, - }, UnusedImports { remove_whole_use: bool, num_to_remove: usize, @@ -640,18 +636,11 @@ pub enum BuiltinLintDiag { path: String, since_kind: DeprecatedSinceKind, }, - UnusedBuiltinAttribute { - attr_name: Symbol, - macro_name: String, - invoc_span: Span, - attr_span: Span, - }, PatternsInFnsWithoutBody { span: Span, ident: Ident, is_foreign: bool, }, - OrPatternsBackCompat(Span, String), ReservedPrefix(Span, String), /// `'r#` in edition < 2021. RawPrefix(Span), @@ -660,7 +649,6 @@ pub enum BuiltinLintDiag { is_string: bool, suggestion: Span, }, - TrailingMacro(bool, Ident), BreakWithLabelAndLoop(Span), UnicodeTextFlow(Span, String), UnexpectedCfgName((Symbol, Span), Option<(Symbol, Span)>),