Skip to content

Commit db2f44d

Browse files
committed
Generate lint diagnostic message from BuiltinLintDiag
Translation of the lint message happens when the actual diagnostic is created, not when the lint is buffered. Generating the message from BuiltinLintDiag ensures that all required data to construct the message is preserved in the LintBuffer, eventually allowing the messages to be moved to fluent. Remove the `msg` field from BufferedEarlyLint, it is either generated from the data in the BuiltinLintDiag or stored inside BuiltinLintDiag::Normal.
1 parent d04be5e commit db2f44d

File tree

26 files changed

+210
-129
lines changed

26 files changed

+210
-129
lines changed

compiler/rustc_ast_passes/messages.ftl

-5
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,6 @@ ast_passes_const_without_body =
5555
ast_passes_constraint_on_negative_bound =
5656
associated type constraints not allowed on negative bounds
5757
58-
ast_passes_deprecated_where_clause_location =
59-
where clause not allowed here
60-
6158
ast_passes_equality_in_where = equality constraints are not yet supported in `where` clauses
6259
.label = not supported
6360
.suggestion = if `{$ident}` is an associated type you're trying to set, use the associated type binding syntax
@@ -80,8 +77,6 @@ ast_passes_extern_types_cannot = `type`s inside `extern` blocks cannot have {$de
8077
.suggestion = remove the {$remove_descr}
8178
.label = `extern` block begins here
8279
83-
ast_passes_extern_without_abi = extern declarations without an explicit ABI are deprecated
84-
8580
ast_passes_feature_on_non_nightly = `#![feature]` may not be used on the {$channel} release channel
8681
.suggestion = remove the attribute
8782
.stable_since = the feature `{$name}` has been stable since `{$since}` and no longer requires an attribute to enable

compiler/rustc_ast_passes/src/ast_validation.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ use std::ops::{Deref, DerefMut};
2727
use thin_vec::thin_vec;
2828

2929
use crate::errors;
30-
use crate::fluent_generated as fluent;
3130

3231
/// Is `self` allowed semantically as the first parameter in an `FnDecl`?
3332
enum SelfSemantic {
@@ -771,7 +770,6 @@ impl<'a> AstValidator<'a> {
771770
MISSING_ABI,
772771
id,
773772
span,
774-
fluent::ast_passes_extern_without_abi,
775773
BuiltinLintDiag::MissingAbi(span, abi::Abi::FALLBACK),
776774
)
777775
}
@@ -1429,16 +1427,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14291427
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
14301428
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
14311429
if let Some(ident) = ident {
1432-
let msg = match ctxt {
1433-
FnCtxt::Foreign => fluent::ast_passes_pattern_in_foreign,
1434-
_ => fluent::ast_passes_pattern_in_bodiless,
1435-
};
1436-
let diag = BuiltinLintDiag::PatternsInFnsWithoutBody(span, ident);
1430+
let is_foreign = matches!(ctxt, FnCtxt::Foreign);
1431+
let diag =
1432+
BuiltinLintDiag::PatternsInFnsWithoutBody { span, ident, is_foreign };
14371433
self.lint_buffer.buffer_lint_with_diagnostic(
14381434
PATTERNS_IN_FNS_WITHOUT_BODY,
14391435
id,
14401436
span,
1441-
msg,
14421437
diag,
14431438
)
14441439
}
@@ -1515,7 +1510,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15151510
DEPRECATED_WHERE_CLAUSE_LOCATION,
15161511
item.id,
15171512
err.span,
1518-
fluent::ast_passes_deprecated_where_clause_location,
15191513
BuiltinLintDiag::DeprecatedWhereclauseLocation(sugg),
15201514
);
15211515
}

compiler/rustc_ast_passes/src/errors.rs

+2
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,7 @@ pub struct ConstAndCVariadic {
669669

670670
#[derive(Diagnostic)]
671671
#[diag(ast_passes_pattern_in_foreign, code = E0130)]
672+
// FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::PatternsInFnsWithoutBody`)
672673
pub struct PatternInForeign {
673674
#[primary_span]
674675
#[label]
@@ -677,6 +678,7 @@ pub struct PatternInForeign {
677678

678679
#[derive(Diagnostic)]
679680
#[diag(ast_passes_pattern_in_bodiless, code = E0642)]
681+
// FIXME: deduplicate with rustc_lint (`BuiltinLintDiag::PatternsInFnsWithoutBody`)
680682
pub struct PatternInBodiless {
681683
#[primary_span]
682684
#[label]

compiler/rustc_attr/src/builtin.rs

-6
Original file line numberDiff line numberDiff line change
@@ -532,11 +532,6 @@ pub fn cfg_matches(
532532
UNEXPECTED_CFGS,
533533
cfg.span,
534534
lint_node_id,
535-
if let Some(value) = cfg.value {
536-
format!("unexpected `cfg` condition value: `{value}`")
537-
} else {
538-
format!("unexpected `cfg` condition value: (none)")
539-
},
540535
BuiltinLintDiag::UnexpectedCfgValue(
541536
(cfg.name, cfg.name_span),
542537
cfg.value.map(|v| (v, cfg.value_span.unwrap())),
@@ -548,7 +543,6 @@ pub fn cfg_matches(
548543
UNEXPECTED_CFGS,
549544
cfg.span,
550545
lint_node_id,
551-
format!("unexpected `cfg` condition name: `{}`", cfg.name),
552546
BuiltinLintDiag::UnexpectedCfgName(
553547
(cfg.name, cfg.name_span),
554548
cfg.value.map(|v| (v, cfg.value_span.unwrap())),

compiler/rustc_builtin_macros/src/deriving/generic/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -1625,10 +1625,9 @@ impl<'a> TraitDef<'a> {
16251625
BYTE_SLICE_IN_PACKED_STRUCT_WITH_DERIVE,
16261626
sp,
16271627
ast::CRATE_NODE_ID,
1628-
format!(
1629-
"{ty} slice in a packed struct that derives a built-in trait"
1630-
),
1631-
rustc_lint_defs::BuiltinLintDiag::ByteSliceInPackedStructWithDerive,
1628+
rustc_lint_defs::BuiltinLintDiag::ByteSliceInPackedStructWithDerive {
1629+
ty: ty.to_string(),
1630+
},
16321631
);
16331632
} else {
16341633
// Wrap the expression in `{...}`, causing a copy.

compiler/rustc_builtin_macros/src/format.rs

-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,6 @@ fn make_format_args(
557557
let arg_name = args.explicit_args()[index].kind.ident().unwrap();
558558
ecx.buffered_early_lint.push(BufferedEarlyLint {
559559
span: arg_name.span.into(),
560-
msg: format!("named argument `{}` is not used by name", arg_name.name).into(),
561560
node_id: rustc_ast::CRATE_NODE_ID,
562561
lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),
563562
diagnostic: BuiltinLintDiag::NamedArgumentUsedPositionally {

compiler/rustc_expand/src/base.rs

-1
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,6 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) -> bool {
13821382
PROC_MACRO_BACK_COMPAT,
13831383
item.ident.span,
13841384
ast::CRATE_NODE_ID,
1385-
"using an old version of `rental`",
13861385
BuiltinLintDiag::ProcMacroBackCompat(
13871386
"older versions of the `rental` crate will stop compiling in future versions of Rust; \
13881387
please update to `rental` v0.5.6, or switch to one of the `rental` alternatives".to_string()

compiler/rustc_expand/src/expand.rs

-2
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,6 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
17211721
UNUSED_DOC_COMMENTS,
17221722
current_span,
17231723
self.cx.current_expansion.lint_node_id,
1724-
"unused doc comment",
17251724
BuiltinLintDiag::UnusedDocComment(attr.span),
17261725
);
17271726
} else if rustc_attr::is_builtin_attr(attr) {
@@ -1733,7 +1732,6 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
17331732
UNUSED_ATTRIBUTES,
17341733
attr.span,
17351734
self.cx.current_expansion.lint_node_id,
1736-
format!("unused attribute `{attr_name}`"),
17371735
BuiltinLintDiag::UnusedBuiltinAttribute {
17381736
attr_name,
17391737
macro_name: pprust::path_to_string(&call.path),

compiler/rustc_expand/src/mbe/macro_rules.rs

-2
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ impl<'a> ParserAnyMacro<'a> {
8282
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
8383
parser.token.span,
8484
lint_node_id,
85-
"trailing semicolon in macro used in expression position",
8685
BuiltinLintDiag::TrailingMacro(is_trailing_mac, macro_ident),
8786
);
8887
}
@@ -1153,7 +1152,6 @@ fn check_matcher_core<'tt>(
11531152
RUST_2021_INCOMPATIBLE_OR_PATTERNS,
11541153
span,
11551154
ast::CRATE_NODE_ID,
1156-
"the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro",
11571155
BuiltinLintDiag::OrPatternsBackCompat(span, suggestion),
11581156
);
11591157
}

compiler/rustc_interface/src/util.rs

-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,6 @@ pub(crate) fn check_attr_crate_type(
388388
lint::builtin::UNKNOWN_CRATE_TYPES,
389389
ast::CRATE_NODE_ID,
390390
span,
391-
"invalid `crate_type` value",
392391
BuiltinLintDiag::UnknownCrateTypes(
393392
span,
394393
"did you mean".to_string(),

compiler/rustc_lint/messages.ftl

+11
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ lint_deprecated_lint_name =
187187
.suggestion = change it to
188188
.help = change it to {$replace}
189189
190+
lint_deprecated_where_clause_location =
191+
where clause not allowed here
192+
190193
lint_diag_out_of_impl =
191194
diagnostics should only be created in `Diagnostic`/`Subdiagnostic`/`LintDiagnostic` impls
192195
@@ -216,6 +219,8 @@ lint_expectation = this lint expectation is unfulfilled
216219
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
217220
.rationale = {$rationale}
218221
222+
lint_extern_without_abi = extern declarations without an explicit ABI are deprecated
223+
219224
lint_for_loops_over_fallibles =
220225
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
221226
.suggestion = consider using `if let` to clear intent
@@ -515,6 +520,12 @@ lint_path_statement_drop = path statement drops value
515520
516521
lint_path_statement_no_effect = path statement with no effect
517522
523+
lint_pattern_in_bodiless = patterns aren't allowed in functions without bodies
524+
.label = pattern not allowed in function without body
525+
526+
lint_pattern_in_foreign = patterns aren't allowed in foreign function declarations
527+
.label = pattern not allowed in foreign function
528+
518529
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
519530
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
520531
.label = expression has type `{$orig_ty}`

compiler/rustc_lint/src/context.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -536,12 +536,11 @@ pub trait LintContext {
536536
&self,
537537
lint: &'static Lint,
538538
span: Option<impl Into<MultiSpan>>,
539-
msg: impl Into<DiagMessage>,
540539
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
541540
diagnostic: BuiltinLintDiag,
542541
) {
543542
// We first generate a blank diagnostic.
544-
self.opt_span_lint(lint, span, msg, |db| {
543+
self.opt_span_lint(lint, span, diagnostics::builtin_message(&diagnostic), |db| {
545544
// Now, set up surrounding context.
546545
diagnostics::builtin(self.sess(), diagnostic, db);
547546
// Rewrap `db`, and pass control to the user.

0 commit comments

Comments
 (0)