Skip to content

Commit c227f35

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 2482f3c commit c227f35

File tree

26 files changed

+212
-130
lines changed

26 files changed

+212
-130
lines changed

compiler/rustc_ast_passes/messages.ftl

Lines changed: 0 additions & 5 deletions
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

Lines changed: 5 additions & 10 deletions
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 {
@@ -770,7 +769,6 @@ impl<'a> AstValidator<'a> {
770769
MISSING_ABI,
771770
id,
772771
span,
773-
fluent::ast_passes_extern_without_abi,
774772
BuiltinLintDiag::MissingAbi(span, abi::Abi::FALLBACK),
775773
)
776774
}
@@ -1428,17 +1426,15 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
14281426
Self::check_decl_no_pat(&sig.decl, |span, ident, mut_ident| {
14291427
if mut_ident && matches!(ctxt, FnCtxt::Assoc(_)) {
14301428
if let Some(ident) = ident {
1431-
let msg = match ctxt {
1432-
FnCtxt::Foreign => fluent::ast_passes_pattern_in_foreign,
1433-
_ => fluent::ast_passes_pattern_in_bodiless,
1434-
};
1435-
let diag = BuiltinLintDiag::PatternsInFnsWithoutBody(span, ident);
14361429
self.lint_buffer.buffer_lint_with_diagnostic(
14371430
PATTERNS_IN_FNS_WITHOUT_BODY,
14381431
id,
14391432
span,
1440-
msg,
1441-
diag,
1433+
BuiltinLintDiag::PatternsInFnsWithoutBody {
1434+
span,
1435+
ident,
1436+
is_foreign: matches!(ctxt, FnCtxt::Foreign),
1437+
},
14421438
)
14431439
}
14441440
} else {
@@ -1514,7 +1510,6 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
15141510
DEPRECATED_WHERE_CLAUSE_LOCATION,
15151511
item.id,
15161512
err.span,
1517-
fluent::ast_passes_deprecated_where_clause_location,
15181513
BuiltinLintDiag::DeprecatedWhereclauseLocation(sugg),
15191514
);
15201515
}

compiler/rustc_ast_passes/src/errors.rs

Lines changed: 2 additions & 0 deletions
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

Lines changed: 0 additions & 6 deletions
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

Lines changed: 3 additions & 4 deletions
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

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

compiler/rustc_expand/src/base.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1370,7 +1370,6 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) -> bool {
13701370
PROC_MACRO_BACK_COMPAT,
13711371
item.ident.span,
13721372
ast::CRATE_NODE_ID,
1373-
"using an old version of `rental`",
13741373
BuiltinLintDiag::ProcMacroBackCompat(
13751374
"older versions of the `rental` crate will stop compiling in future versions of Rust; \
13761375
please update to `rental` v0.5.6, or switch to one of the `rental` alternatives".to_string()

compiler/rustc_expand/src/expand.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,6 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
18031803
UNUSED_DOC_COMMENTS,
18041804
current_span,
18051805
self.cx.current_expansion.lint_node_id,
1806-
"unused doc comment",
18071806
BuiltinLintDiag::UnusedDocComment(attr.span),
18081807
);
18091808
} else if rustc_attr::is_builtin_attr(attr) {
@@ -1815,7 +1814,6 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
18151814
UNUSED_ATTRIBUTES,
18161815
attr.span,
18171816
self.cx.current_expansion.lint_node_id,
1818-
format!("unused attribute `{attr_name}`"),
18191817
BuiltinLintDiag::UnusedBuiltinAttribute {
18201818
attr_name,
18211819
macro_name: pprust::path_to_string(&call.path),

compiler/rustc_expand/src/mbe/macro_rules.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@ impl<'a> ParserAnyMacro<'a> {
8383
SEMICOLON_IN_EXPRESSIONS_FROM_MACROS,
8484
parser.token.span,
8585
lint_node_id,
86-
"trailing semicolon in macro used in expression position",
8786
BuiltinLintDiag::TrailingMacro(is_trailing_mac, macro_ident),
8887
);
8988
}
@@ -1158,7 +1157,6 @@ fn check_matcher_core<'tt>(
11581157
RUST_2021_INCOMPATIBLE_OR_PATTERNS,
11591158
span,
11601159
ast::CRATE_NODE_ID,
1161-
"the meaning of the `pat` fragment specifier is changing in Rust 2021, which may affect this macro",
11621160
BuiltinLintDiag::OrPatternsBackCompat(span, suggestion),
11631161
);
11641162
}

compiler/rustc_interface/src/util.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ pub(crate) fn check_attr_crate_type(
409409
lint::builtin::UNKNOWN_CRATE_TYPES,
410410
ast::CRATE_NODE_ID,
411411
span,
412-
"invalid `crate_type` value",
413412
BuiltinLintDiag::UnknownCrateTypes(
414413
span,
415414
"did you mean".to_string(),

compiler/rustc_lint/messages.ftl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,9 @@ lint_deprecated_lint_name =
180180
.suggestion = change it to
181181
.help = change it to {$replace}
182182
183+
lint_deprecated_where_clause_location =
184+
where clause not allowed here
185+
183186
lint_diag_out_of_impl =
184187
diagnostics should only be created in `Diagnostic`/`Subdiagnostic`/`LintDiagnostic` impls
185188
@@ -209,6 +212,8 @@ lint_expectation = this lint expectation is unfulfilled
209212
.note = the `unfulfilled_lint_expectations` lint can't be expected and will always produce this message
210213
.rationale = {$rationale}
211214
215+
lint_extern_without_abi = extern declarations without an explicit ABI are deprecated
216+
212217
lint_for_loops_over_fallibles =
213218
for loop over {$article} `{$ty}`. This is more readably written as an `if let` statement
214219
.suggestion = consider using `if let` to clear intent
@@ -521,6 +526,12 @@ lint_path_statement_drop = path statement drops value
521526
522527
lint_path_statement_no_effect = path statement with no effect
523528
529+
lint_pattern_in_bodiless = patterns aren't allowed in functions without bodies
530+
.label = pattern not allowed in function without body
531+
532+
lint_pattern_in_foreign = patterns aren't allowed in foreign function declarations
533+
.label = pattern not allowed in foreign function
534+
524535
lint_ptr_null_checks_fn_ptr = function pointers are not nullable, so checking them for null will always return false
525536
.help = wrap the function pointer inside an `Option` and use `Option::is_none` to check for null pointer value
526537
.label = expression has type `{$orig_ty}`

compiler/rustc_lint/src/context.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -539,12 +539,11 @@ pub trait LintContext {
539539
&self,
540540
lint: &'static Lint,
541541
span: Option<impl Into<MultiSpan>>,
542-
msg: impl Into<DiagMessage>,
543542
decorate: impl for<'a, 'b> FnOnce(&'b mut Diag<'a, ()>),
544543
diagnostic: BuiltinLintDiag,
545544
) {
546545
// We first generate a blank diagnostic.
547-
self.opt_span_lint(lint, span, msg, |db| {
546+
self.opt_span_lint(lint, span, diagnostics::builtin_message(&diagnostic), |db| {
548547
// Now, set up surrounding context.
549548
diagnostics::builtin(self.sess(), diagnostic, db);
550549
// Rewrap `db`, and pass control to the user.

0 commit comments

Comments
 (0)