Skip to content

Commit 99f8205

Browse files
committed
fixup! Convert early lints to diag structs
1 parent 6c04cb2 commit 99f8205

File tree

5 files changed

+174
-78
lines changed

5 files changed

+174
-78
lines changed

compiler/rustc_expand/src/base.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1555,14 +1555,14 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) -> bool {
15551555
// FIXME: make this translatable
15561556
#[allow(rustc::untranslatable_diagnostic)]
15571557
sess.psess.buffer_lint_with_diagnostic(
1558-
PROC_MACRO_BACK_COMPAT,
1559-
item.ident.span,
1560-
ast::CRATE_NODE_ID,
1561-
BuiltinLintDiag::ProcMacroBackCompat(
1562-
"older versions of the `rental` crate will stop compiling in future versions of Rust; \
1563-
please update to `rental` v0.5.6, or switch to one of the `rental` alternatives".to_string()
1564-
)
1565-
);
1558+
PROC_MACRO_BACK_COMPAT,
1559+
item.ident.span,
1560+
ast::CRATE_NODE_ID,
1561+
BuiltinLintDiag::ProcMacroBackCompat {
1562+
crate_name: "rental".to_string(),
1563+
fixed_version: "0.5.6".to_string(),
1564+
},
1565+
);
15661566
return true;
15671567
}
15681568
}

compiler/rustc_lint/messages.ftl

+16-5
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ lint_expectation = this lint expectation is unfulfilled
254254
lint_extern_crate_not_idiomatic = `extern crate` is not idiomatic in the new edition
255255
256256
lint_extern_without_abi = extern declarations without an explicit ABI are deprecated
257+
.label = ABI should be specified here
258+
.help = the default ABI is {$default_abi}
257259
258260
lint_for_loops_over_fallibles =
259261
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
@@ -414,8 +416,8 @@ lint_invalid_reference_casting_note_book = for more information, visit <https://
414416
415417
lint_invalid_reference_casting_note_ty_has_interior_mutability = even for types with interior mutability, the only legal way to obtain a mutable pointer from a shared reference is through `UnsafeCell::get`
416418
417-
lint_legacy_derive_helpers =
418-
derive helper attribute is used before it is introduced
419+
lint_legacy_derive_helpers = derive helper attribute is used before it is introduced
420+
.label = the attribute is introduced here
419421
420422
lint_lintpass_by_hand = implementing `LintPass` by hand
421423
.help = try using `declare_lint_pass!` or `impl_lint_pass!` instead
@@ -567,8 +569,8 @@ lint_opaque_hidden_inferred_bound = opaque type `{$ty}` does not satisfy its ass
567569
568570
lint_opaque_hidden_inferred_bound_sugg = add this bound
569571
570-
lint_or_patterns_back_compat =
571-
the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
572+
lint_or_patterns_back_compat = the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro
573+
.suggestion = use pat_param to preserve semantics
572574
573575
lint_overflowing_bin_hex = literal out of range for `{$ty}`
574576
.negative_note = the literal `{$lit}` (decimal `{$dec}`) does not fit into the type `{$ty}`
@@ -608,7 +610,8 @@ lint_pattern_in_foreign = patterns aren't allowed in foreign function declaratio
608610
lint_private_extern_crate_reexport =
609611
extern crate `{$ident}` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
610612
611-
lint_proc_macro_back_compat_rental = using an old version of `rental`
613+
lint_proc_macro_back_compat = using an old version of `{$crate_name}`
614+
.note = older versions of the `{$crate_name}` crate will stop compiling in future versions of Rust; please update to `{$crate_name}` v{$fixed_version}, or switch to one of the `{$crate_name}` alternatives
612615
613616
lint_proc_macro_derive_resolution_fallback = cannot find {$ns} `{$ident}` in this scope
614617
.label = names from parent modules are not accessible without an explicit import
@@ -652,6 +655,8 @@ lint_redundant_semicolons =
652655
653656
lint_reference_without_lifetime_not_allowed = `&` without an explicit lifetime name cannot be used here
654657
658+
lint_remove_mut_from_pattern = remove `mut` from the parameter
659+
655660
lint_removed_lint = lint `{$name}` has been removed: {$reason}
656661
657662
lint_renamed_lint = lint `{$name}` has been renamed to `{$replace}`
@@ -660,6 +665,10 @@ lint_renamed_lint = lint `{$name}` has been renamed to `{$replace}`
660665
661666
lint_requested_level = requested on the command line with `{$level} {$lint_name}`
662667
668+
lint_reserved_prefix = prefix `{$prefix}` is unknown
669+
.label = unknown prefix
670+
.suggestion = insert whitespace here to avoid this being parsed as a prefix in Rust 2021
671+
663672
lint_span_use_eq_ctxt = use `.eq_ctxt()` instead of `.ctxt() == .ctxt()`
664673
665674
lint_supertrait_as_deref_target = this `Deref` implementation is covered by an implicit supertrait coercion
@@ -763,6 +772,8 @@ lint_unused_delim = unnecessary {$delim} around {$item}
763772
.suggestion = remove these {$delim}
764773
765774
lint_unused_doc_comment = unused doc comment
775+
.label = rustdoc does not generate documentation for macro invocations
776+
.help = to document an item produced by a macro, the macro must produce the documentation as part of its expansion
766777
767778
lint_unused_import_braces = braces around {$node} is unnecessary
768779

compiler/rustc_lint/src/context/diagnostics.rs

+70-63
Original file line numberDiff line numberDiff line change
@@ -19,46 +19,6 @@ mod check_cfg;
1919

2020
fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Diag<'_, ()>) {
2121
match diagnostic {
22-
BuiltinLintDiag::UnusedDocComment(span) => {
23-
diag.span_label(span, "rustdoc does not generate documentation for macro invocations");
24-
diag.help("to document an item produced by a macro, \
25-
the macro must produce the documentation as part of its expansion");
26-
}
27-
BuiltinLintDiag::PatternsInFnsWithoutBody { span, ident, .. } => {
28-
diag.span_suggestion(
29-
span,
30-
"remove `mut` from the parameter",
31-
ident,
32-
Applicability::MachineApplicable,
33-
);
34-
}
35-
BuiltinLintDiag::MissingAbi(span, default_abi) => {
36-
diag.span_label(span, "ABI should be specified here");
37-
diag.help(format!("the default ABI is {}", default_abi.name()));
38-
}
39-
BuiltinLintDiag::LegacyDeriveHelpers(span) => {
40-
diag.span_label(span, "the attribute is introduced here");
41-
}
42-
BuiltinLintDiag::ProcMacroBackCompat(note) => {
43-
diag.note(note);
44-
}
45-
BuiltinLintDiag::OrPatternsBackCompat(span, suggestion) => {
46-
diag.span_suggestion(
47-
span,
48-
"use pat_param to preserve semantics",
49-
suggestion,
50-
Applicability::MachineApplicable,
51-
);
52-
}
53-
BuiltinLintDiag::ReservedPrefix(span, _) => {
54-
diag.span_label(span, "unknown prefix");
55-
diag.span_suggestion_verbose(
56-
span.shrink_to_hi(),
57-
"insert whitespace here to avoid this being parsed as a prefix in Rust 2021",
58-
" ",
59-
Applicability::MachineApplicable,
60-
);
61-
}
6222
BuiltinLintDiag::UnusedBuiltinAttribute { attr_name, macro_name, invoc_span } => {
6323
diag.span_note(
6424
invoc_span,
@@ -257,6 +217,14 @@ fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Dia
257217
| BuiltinLintDiag::UnusedImports { .. }
258218
| BuiltinLintDiag::RedundantImport(_, _)
259219
| BuiltinLintDiag::DeprecatedMacro { .. }
220+
| BuiltinLintDiag::UnusedDocComment(_)
221+
| BuiltinLintDiag::PatternsInFnsWithoutBody { .. }
222+
| BuiltinLintDiag::MissingAbi(_, _)
223+
| BuiltinLintDiag::LegacyDeriveHelpers(_)
224+
| BuiltinLintDiag::ProcMacroBackCompat{..}
225+
| BuiltinLintDiag::OrPatternsBackCompat(_, _)
226+
| BuiltinLintDiag::ReservedPrefix(_, _)
227+
// ================== //
260228
| BuiltinLintDiag::MacroUseDeprecated
261229
| BuiltinLintDiag::UnusedMacroUse
262230
| BuiltinLintDiag::PrivateExternCrateReexport(_)
@@ -288,24 +256,9 @@ fn buffered_decorate(sess: &Session, diagnostic: BuiltinLintDiag, diag: &mut Dia
288256

289257
fn buffered_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
290258
match diagnostic {
291-
BuiltinLintDiag::MissingAbi(_, _) => fluent::lint_extern_without_abi,
292-
BuiltinLintDiag::UnusedDocComment(_) => fluent::lint_unused_doc_comment,
293259
BuiltinLintDiag::UnusedBuiltinAttribute { attr_name, .. } => {
294260
format!("unused attribute `{attr_name}`").into()
295261
}
296-
BuiltinLintDiag::PatternsInFnsWithoutBody { is_foreign, .. } => {
297-
if *is_foreign {
298-
fluent::lint_pattern_in_foreign
299-
} else {
300-
fluent::lint_pattern_in_bodiless
301-
}
302-
}
303-
BuiltinLintDiag::LegacyDeriveHelpers(_) => fluent::lint_legacy_derive_helpers,
304-
BuiltinLintDiag::ProcMacroBackCompat(_) => fluent::lint_proc_macro_back_compat_rental,
305-
BuiltinLintDiag::OrPatternsBackCompat(_, _) => fluent::lint_or_patterns_back_compat,
306-
BuiltinLintDiag::ReservedPrefix(_, prefix) => {
307-
format!("prefix `{prefix}` is unknown").into()
308-
}
309262
BuiltinLintDiag::TrailingMacro(_, _) => fluent::lint_trailing_semi_macro,
310263
BuiltinLintDiag::BreakWithLabelAndLoop(_) => fluent::lint_break_with_label_and_loop,
311264
BuiltinLintDiag::UnexpectedCfgName((name, _), _) => {
@@ -362,6 +315,14 @@ fn buffered_message(diagnostic: &BuiltinLintDiag) -> DiagMessage {
362315
| BuiltinLintDiag::UnusedImports { .. }
363316
| BuiltinLintDiag::RedundantImport(_, _)
364317
| BuiltinLintDiag::DeprecatedMacro { .. }
318+
| BuiltinLintDiag::UnusedDocComment(_)
319+
| BuiltinLintDiag::PatternsInFnsWithoutBody { .. }
320+
| BuiltinLintDiag::MissingAbi(_, _)
321+
| BuiltinLintDiag::LegacyDeriveHelpers(_)
322+
| BuiltinLintDiag::ProcMacroBackCompat{..}
323+
| BuiltinLintDiag::OrPatternsBackCompat(_, _)
324+
| BuiltinLintDiag::ReservedPrefix(_, _)
325+
// ================== //
365326
| BuiltinLintDiag::MacroUseDeprecated
366327
| BuiltinLintDiag::UnusedMacroUse
367328
| BuiltinLintDiag::PrivateExternCrateReexport(_)
@@ -398,14 +359,7 @@ pub(super) fn emit_buffered_lint<C: LintContext + ?Sized>(
398359
diagnostic: BuiltinLintDiag,
399360
) {
400361
match diagnostic {
401-
BuiltinLintDiag::MissingAbi(_, _)
402-
| BuiltinLintDiag::UnusedDocComment(_)
403-
| BuiltinLintDiag::UnusedBuiltinAttribute { .. }
404-
| BuiltinLintDiag::PatternsInFnsWithoutBody { .. }
405-
| BuiltinLintDiag::LegacyDeriveHelpers(_)
406-
| BuiltinLintDiag::ProcMacroBackCompat(_)
407-
| BuiltinLintDiag::OrPatternsBackCompat(_, _)
408-
| BuiltinLintDiag::ReservedPrefix(_, _)
362+
BuiltinLintDiag::UnusedBuiltinAttribute { .. }
409363
| BuiltinLintDiag::TrailingMacro(_, _)
410364
| BuiltinLintDiag::BreakWithLabelAndLoop(_)
411365
| BuiltinLintDiag::UnexpectedCfgName(_, _)
@@ -569,6 +523,59 @@ pub(super) fn emit_buffered_lint<C: LintContext + ?Sized>(
569523
stability::Deprecated { sub, kind: "macro".to_string(), path, note, since_kind },
570524
);
571525
}
526+
BuiltinLintDiag::UnusedDocComment(attr_span) => {
527+
ctx.emit_span_lint(lint, span, lints::UnusedDocComment { span: attr_span });
528+
}
529+
BuiltinLintDiag::PatternsInFnsWithoutBody { span: remove_span, ident, is_foreign } => {
530+
let sub = lints::PatternsInFnsWithoutBodySub { ident, span: remove_span };
531+
532+
ctx.emit_span_lint(
533+
lint,
534+
span,
535+
if is_foreign {
536+
lints::PatternsInFnsWithoutBody::Foreign { sub }
537+
} else {
538+
lints::PatternsInFnsWithoutBody::Bodiless { sub }
539+
},
540+
);
541+
}
542+
BuiltinLintDiag::MissingAbi(label_span, default_abi) => {
543+
ctx.emit_span_lint(
544+
lint,
545+
span,
546+
lints::MissingAbi { span: label_span, default_abi: default_abi.name() },
547+
);
548+
}
549+
BuiltinLintDiag::LegacyDeriveHelpers(label_span) => {
550+
ctx.emit_span_lint(lint, span, lints::LegacyDeriveHelpers { span: label_span });
551+
}
552+
BuiltinLintDiag::ProcMacroBackCompat { crate_name, fixed_version } => {
553+
ctx.emit_span_lint(
554+
lint,
555+
span,
556+
lints::ProcMacroBackCompat { crate_name, fixed_version },
557+
);
558+
}
559+
BuiltinLintDiag::OrPatternsBackCompat(suggestion_span, suggestion) => {
560+
ctx.emit_span_lint(
561+
lint,
562+
span,
563+
lints::OrPatternsBackCompat { span: suggestion_span, suggestion },
564+
);
565+
}
566+
BuiltinLintDiag::ReservedPrefix(label_span, prefix) => {
567+
ctx.emit_span_lint(
568+
lint,
569+
span,
570+
lints::ReservedPrefix {
571+
label: label_span,
572+
suggestion: label_span.shrink_to_hi(),
573+
prefix,
574+
},
575+
);
576+
}
577+
578+
// ================== //
572579
BuiltinLintDiag::MacroUseDeprecated => {
573580
ctx.emit_span_lint(lint, span, lints::MacroUseDeprecated)
574581
}

compiler/rustc_lint/src/lints.rs

+76-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_errors::{
99
ElidedLifetimeInPathSubdiag, EmissionGuarantee, LintDiagnostic, SubdiagMessageOp,
1010
Subdiagnostic, SuggestionStyle,
1111
};
12-
use rustc_hir::def_id::DefId;
12+
use rustc_hir::{def::Namespace, def_id::DefId};
1313
use rustc_macros::{LintDiagnostic, Subdiagnostic};
1414
use rustc_middle::ty::{
1515
inhabitedness::InhabitedPredicate, Clause, PolyExistentialTraitRef, Ty, TyCtxt,
@@ -2229,3 +2229,78 @@ pub enum RedundantImportSub {
22292229
#[label(lint_label_defined_prelude)]
22302230
DefinedPrelude(#[primary_span] Span),
22312231
}
2232+
2233+
#[derive(LintDiagnostic)]
2234+
#[diag(lint_unused_doc_comment)]
2235+
#[help]
2236+
pub struct UnusedDocComment {
2237+
#[label]
2238+
pub span: Span,
2239+
}
2240+
2241+
#[derive(LintDiagnostic)]
2242+
pub enum PatternsInFnsWithoutBody {
2243+
#[diag(lint_pattern_in_foreign)]
2244+
Foreign {
2245+
#[subdiagnostic]
2246+
sub: PatternsInFnsWithoutBodySub,
2247+
},
2248+
#[diag(lint_pattern_in_bodiless)]
2249+
Bodiless {
2250+
#[subdiagnostic]
2251+
sub: PatternsInFnsWithoutBodySub,
2252+
},
2253+
}
2254+
2255+
#[derive(Subdiagnostic)]
2256+
#[suggestion(lint_remove_mut_from_pattern, code = "{ident}", applicability = "machine-applicable")]
2257+
pub struct PatternsInFnsWithoutBodySub {
2258+
#[primary_span]
2259+
pub span: Span,
2260+
2261+
pub ident: Ident,
2262+
}
2263+
2264+
#[derive(LintDiagnostic)]
2265+
#[diag(lint_extern_without_abi)]
2266+
#[help]
2267+
pub struct MissingAbi {
2268+
#[label]
2269+
pub span: Span,
2270+
2271+
pub default_abi: &'static str,
2272+
}
2273+
2274+
#[derive(LintDiagnostic)]
2275+
#[diag(lint_legacy_derive_helpers)]
2276+
pub struct LegacyDeriveHelpers {
2277+
#[label]
2278+
pub span: Span,
2279+
}
2280+
2281+
#[derive(LintDiagnostic)]
2282+
#[diag(lint_proc_macro_back_compat)]
2283+
#[note]
2284+
pub struct ProcMacroBackCompat {
2285+
pub crate_name: String,
2286+
pub fixed_version: String,
2287+
}
2288+
2289+
#[derive(LintDiagnostic)]
2290+
#[diag(lint_or_patterns_back_compat)]
2291+
pub struct OrPatternsBackCompat {
2292+
#[suggestion(code = "{suggestion}", applicability = "machine-applicable")]
2293+
pub span: Span,
2294+
pub suggestion: String,
2295+
}
2296+
2297+
#[derive(LintDiagnostic)]
2298+
#[diag(lint_reserved_prefix)]
2299+
pub struct ReservedPrefix {
2300+
#[label]
2301+
pub label: Span,
2302+
#[suggestion(code = " ", applicability = "machine-applicable")]
2303+
pub suggestion: Span,
2304+
2305+
pub prefix: String,
2306+
}

compiler/rustc_lint_defs/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,10 @@ pub enum BuiltinLintDiag {
619619
is_foreign: bool,
620620
},
621621
LegacyDeriveHelpers(Span),
622-
ProcMacroBackCompat(String),
622+
ProcMacroBackCompat {
623+
crate_name: String,
624+
fixed_version: String,
625+
},
623626
OrPatternsBackCompat(Span, String),
624627
ReservedPrefix(Span, String),
625628
TrailingMacro(bool, Ident),

0 commit comments

Comments
 (0)