Skip to content

Commit 1b20034

Browse files
committed
Move more early buffered lints to dyn lint diagnostics
1 parent d327d65 commit 1b20034

File tree

12 files changed

+80
-106
lines changed

12 files changed

+80
-106
lines changed

compiler/rustc_expand/messages.ftl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ expand_malformed_feature_attribute =
8989
malformed `feature` attribute input
9090
.expected = expected just one word
9191
92+
expand_metavariable_still_repeating = variable `{$name}` is still repeating at this depth
93+
.label = expected repetition
94+
95+
expand_metavariable_wrong_operator = meta-variable repeats with different Kleene operator
96+
.binder_label = expected repetition
97+
.occurrence_label = conflicting repetition
98+
9299
expand_meta_var_dif_seq_matchers = {$msg}
93100
94101
expand_missing_fragment_specifier = missing fragment specifier
@@ -176,6 +183,8 @@ expand_resolve_relative_path =
176183
177184
expand_trace_macro = trace_macro
178185
186+
expand_unknown_macro_variable = unknown macro variable `{$name}`
187+
179188
expand_unsupported_key_value =
180189
key-value macro attributes are not supported
181190

compiler/rustc_expand/src/errors.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::borrow::Cow;
22

33
use rustc_ast::ast;
44
use rustc_errors::codes::*;
5-
use rustc_macros::{Diagnostic, Subdiagnostic};
5+
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
66
use rustc_session::Limit;
77
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol};
88

@@ -35,6 +35,23 @@ pub(crate) struct VarStillRepeating {
3535
pub ident: MacroRulesNormalizedIdent,
3636
}
3737

38+
#[derive(LintDiagnostic)]
39+
#[diag(expand_metavariable_still_repeating)]
40+
pub(crate) struct MetaVariableStillRepeating {
41+
pub name: MacroRulesNormalizedIdent,
42+
#[label]
43+
pub label: Span,
44+
}
45+
46+
#[derive(LintDiagnostic)]
47+
#[diag(expand_metavariable_wrong_operator)]
48+
pub(crate) struct MetaVariableWrongOperator {
49+
#[label(expand_binder_label)]
50+
pub binder: Span,
51+
#[label(expand_occurrence_label)]
52+
pub occurrence: Span,
53+
}
54+
3855
#[derive(Diagnostic)]
3956
#[diag(expand_meta_var_dif_seq_matchers)]
4057
pub(crate) struct MetaVarsDifSeqMatchers {
@@ -43,6 +60,12 @@ pub(crate) struct MetaVarsDifSeqMatchers {
4360
pub msg: String,
4461
}
4562

63+
#[derive(LintDiagnostic)]
64+
#[diag(expand_unknown_macro_variable)]
65+
pub(crate) struct UnknownMacroVariable {
66+
pub name: MacroRulesNormalizedIdent,
67+
}
68+
4669
#[derive(Diagnostic)]
4770
#[diag(expand_resolve_relative_path)]
4871
pub(crate) struct ResolveRelativePath {
@@ -345,6 +368,15 @@ pub(crate) struct DuplicateMatcherBinding {
345368
pub prev: Span,
346369
}
347370

371+
#[derive(LintDiagnostic)]
372+
#[diag(expand_duplicate_matcher_binding)]
373+
pub(crate) struct DuplicateMatcherBindingLint {
374+
#[label]
375+
pub span: Span,
376+
#[label(expand_label2)]
377+
pub prev: Span,
378+
}
379+
348380
#[derive(Diagnostic)]
349381
#[diag(expand_missing_fragment_specifier)]
350382
#[note]

compiler/rustc_expand/src/mbe/macro_check.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,7 @@
108108
use rustc_ast::token::{Delimiter, IdentIsRaw, Token, TokenKind};
109109
use rustc_ast::{DUMMY_NODE_ID, NodeId};
110110
use rustc_data_structures::fx::FxHashMap;
111-
use rustc_errors::MultiSpan;
112-
use rustc_lint_defs::BuiltinLintDiag;
111+
use rustc_errors::DecorateDiagCompat;
113112
use rustc_session::lint::builtin::META_VARIABLE_MISUSE;
114113
use rustc_session::parse::ParseSess;
115114
use rustc_span::{ErrorGuaranteed, MacroRulesNormalizedIdent, Span, kw};
@@ -245,9 +244,7 @@ fn check_binders(
245244
// There are 3 possibilities:
246245
if let Some(prev_info) = binders.get(&name) {
247246
// 1. The meta-variable is already bound in the current LHS: This is an error.
248-
let mut span = MultiSpan::from_span(span);
249-
span.push_span_label(prev_info.span, "previous declaration");
250-
buffer_lint(psess, span, node_id, BuiltinLintDiag::DuplicateMatcherBinding);
247+
buffer_lint(psess, span, node_id, errors::DuplicateMatcherBindingLint { span, prev: prev_info.span});
251248
} else if get_binder_info(macros, binders, name).is_none() {
252249
// 2. The meta-variable is free: This is a binder.
253250
binders.insert(name, BinderInfo { span, ops: ops.into() });
@@ -579,7 +576,7 @@ fn check_ops_is_prefix(
579576
return;
580577
}
581578
}
582-
buffer_lint(psess, span.into(), node_id, BuiltinLintDiag::UnknownMacroVariable(name));
579+
buffer_lint(psess, span, node_id, errors::UnknownMacroVariable { name });
583580
}
584581

585582
/// Returns whether `binder_ops` is a prefix of `occurrence_ops`.
@@ -610,23 +607,21 @@ fn ops_is_prefix(
610607
) {
611608
for (i, binder) in binder_ops.iter().enumerate() {
612609
if i >= occurrence_ops.len() {
613-
let mut span = MultiSpan::from_span(span);
614-
span.push_span_label(binder.span, "expected repetition");
615-
buffer_lint(psess, span, node_id, BuiltinLintDiag::MetaVariableStillRepeating(name));
610+
buffer_lint(psess, span, node_id, errors::MetaVariableStillRepeating { name, label: binder.span });
616611
return;
617612
}
618613
let occurrence = &occurrence_ops[i];
619614
if occurrence.op != binder.op {
620-
let mut span = MultiSpan::from_span(span);
621-
span.push_span_label(binder.span, "expected repetition");
622-
span.push_span_label(occurrence.span, "conflicting repetition");
623-
buffer_lint(psess, span, node_id, BuiltinLintDiag::MetaVariableWrongOperator);
615+
buffer_lint(psess, span, node_id, errors::MetaVariableWrongOperator {
616+
binder: binder.span,
617+
occurrence: occurrence.span,
618+
});
624619
return;
625620
}
626621
}
627622
}
628623

629-
fn buffer_lint(psess: &ParseSess, span: MultiSpan, node_id: NodeId, diag: BuiltinLintDiag) {
624+
fn buffer_lint(psess: &ParseSess, span: Span, node_id: NodeId, diag: impl Into<DecorateDiagCompat>) {
630625
// Macros loaded from other crates have dummy node ids.
631626
if node_id != DUMMY_NODE_ID {
632627
psess.buffer_lint(META_VARIABLE_MISUSE, span, node_id, diag);

compiler/rustc_lint/messages.ftl

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ lint_dropping_references = calls to `std::mem::drop` with a reference instead of
252252
lint_duplicate_macro_attribute =
253253
duplicated attribute
254254
255-
lint_duplicate_matcher_binding = duplicate matcher binding
256-
257255
lint_enum_intrinsics_mem_discriminant =
258256
the return value of `mem::discriminant` is unspecified when called with a non-enum type
259257
.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
@@ -476,9 +474,6 @@ lint_legacy_derive_helpers = derive helper attribute is used before it is introd
476474
lint_lintpass_by_hand = implementing `LintPass` by hand
477475
.help = try using `declare_lint_pass!` or `impl_lint_pass!` instead
478476
479-
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
480-
.note = the macro is defined here
481-
482477
lint_macro_expr_fragment_specifier_2024_migration =
483478
the `expr` fragment specifier will accept more expressions in the 2024 edition
484479
.suggestion = to keep the existing behavior, use the `expr_2021` fragment specifier
@@ -499,10 +494,6 @@ lint_map_unit_fn = `Iterator::map` call that discard the iterator's values
499494
.map_label = after this call to map, the resulting iterator is `impl Iterator<Item = ()>`, which means the only information carried by the iterator is the number of items
500495
.suggestion = you might have meant to use `Iterator::for_each`
501496
502-
lint_metavariable_still_repeating = variable `{$name}` is still repeating at this depth
503-
504-
lint_metavariable_wrong_operator = meta-variable repeats with different Kleene operator
505-
506497
lint_mismatched_lifetime_syntaxes_eliding_while_named =
507498
eliding a lifetime that's named elsewhere is confusing
508499
@@ -719,9 +710,6 @@ lint_pattern_in_foreign = patterns aren't allowed in foreign function declaratio
719710
lint_private_extern_crate_reexport = extern crate `{$ident}` is private and cannot be re-exported
720711
.suggestion = consider making the `extern crate` item publicly accessible
721712
722-
lint_proc_macro_derive_resolution_fallback = cannot find {$ns_descr} `{$ident}` in this scope
723-
.label = names from parent modules are not accessible without an explicit import
724-
725713
lint_query_instability = using `{$query}` can result in unstable query results
726714
.note = if you believe this case to be fine, allow this lint and add a comment explaining your rationale
727715
@@ -935,8 +923,6 @@ lint_unknown_lint =
935923
*[false] did you mean: `{$replace}`
936924
}
937925
938-
lint_unknown_macro_variable = unknown macro variable `{$name}`
939-
940926
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
941927
.help = add `#![register_tool({$tool_name})]` to the crate root
942928

compiler/rustc_lint/src/early/diagnostics.rs

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,6 @@ pub fn decorate_builtin_lint(
6464
}
6565
.decorate_lint(diag);
6666
}
67-
BuiltinLintDiag::ProcMacroDeriveResolutionFallback {
68-
span: macro_span,
69-
ns_descr,
70-
ident,
71-
} => lints::ProcMacroDeriveResolutionFallback { span: macro_span, ns_descr, ident }
72-
.decorate_lint(diag),
73-
BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def) => {
74-
lints::MacroExpandedMacroExportsAccessedByAbsolutePaths { definition: span_def }
75-
.decorate_lint(diag)
76-
}
77-
7867
BuiltinLintDiag::ElidedLifetimesInPaths(n, path_span, incl_angl_brckt, insertion_span) => {
7968
lints::ElidedLifetimesInPaths {
8069
subdiag: elided_lifetime_in_path_suggestion(
@@ -416,18 +405,6 @@ pub fn decorate_builtin_lint(
416405
BuiltinLintDiag::CfgAttrNoAttributes => {
417406
lints::CfgAttrNoAttributes.decorate_lint(diag);
418407
}
419-
BuiltinLintDiag::MetaVariableStillRepeating(name) => {
420-
lints::MetaVariableStillRepeating { name }.decorate_lint(diag);
421-
}
422-
BuiltinLintDiag::MetaVariableWrongOperator => {
423-
lints::MetaVariableWrongOperator.decorate_lint(diag);
424-
}
425-
BuiltinLintDiag::DuplicateMatcherBinding => {
426-
lints::DuplicateMatcherBinding.decorate_lint(diag);
427-
}
428-
BuiltinLintDiag::UnknownMacroVariable(name) => {
429-
lints::UnknownMacroVariable { name }.decorate_lint(diag);
430-
}
431408
BuiltinLintDiag::UnusedCrateDependency { extern_crate, local_crate } => {
432409
lints::UnusedCrateDependency { extern_crate, local_crate }.decorate_lint(diag)
433410
}

compiler/rustc_lint/src/lints.rs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::{Clause, PolyExistentialTraitRef, Ty, TyCtxt};
1717
use rustc_session::Session;
1818
use rustc_session::lint::AmbiguityErrorDiag;
1919
use rustc_span::edition::Edition;
20-
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
20+
use rustc_span::{Ident, Span, Symbol, sym};
2121

2222
use crate::builtin::{InitError, ShorthandAssocTyCollector, TypeAliasBounds};
2323
use crate::errors::{OverruledAttributeSub, RequestedLevel};
@@ -2609,26 +2609,6 @@ pub(crate) struct DuplicateMacroAttribute;
26092609
#[diag(lint_cfg_attr_no_attributes)]
26102610
pub(crate) struct CfgAttrNoAttributes;
26112611

2612-
#[derive(LintDiagnostic)]
2613-
#[diag(lint_metavariable_still_repeating)]
2614-
pub(crate) struct MetaVariableStillRepeating {
2615-
pub name: MacroRulesNormalizedIdent,
2616-
}
2617-
2618-
#[derive(LintDiagnostic)]
2619-
#[diag(lint_metavariable_wrong_operator)]
2620-
pub(crate) struct MetaVariableWrongOperator;
2621-
2622-
#[derive(LintDiagnostic)]
2623-
#[diag(lint_duplicate_matcher_binding)]
2624-
pub(crate) struct DuplicateMatcherBinding;
2625-
2626-
#[derive(LintDiagnostic)]
2627-
#[diag(lint_unknown_macro_variable)]
2628-
pub(crate) struct UnknownMacroVariable {
2629-
pub name: MacroRulesNormalizedIdent,
2630-
}
2631-
26322612
#[derive(LintDiagnostic)]
26332613
#[diag(lint_unused_crate_dependency)]
26342614
#[help]
@@ -2714,22 +2694,6 @@ pub(crate) struct AbsPathWithModuleSugg {
27142694
pub replacement: String,
27152695
}
27162696

2717-
#[derive(LintDiagnostic)]
2718-
#[diag(lint_proc_macro_derive_resolution_fallback)]
2719-
pub(crate) struct ProcMacroDeriveResolutionFallback {
2720-
#[label]
2721-
pub span: Span,
2722-
pub ns_descr: &'static str,
2723-
pub ident: Ident,
2724-
}
2725-
2726-
#[derive(LintDiagnostic)]
2727-
#[diag(lint_macro_expanded_macro_exports_accessed_by_absolute_paths)]
2728-
pub(crate) struct MacroExpandedMacroExportsAccessedByAbsolutePaths {
2729-
#[note]
2730-
pub definition: Span,
2731-
}
2732-
27332697
#[derive(LintDiagnostic)]
27342698
#[diag(lint_hidden_lifetime_parameters)]
27352699
pub(crate) struct ElidedLifetimesInPaths {

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_hir_id::{HashStableContext, HirId, ItemLocalId};
1111
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1212
use rustc_span::def_id::DefPathHash;
1313
pub use rustc_span::edition::Edition;
14-
use rustc_span::{Ident, MacroRulesNormalizedIdent, Span, Symbol, sym};
14+
use rustc_span::{Ident, Span, Symbol, sym};
1515
use serde::{Deserialize, Serialize};
1616

1717
pub use self::Level::*;
@@ -620,12 +620,6 @@ pub enum DeprecatedSinceKind {
620620
#[derive(Debug)]
621621
pub enum BuiltinLintDiag {
622622
AbsPathWithModule(Span),
623-
ProcMacroDeriveResolutionFallback {
624-
span: Span,
625-
ns_descr: &'static str,
626-
ident: Ident,
627-
},
628-
MacroExpandedMacroExportsAccessedByAbsolutePaths(Span),
629623
ElidedLifetimesInPaths(usize, Span, bool, Span),
630624
UnknownCrateTypes {
631625
span: Span,
@@ -780,10 +774,6 @@ pub enum BuiltinLintDiag {
780774
UnnameableTestItems,
781775
DuplicateMacroAttribute,
782776
CfgAttrNoAttributes,
783-
MetaVariableStillRepeating(MacroRulesNormalizedIdent),
784-
MetaVariableWrongOperator,
785-
DuplicateMatcherBinding,
786-
UnknownMacroVariable(MacroRulesNormalizedIdent),
787777
UnusedCrateDependency {
788778
extern_crate: Symbol,
789779
local_crate: Symbol,

compiler/rustc_resolve/messages.ftl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ resolve_macro_defined_later =
260260
resolve_macro_expanded_extern_crate_cannot_shadow_extern_arguments =
261261
macro-expanded `extern crate` items cannot shadow names passed with `--extern`
262262
263+
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
264+
.note = the macro is defined here
265+
263266
resolve_macro_expected_found =
264267
expected {$expected}, found {$found} `{$macro_path}`
265268
.label = not {$article} {$expected}
@@ -339,6 +342,9 @@ resolve_param_in_ty_of_const_param =
339342
340343
resolve_pattern_doesnt_bind_name = pattern doesn't bind `{$name}`
341344
345+
resolve_proc_macro_derive_resolution_fallback = cannot find {$ns_descr} `{$ident}` in this scope
346+
.label = names from parent modules are not accessible without an explicit import
347+
342348
resolve_proc_macro_same_crate = can't use a procedural macro from the same crate that defines it
343349
.help = you can define integration tests in a directory named `tests`
344350

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
137137
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
138138
CRATE_NODE_ID,
139139
span_use,
140-
BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
140+
errors::MacroExpandedMacroExportsAccessedByAbsolutePaths { definition: span_def },
141141
);
142142
}
143143

compiler/rustc_resolve/src/errors.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_errors::{
33
Applicability, Diag, ElidedLifetimeInPathSubdiag, EmissionGuarantee, IntoDiagArg, MultiSpan,
44
Subdiagnostic,
55
};
6-
use rustc_macros::{Diagnostic, Subdiagnostic};
6+
use rustc_macros::{Diagnostic, Subdiagnostic, LintDiagnostic};
77
use rustc_span::{Ident, Span, Symbol};
88

99
use crate::late::PatternSource;
@@ -566,6 +566,22 @@ pub(crate) struct ProcMacroSameCrate {
566566
pub(crate) is_test: bool,
567567
}
568568

569+
#[derive(LintDiagnostic)]
570+
#[diag(resolve_proc_macro_derive_resolution_fallback)]
571+
pub(crate) struct ProcMacroDeriveResolutionFallback {
572+
#[label]
573+
pub span: Span,
574+
pub ns_descr: &'static str,
575+
pub ident: Ident,
576+
}
577+
578+
#[derive(LintDiagnostic)]
579+
#[diag(resolve_macro_expanded_macro_exports_accessed_by_absolute_paths)]
580+
pub(crate) struct MacroExpandedMacroExportsAccessedByAbsolutePaths {
581+
#[note]
582+
pub definition: Span,
583+
}
584+
569585
#[derive(Diagnostic)]
570586
#[diag(resolve_imported_crate)]
571587
pub(crate) struct CrateImported {

0 commit comments

Comments
 (0)