Skip to content

Commit 30f1697

Browse files
committed
Convert uses of BuiltinLintDiag::Normal to custom variants
This ensures all diagnostic messages are created at diagnostic emission time, making them translatable.
1 parent 315dbab commit 30f1697

File tree

22 files changed

+296
-175
lines changed

22 files changed

+296
-175
lines changed

compiler/rustc_builtin_macros/messages.ftl

-2
Original file line numberDiff line numberDiff line change
@@ -247,5 +247,3 @@ builtin_macros_unexpected_lit = expected path to a trait, found literal
247247
.label = not a trait
248248
.str_lit = try using `#[derive({$sym})]`
249249
.other = for example, write `#[derive(Debug)]` for `Debug`
250-
251-
builtin_macros_unnameable_test_items = cannot test inner items

compiler/rustc_builtin_macros/src/asm.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::errors;
22
use crate::util::expr_to_spanned_string;
33
use ast::token::IdentIsRaw;
4+
use lint::BuiltinLintDiag;
45
use rustc_ast as ast;
56
use rustc_ast::ptr::P;
67
use rustc_ast::token::{self, Delimiter};
@@ -509,19 +510,19 @@ fn expand_preparsed_asm(
509510
};
510511

511512
if template_str.contains(".intel_syntax") {
512-
ecx.psess().buffer_lint(
513+
ecx.psess().buffer_lint_with_diagnostic(
513514
lint::builtin::BAD_ASM_STYLE,
514515
find_span(".intel_syntax"),
515516
ecx.current_expansion.lint_node_id,
516-
"avoid using `.intel_syntax`, Intel syntax is the default",
517+
BuiltinLintDiag::AvoidUsingIntelSyntax,
517518
);
518519
}
519520
if template_str.contains(".att_syntax") {
520-
ecx.psess().buffer_lint(
521+
ecx.psess().buffer_lint_with_diagnostic(
521522
lint::builtin::BAD_ASM_STYLE,
522523
find_span(".att_syntax"),
523524
ecx.current_expansion.lint_node_id,
524-
"avoid using `.att_syntax`, prefer using `options(att_syntax)` instead",
525+
BuiltinLintDiag::AvoidUsingAttSyntax,
525526
);
526527
}
527528
}

compiler/rustc_builtin_macros/src/source_util.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use rustc_expand::base::{
1111
resolve_path, DummyResult, ExpandResult, ExtCtxt, MacEager, MacResult, MacroExpanderResult,
1212
};
1313
use rustc_expand::module::DirOwnership;
14+
use rustc_lint_defs::BuiltinLintDiag;
1415
use rustc_parse::new_parser_from_file;
1516
use rustc_parse::parser::{ForceCollect, Parser};
1617
use rustc_session::lint::builtin::INCOMPLETE_INCLUDE;
@@ -143,11 +144,11 @@ pub(crate) fn expand_include<'cx>(
143144
fn make_expr(mut self: Box<ExpandInclude<'a>>) -> Option<P<ast::Expr>> {
144145
let expr = parse_expr(&mut self.p).ok()?;
145146
if self.p.token != token::Eof {
146-
self.p.psess.buffer_lint(
147+
self.p.psess.buffer_lint_with_diagnostic(
147148
INCOMPLETE_INCLUDE,
148149
self.p.token.span,
149150
self.node_id,
150-
"include macro expected single expression in source",
151+
BuiltinLintDiag::IncompleteInclude,
151152
);
152153
}
153154
Some(expr)

compiler/rustc_builtin_macros/src/test_harness.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use rustc_ast::{attr, ModKind};
99
use rustc_expand::base::{ExtCtxt, ResolverExpand};
1010
use rustc_expand::expand::{AstFragment, ExpansionConfig};
1111
use rustc_feature::Features;
12+
use rustc_lint_defs::BuiltinLintDiag;
1213
use rustc_session::lint::builtin::UNNAMEABLE_TEST_ITEMS;
1314
use rustc_session::Session;
1415
use rustc_span::hygiene::{AstPass, SyntaxContext, Transparency};
@@ -159,11 +160,11 @@ struct InnerItemLinter<'a> {
159160
impl<'a> Visitor<'a> for InnerItemLinter<'_> {
160161
fn visit_item(&mut self, i: &'a ast::Item) {
161162
if let Some(attr) = attr::find_by_name(&i.attrs, sym::rustc_test_marker) {
162-
self.sess.psess.buffer_lint(
163+
self.sess.psess.buffer_lint_with_diagnostic(
163164
UNNAMEABLE_TEST_ITEMS,
164165
attr.span,
165166
i.id,
166-
crate::fluent_generated::builtin_macros_unnameable_test_items,
167+
BuiltinLintDiag::UnnameableTestItems,
167168
);
168169
}
169170
}

compiler/rustc_builtin_macros/src/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_errors::{Applicability, Diag, ErrorGuaranteed};
55
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt};
66
use rustc_expand::expand::AstFragment;
77
use rustc_feature::AttributeTemplate;
8-
use rustc_lint_defs::builtin::DUPLICATE_MACRO_ATTRIBUTES;
8+
use rustc_lint_defs::{builtin::DUPLICATE_MACRO_ATTRIBUTES, BuiltinLintDiag};
99
use rustc_parse::{parser, validate_attr};
1010
use rustc_session::errors::report_lit_error;
1111
use rustc_span::{BytePos, Span, Symbol};
@@ -42,11 +42,11 @@ pub(crate) fn warn_on_duplicate_attribute(ecx: &ExtCtxt<'_>, item: &Annotatable,
4242
};
4343
if let Some(attrs) = attrs {
4444
if let Some(attr) = attr::find_by_name(attrs, name) {
45-
ecx.psess().buffer_lint(
45+
ecx.psess().buffer_lint_with_diagnostic(
4646
DUPLICATE_MACRO_ATTRIBUTES,
4747
attr.span,
4848
ecx.current_expansion.lint_node_id,
49-
"duplicated attribute",
49+
BuiltinLintDiag::DuplicateMacroAttribute,
5050
);
5151
}
5252
}

compiler/rustc_expand/src/config.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use rustc_attr as attr;
1414
use rustc_data_structures::flat_map_in_place::FlatMapInPlace;
1515
use rustc_feature::Features;
1616
use rustc_feature::{ACCEPTED_FEATURES, REMOVED_FEATURES, UNSTABLE_FEATURES};
17+
use rustc_lint_defs::BuiltinLintDiag;
1718
use rustc_parse::validate_attr;
1819
use rustc_session::parse::feature_err;
1920
use rustc_session::Session;
@@ -249,11 +250,11 @@ impl<'a> StripUnconfigured<'a> {
249250

250251
// Lint on zero attributes in source.
251252
if expanded_attrs.is_empty() {
252-
self.sess.psess.buffer_lint(
253+
self.sess.psess.buffer_lint_with_diagnostic(
253254
rustc_lint_defs::builtin::UNUSED_ATTRIBUTES,
254255
attr.span,
255256
ast::CRATE_NODE_ID,
256-
"`#[cfg_attr]` does not expand to any attributes",
257+
BuiltinLintDiag::CfgAttrNoAttributes,
257258
);
258259
}
259260

@@ -333,19 +334,19 @@ impl<'a> StripUnconfigured<'a> {
333334
item_span,
334335
);
335336
if attr.has_name(sym::crate_type) {
336-
self.sess.psess.buffer_lint(
337+
self.sess.psess.buffer_lint_with_diagnostic(
337338
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
338339
attr.span,
339340
ast::CRATE_NODE_ID,
340-
"`crate_type` within an `#![cfg_attr] attribute is deprecated`",
341+
BuiltinLintDiag::CrateTypeInCfgAttr,
341342
);
342343
}
343344
if attr.has_name(sym::crate_name) {
344-
self.sess.psess.buffer_lint(
345+
self.sess.psess.buffer_lint_with_diagnostic(
345346
rustc_lint_defs::builtin::DEPRECATED_CFG_ATTR_CRATE_TYPE_NAME,
346347
attr.span,
347348
ast::CRATE_NODE_ID,
348-
"`crate_name` within an `#![cfg_attr] attribute is deprecated`",
349+
BuiltinLintDiag::CrateNameInCfgAttr,
349350
);
350351
}
351352
attr

compiler/rustc_expand/src/mbe/macro_check.rs

+10-16
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,8 @@ use crate::mbe::{KleeneToken, TokenTree};
110110
use rustc_ast::token::{Delimiter, IdentIsRaw, Token, TokenKind};
111111
use rustc_ast::{NodeId, DUMMY_NODE_ID};
112112
use rustc_data_structures::fx::FxHashMap;
113-
use rustc_errors::{DiagMessage, MultiSpan};
113+
use rustc_errors::MultiSpan;
114+
use rustc_lint_defs::BuiltinLintDiag;
114115
use rustc_session::lint::builtin::{META_VARIABLE_MISUSE, MISSING_FRAGMENT_SPECIFIER};
115116
use rustc_session::parse::ParseSess;
116117
use rustc_span::symbol::kw;
@@ -252,7 +253,7 @@ fn check_binders(
252253
// 1. The meta-variable is already bound in the current LHS: This is an error.
253254
let mut span = MultiSpan::from_span(span);
254255
span.push_span_label(prev_info.span, "previous declaration");
255-
buffer_lint(psess, span, node_id, "duplicate matcher binding");
256+
buffer_lint(psess, span, node_id, BuiltinLintDiag::DuplicateMatcherBinding);
256257
} else if get_binder_info(macros, binders, name).is_none() {
257258
// 2. The meta-variable is free: This is a binder.
258259
binders.insert(name, BinderInfo { span, ops: ops.into() });
@@ -267,11 +268,11 @@ fn check_binders(
267268
// FIXME: Report this as a hard error eventually and remove equivalent errors from
268269
// `parse_tt_inner` and `nameize`. Until then the error may be reported twice, once
269270
// as a hard error and then once as a buffered lint.
270-
psess.buffer_lint(
271+
psess.buffer_lint_with_diagnostic(
271272
MISSING_FRAGMENT_SPECIFIER,
272273
span,
273274
node_id,
274-
"missing fragment specifier",
275+
BuiltinLintDiag::MissingFragmentSpecifier,
275276
);
276277
}
277278
if !macros.is_empty() {
@@ -595,7 +596,7 @@ fn check_ops_is_prefix(
595596
return;
596597
}
597598
}
598-
buffer_lint(psess, span.into(), node_id, format!("unknown macro variable `{name}`"));
599+
buffer_lint(psess, span.into(), node_id, BuiltinLintDiag::UnknownMacroVariable(name));
599600
}
600601

601602
/// Returns whether `binder_ops` is a prefix of `occurrence_ops`.
@@ -628,30 +629,23 @@ fn ops_is_prefix(
628629
if i >= occurrence_ops.len() {
629630
let mut span = MultiSpan::from_span(span);
630631
span.push_span_label(binder.span, "expected repetition");
631-
let message = format!("variable '{name}' is still repeating at this depth");
632-
buffer_lint(psess, span, node_id, message);
632+
buffer_lint(psess, span, node_id, BuiltinLintDiag::MetaVariableStillRepeating(name));
633633
return;
634634
}
635635
let occurrence = &occurrence_ops[i];
636636
if occurrence.op != binder.op {
637637
let mut span = MultiSpan::from_span(span);
638638
span.push_span_label(binder.span, "expected repetition");
639639
span.push_span_label(occurrence.span, "conflicting repetition");
640-
let message = "meta-variable repeats with different Kleene operator";
641-
buffer_lint(psess, span, node_id, message);
640+
buffer_lint(psess, span, node_id, BuiltinLintDiag::MetaVariableWrongOperator);
642641
return;
643642
}
644643
}
645644
}
646645

647-
fn buffer_lint(
648-
psess: &ParseSess,
649-
span: MultiSpan,
650-
node_id: NodeId,
651-
message: impl Into<DiagMessage>,
652-
) {
646+
fn buffer_lint(psess: &ParseSess, span: MultiSpan, node_id: NodeId, diag: BuiltinLintDiag) {
653647
// Macros loaded from other crates have dummy node ids.
654648
if node_id != DUMMY_NODE_ID {
655-
psess.buffer_lint(META_VARIABLE_MISUSE, span, node_id, message);
649+
psess.buffer_lint_with_diagnostic(META_VARIABLE_MISUSE, span, node_id, diag);
656650
}
657651
}

compiler/rustc_interface/src/util.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -378,30 +378,17 @@ pub(crate) fn check_attr_crate_type(
378378

379379
if let ast::MetaItemKind::NameValue(spanned) = a.meta_kind().unwrap() {
380380
let span = spanned.span;
381-
let lev_candidate = find_best_match_for_name(
381+
let candidate = find_best_match_for_name(
382382
&CRATE_TYPES.iter().map(|(k, _)| *k).collect::<Vec<_>>(),
383383
n,
384384
None,
385385
);
386-
if let Some(candidate) = lev_candidate {
387-
lint_buffer.buffer_lint_with_diagnostic(
388-
lint::builtin::UNKNOWN_CRATE_TYPES,
389-
ast::CRATE_NODE_ID,
390-
span,
391-
BuiltinLintDiag::UnknownCrateTypes(
392-
span,
393-
"did you mean".to_string(),
394-
format!("\"{candidate}\""),
395-
),
396-
);
397-
} else {
398-
lint_buffer.buffer_lint(
399-
lint::builtin::UNKNOWN_CRATE_TYPES,
400-
ast::CRATE_NODE_ID,
401-
span,
402-
"invalid `crate_type` value",
403-
);
404-
}
386+
lint_buffer.buffer_lint_with_diagnostic(
387+
lint::builtin::UNKNOWN_CRATE_TYPES,
388+
ast::CRATE_NODE_ID,
389+
span,
390+
BuiltinLintDiag::UnknownCrateTypes { span, candidate },
391+
);
405392
}
406393
} else {
407394
// This is here mainly to check for using a macro, such as

compiler/rustc_lint/messages.ftl

+14
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ lint_builtin_unused_doc_comment = unused doc comment
163163
lint_builtin_while_true = denote infinite loops with `loop {"{"} ... {"}"}`
164164
.suggestion = use `loop`
165165
166+
lint_cfg_attr_no_attributes =
167+
`#[cfg_attr]` does not expand to any attributes
168+
166169
lint_check_name_unknown_tool = unknown lint tool: `{$tool_name}`
167170
168171
lint_command_line_source = `forbid` lint level was set on command line
@@ -171,6 +174,12 @@ lint_confusable_identifier_pair = found both `{$existing_sym}` and `{$sym}` as i
171174
.current_use = this identifier can be confused with `{$existing_sym}`
172175
.other_use = other identifier used here
173176
177+
lint_crate_name_in_cfg_attr_deprecated =
178+
`crate_name` within an `#![cfg_attr] attribute is deprecated`
179+
180+
lint_crate_type_in_cfg_attr_deprecated =
181+
`crate_type` within an `#![cfg_attr] attribute is deprecated`
182+
174183
lint_cstring_ptr = getting the inner pointer of a temporary `CString`
175184
.as_ptr_label = this pointer will be invalid
176185
.unwrap_label = this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
@@ -651,6 +660,8 @@ lint_unknown_lint =
651660
lint_unknown_tool_in_scoped_lint = unknown tool name `{$tool_name}` found in scoped lint: `{$tool_name}::{$lint_name}`
652661
.help = add `#![register_tool({$tool_name})]` to the crate root
653662
663+
lint_unnameable_test_items = cannot test inner items
664+
654665
lint_unsupported_group = `{$lint_group}` lint group is not supported with ´--force-warn´
655666
656667
lint_untranslatable_diag = diagnostics should be created using translatable messages
@@ -690,3 +701,6 @@ lint_unused_result = unused result of type `{$ty}`
690701
691702
lint_variant_size_differences =
692703
enum variant is more than three times larger ({$largest} bytes) than the next largest
704+
705+
lint_wasm_c_abi =
706+
older versions of the `wasm-bindgen` crate will be incompatible with future versions of Rust; please update to `wasm-bindgen` v0.2.88

0 commit comments

Comments
 (0)