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.
This commit is contained in:
Xiretza 2024-04-14 17:59:54 +00:00
parent 2482f3c17c
commit c227f35a9c
26 changed files with 212 additions and 130 deletions

View file

@ -32,7 +32,7 @@ use rustc_ast as ast;
use rustc_ast::visit::{self, Visitor};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap, FxIndexSet};
use rustc_data_structures::unord::UnordSet;
use rustc_errors::{pluralize, MultiSpan};
use rustc_errors::MultiSpan;
use rustc_hir::def::{DefKind, Res};
use rustc_session::lint::builtin::{MACRO_USE_EXTERN_CRATE, UNUSED_EXTERN_CRATES};
use rustc_session::lint::builtin::{UNUSED_IMPORTS, UNUSED_QUALIFICATIONS};
@ -155,7 +155,6 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
UNUSED_EXTERN_CRATES,
extern_crate.id,
span,
"unused extern crate",
BuiltinLintDiag::UnusedExternCrate {
removal_span: extern_crate.span_with_attributes,
},
@ -208,7 +207,6 @@ impl<'a, 'b, 'tcx> UnusedImportCheckVisitor<'a, 'b, 'tcx> {
UNUSED_EXTERN_CRATES,
extern_crate.id,
extern_crate.span,
"`extern crate` is not idiomatic in the new edition",
BuiltinLintDiag::ExternCrateNotIdiomatic { vis_span, ident_span },
);
}
@ -459,16 +457,6 @@ impl Resolver<'_, '_> {
.collect::<Vec<String>>();
span_snippets.sort();
let msg = format!(
"unused import{}{}",
pluralize!(ms.primary_spans().len()),
if !span_snippets.is_empty() {
format!(": {}", span_snippets.join(", "))
} else {
String::new()
}
);
let fix_msg = if fixes.len() == 1 && fixes[0].0 == unused.item_span {
"remove the whole `use` item"
} else if ms.primary_spans().len() > 1 {
@ -505,8 +493,12 @@ impl Resolver<'_, '_> {
UNUSED_IMPORTS,
unused.use_tree_id,
ms,
msg,
BuiltinLintDiag::UnusedImports(fix_msg.into(), fixes, test_module_span),
BuiltinLintDiag::UnusedImports {
fix_msg: fix_msg.into(),
fixes,
test_module_span,
span_snippets,
},
);
}
@ -556,7 +548,6 @@ impl Resolver<'_, '_> {
UNUSED_QUALIFICATIONS,
unn_qua.node_id,
unn_qua.path_span,
"unnecessary qualification",
BuiltinLintDiag::UnusedQualifications { removal_span: unn_qua.removal_span },
);
}

View file

@ -128,13 +128,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
self.report_with_use_injections(krate);
for &(span_use, span_def) in &self.macro_expanded_macro_export_errors {
let msg = "macro-expanded `macro_export` macros from the current crate \
cannot be referred to by absolute paths";
self.lint_buffer.buffer_lint_with_diagnostic(
MACRO_EXPANDED_MACRO_EXPORTS_ACCESSED_BY_ABSOLUTE_PATHS,
CRATE_NODE_ID,
span_use,
msg,
BuiltinLintDiag::MacroExpandedMacroExportsAccessedByAbsolutePaths(span_def),
);
}
@ -149,7 +146,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
AMBIGUOUS_GLOB_IMPORTS,
import.root_id,
ambiguity_error.ident.span,
diag.msg.to_string(),
BuiltinLintDiag::AmbiguousGlobImports { diag },
);
} else {
@ -530,8 +526,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
ABSOLUTE_PATHS_NOT_STARTING_WITH_CRATE,
node_id,
root_span,
"absolute paths must start with `self`, `super`, \
`crate`, or an external crate name in the 2018 edition",
diag,
);
}

View file

@ -528,14 +528,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
PROC_MACRO_DERIVE_RESOLUTION_FALLBACK,
lint_id,
orig_ident.span,
format!(
"cannot find {} `{}` in this scope",
ns.descr(),
ident
),
BuiltinLintDiag::ProcMacroDeriveResolutionFallback(
orig_ident.span,
),
BuiltinLintDiag::ProcMacroDeriveResolutionFallback {
span: orig_ident.span,
ns,
ident,
},
);
}
let misc_flags = if module == this.graph_root {

View file

@ -623,7 +623,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
AMBIGUOUS_GLOB_REEXPORTS,
import.root_id,
import.root_span,
"ambiguous glob re-exports",
BuiltinLintDiag::AmbiguousGlobReexports {
name: key.ident.to_string(),
namespace: key.ns.descr().to_string(),
@ -659,7 +658,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
HIDDEN_GLOB_REEXPORTS,
binding_id,
binding.span,
"private item shadows public glob re-export",
BuiltinLintDiag::HiddenGlobReexports {
name: key.ident.name.to_string(),
namespace: key.ns.descr().to_owned(),
@ -1015,17 +1013,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
&& !max_vis.is_at_least(import_vis, self.tcx)
{
let def_id = self.local_def_id(id);
let msg = format!(
"glob import doesn't reexport anything with visibility `{}` because no imported item is public enough",
import_vis.to_string(def_id, self.tcx)
);
self.lint_buffer.buffer_lint_with_diagnostic(
UNUSED_IMPORTS,
id,
import.span,
msg,
BuiltinLintDiag::RedundantImportVisibility {
max_vis: max_vis.to_string(def_id, self.tcx),
import_vis: import_vis.to_string(def_id, self.tcx),
span: import.span,
},
);
@ -1397,7 +1391,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
UNUSED_IMPORTS,
id,
import.span,
format!("the item `{source}` is imported redundantly"),
BuiltinLintDiag::RedundantImport(redundant_spans, source),
);
*/

View file

@ -1675,16 +1675,10 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
return;
}
LifetimeRibKind::AnonymousWarn(node_id) => {
let msg = if elided {
"`&` without an explicit lifetime name cannot be used here"
} else {
"`'_` cannot be used here"
};
self.r.lint_buffer.buffer_lint_with_diagnostic(
lint::builtin::ELIDED_LIFETIMES_IN_ASSOCIATED_CONSTANT,
node_id,
lifetime.ident.span,
msg,
lint::BuiltinLintDiag::AssociatedConstElidedLifetime {
elided,
span: lifetime.ident.span,
@ -1970,7 +1964,6 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
lint::builtin::ELIDED_LIFETIMES_IN_PATHS,
segment_id,
elided_lifetime_span,
"hidden lifetime parameters in types are deprecated",
lint::BuiltinLintDiag::ElidedLifetimesInPaths(
expected_lifetimes,
path_span,

View file

@ -2655,11 +2655,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
lint::builtin::SINGLE_USE_LIFETIMES,
param.id,
param.ident.span,
format!("lifetime parameter `{}` only used once", param.ident),
lint::BuiltinLintDiag::SingleUseLifetime {
param_span: param.ident.span,
use_span: Some((use_span, elidable)),
deletion_span,
ident: param.ident,
},
);
}
@ -2673,11 +2673,11 @@ impl<'a: 'ast, 'ast, 'tcx> LateResolutionVisitor<'a, '_, 'ast, 'tcx> {
lint::builtin::UNUSED_LIFETIMES,
param.id,
param.ident.span,
format!("lifetime parameter `{}` never used", param.ident),
lint::BuiltinLintDiag::SingleUseLifetime {
param_span: param.ident.span,
use_span: None,
deletion_span,
ident: param.ident,
},
);
}

View file

@ -786,7 +786,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
LEGACY_DERIVE_HELPERS,
node_id,
ident.span,
"derive helper attribute is used before it is introduced",
BuiltinLintDiag::LegacyDeriveHelpers(binding.span),
);
}