Skip to content

Commit 6a12129

Browse files
committed
Rewrite the untranslatable_diagnostic lint.
Currently it only checks calls to functions marked with `#[rustc_lint_diagnostics]`. This commit changes it to check calls to any function with a `impl Into<{D,Subd}iagnosticMessage>` parameter. This greatly improves its coverage and doesn't rely on people remembering to add `#[rustc_lint_diagnostics]`. The commit also adds `#[allow(rustc::untranslatable_diagnostic)`] attributes to places that need it that are caught by the improved lint. These places that might be easy to convert to translatable diagnostics. XXX: fulfill_expectation: remove rustc_lint_diagnostics
1 parent b564d14 commit 6a12129

File tree

33 files changed

+172
-52
lines changed

33 files changed

+172
-52
lines changed

compiler/rustc_ast_lowering/src/asm.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ use std::collections::hash_map::Entry;
2222
use std::fmt::Write;
2323

2424
impl<'a, 'hir> LoweringContext<'a, 'hir> {
25+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
2526
pub(crate) fn lower_inline_asm(
2627
&mut self,
2728
sp: Span,

compiler/rustc_ast_lowering/src/expr.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14951495
}
14961496
}
14971497

1498+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
14981499
fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
14991500
let is_async_gen = match self.coroutine_kind {
15001501
Some(hir::CoroutineKind::Desugared(hir::CoroutineDesugaring::Gen, _)) => false,

compiler/rustc_ast_lowering/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2272,6 +2272,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22722272
self.expr_block(block)
22732273
}
22742274

2275+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
22752276
fn lower_array_length(&mut self, c: &AnonConst) -> hir::ArrayLen {
22762277
match c.value.kind {
22772278
ExprKind::Underscore => {

compiler/rustc_ast_passes/src/feature_gate.rs

+4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::errors;
1717
macro_rules! gate {
1818
($visitor:expr, $feature:ident, $span:expr, $explain:expr) => {{
1919
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
20+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
2021
feature_err(&$visitor.sess, sym::$feature, $span, $explain).emit();
2122
}
2223
}};
@@ -34,6 +35,7 @@ macro_rules! gate {
3435
macro_rules! gate_alt {
3536
($visitor:expr, $has_feature:expr, $name:expr, $span:expr, $explain:expr) => {{
3637
if !$has_feature && !$span.allows_unstable($name) {
38+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
3739
feature_err(&$visitor.sess, $name, $span, $explain).emit();
3840
}
3941
}};
@@ -73,6 +75,7 @@ struct PostExpansionVisitor<'a> {
7375
}
7476

7577
impl<'a> PostExpansionVisitor<'a> {
78+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
7679
fn check_abi(&self, abi: ast::StrLit, constness: ast::Const) {
7780
let ast::StrLit { symbol_unescaped, span, .. } = abi;
7881

@@ -579,6 +582,7 @@ pub fn check_crate(krate: &ast::Crate, sess: &Session, features: &Features) {
579582
if let Ok(snippet) = sm.span_to_snippet(span)
580583
&& snippet == "!"
581584
{
585+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
582586
feature_err(sess, sym::never_patterns, span, "`!` patterns are experimental")
583587
.emit();
584588
} else {

compiler/rustc_attr/src/builtin.rs

+3
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ pub struct Condition {
516516
}
517517

518518
/// Tests if a cfg-pattern matches the cfg set
519+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
519520
pub fn cfg_matches(
520521
cfg: &ast::MetaItem,
521522
sess: &Session,
@@ -566,6 +567,7 @@ fn try_gate_cfg(name: Symbol, span: Span, sess: &Session, features: Option<&Feat
566567
}
567568
}
568569

570+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
569571
fn gate_cfg(gated_cfg: &GatedCfg, cfg_span: Span, sess: &Session, features: &Features) {
570572
let (cfg, feature, has_feature) = gated_cfg;
571573
if !has_feature(features) && !cfg_span.allows_unstable(*feature) {
@@ -592,6 +594,7 @@ fn parse_version(s: Symbol) -> Option<RustcVersion> {
592594

593595
/// Evaluate a cfg-like condition (with `any` and `all`), using `eval` to
594596
/// evaluate individual items.
597+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
595598
pub fn eval_condition(
596599
cfg: &ast::MetaItem,
597600
sess: &Session,

compiler/rustc_borrowck/src/diagnostics/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10081008
self.borrow_spans(span, borrow.reserve_location)
10091009
}
10101010

1011+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
10111012
fn explain_captures(
10121013
&mut self,
10131014
err: &mut DiagnosticBuilder<'_>,

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+4
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
198198
// For generic associated types (GATs) which implied 'static requirement
199199
// from higher-ranked trait bounds (HRTB). Try to locate span of the trait
200200
// and the span which bounded to the trait for adding 'static lifetime suggestion
201+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
201202
fn suggest_static_lifetime_for_gat_from_hrtb(
202203
&self,
203204
diag: &mut DiagnosticBuilder<'_>,
@@ -821,6 +822,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
821822
/// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
822823
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
823824
/// ```
825+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
824826
fn add_static_impl_trait_suggestion(
825827
&self,
826828
diag: &mut DiagnosticBuilder<'_>,
@@ -971,6 +973,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
971973
self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
972974
}
973975

976+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
974977
#[instrument(skip(self, err), level = "debug")]
975978
fn suggest_constrain_dyn_trait_in_impl(
976979
&self,
@@ -1038,6 +1041,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10381041
suggest_adding_lifetime_params(self.infcx.tcx, sub, ty_sup, ty_sub, diag);
10391042
}
10401043

1044+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
10411045
fn suggest_move_on_borrowing_closure(&self, diag: &mut DiagnosticBuilder<'_>) {
10421046
let map = self.infcx.tcx.hir();
10431047
let body_id = map.body_owned_by(self.mir_def_id());

compiler/rustc_const_eval/src/transform/check_consts/ops.rs

+4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ impl<'tcx> NonConstOp<'tcx> for FloatingPointOp {
6262
}
6363
}
6464

65+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
6566
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
6667
feature_err(
6768
&ccx.tcx.sess,
@@ -556,6 +557,7 @@ impl<'tcx> NonConstOp<'tcx> for RawMutPtrDeref {
556557
Status::Unstable(sym::const_mut_refs)
557558
}
558559

560+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
559561
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
560562
feature_err(
561563
&ccx.tcx.sess,
@@ -589,6 +591,7 @@ impl<'tcx> NonConstOp<'tcx> for StaticAccess {
589591
}
590592
}
591593

594+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
592595
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
593596
let mut err = feature_err(
594597
&ccx.tcx.sess,
@@ -634,6 +637,7 @@ pub mod ty {
634637
}
635638
}
636639

640+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
637641
fn build_error(&self, ccx: &ConstCx<'_, 'tcx>, span: Span) -> DiagnosticBuilder<'tcx> {
638642
feature_err(
639643
&ccx.tcx.sess,

compiler/rustc_driver_impl/src/args.rs

+1
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ impl Expander {
9797
/// **Note:** This function doesn't interpret argument 0 in any special way.
9898
/// If this function is intended to be used with command line arguments,
9999
/// `argv[0]` must be removed prior to calling it manually.
100+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
100101
pub fn arg_expand_all(early_dcx: &EarlyDiagCtxt, at_args: &[String]) -> Vec<String> {
101102
let mut expander = Expander::default();
102103
for arg in at_args {

compiler/rustc_driver_impl/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
//!
55
//! This API is completely unstable and subject to change.
66
7+
#![allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
78
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/nightly-rustc/")]
89
#![doc(rust_logo)]
910
#![feature(rustdoc_internals)]

compiler/rustc_expand/src/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1479,6 +1479,8 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) -> bool {
14791479
};
14801480

14811481
if crate_matches {
1482+
// FIXME: make this translatable
1483+
#[allow(rustc::untranslatable_diagnostic)]
14821484
sess.parse_sess.buffer_lint_with_diagnostic(
14831485
PROC_MACRO_BACK_COMPAT,
14841486
item.ident.span,

compiler/rustc_expand/src/config.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ impl<'a> StripUnconfigured<'a> {
239239
/// Gives a compiler warning when the `cfg_attr` contains no attributes and
240240
/// is in the original source file. Gives a compiler error if the syntax of
241241
/// the attribute is incorrect.
242+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
242243
pub(crate) fn expand_cfg_attr(&self, attr: &Attribute, recursive: bool) -> Vec<Attribute> {
243244
let Some((cfg_predicate, expanded_attrs)) =
244245
rustc_parse::parse_cfg_attr(attr, &self.sess.parse_sess)
@@ -273,6 +274,7 @@ impl<'a> StripUnconfigured<'a> {
273274
}
274275
}
275276

277+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
276278
fn expand_cfg_attr_item(
277279
&self,
278280
attr: &Attribute,
@@ -371,6 +373,7 @@ impl<'a> StripUnconfigured<'a> {
371373
}
372374

373375
/// If attributes are not allowed on expressions, emit an error for `attr`
376+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
374377
#[instrument(level = "trace", skip(self))]
375378
pub(crate) fn maybe_emit_expr_attr_err(&self, attr: &Attribute) {
376379
if self.features.is_some_and(|features| !features.stmt_expr_attributes)
@@ -384,7 +387,6 @@ impl<'a> StripUnconfigured<'a> {
384387
);
385388

386389
if attr.is_doc_comment() {
387-
#[allow(rustc::untranslatable_diagnostic)]
388390
err.help("`///` is for documentation comments. For a plain comment, use `//`.");
389391
}
390392

compiler/rustc_expand/src/expand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
772772
})
773773
}
774774

775+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
775776
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
776777
let kind = match item {
777778
Annotatable::Item(_)
@@ -814,6 +815,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
814815
}
815816

816817
impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
818+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
817819
fn visit_item(&mut self, item: &'ast ast::Item) {
818820
match &item.kind {
819821
ItemKind::Mod(_, mod_kind)
@@ -1678,6 +1680,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
16781680

16791681
// Detect use of feature-gated or invalid attributes on macro invocations
16801682
// since they will not be detected after macro expansion.
1683+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
16811684
fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
16821685
let features = self.cx.ecfg.features;
16831686
let mut attrs = attrs.iter().peekable();

compiler/rustc_expand/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#![feature(associated_type_defaults)]
66
#![feature(if_let_guard)]
77
#![feature(let_chains)]
8+
#![feature(lint_reasons)]
89
#![feature(macro_metavar_expr)]
910
#![feature(map_try_insert)]
1011
#![feature(proc_macro_diagnostic)]

compiler/rustc_expand/src/proc_macro_server.rs

+3
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,9 @@ impl server::FreeFunctions for Rustc<'_, '_> {
514514
DiagnosticBuilder::new(&self.sess().dcx, diagnostic.level.to_internal(), message);
515515
diag.span(MultiSpan::from_spans(diagnostic.spans));
516516
for child in diagnostic.children {
517+
// This message comes from another diagnostic, and we are just reconstructing the
518+
// diagnostic, so there's no need for translation.
519+
#[allow(rustc::untranslatable_diagnostic)]
517520
diag.sub(child.level.to_internal(), child.message, MultiSpan::from_spans(child.spans));
518521
}
519522
diag.emit();

compiler/rustc_expand/src/tests.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ impl<T: Write> Write for Shared<T> {
180180
}
181181
}
182182

183+
#[allow(rustc::untranslatable_diagnostic)] // no translation needed for tests
183184
fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &str) {
184185
create_default_session_globals_then(|| {
185186
let (handler, source_map, output) = create_test_handler();
@@ -194,7 +195,6 @@ fn test_harness(file_text: &str, span_labels: Vec<SpanLabel>, expected_output: &
194195
println!("text: {:?}", source_map.span_to_snippet(span));
195196
}
196197

197-
#[allow(rustc::untranslatable_diagnostic)]
198198
handler.span_err(msp, "foo");
199199

200200
assert!(

compiler/rustc_interface/src/interface.rs

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ pub struct Config {
318318

319319
// JUSTIFICATION: before session exists, only config
320320
#[allow(rustc::bad_opt_access)]
321+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
321322
pub fn run_compiler<R: Send>(config: Config, f: impl FnOnce(&Compiler) -> R + Send) -> R {
322323
trace!("run_compiler");
323324

compiler/rustc_interface/src/util.rs

+3
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
161161
})
162162
}
163163

164+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
164165
fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBackendFn {
165166
fn format_err(e: &(dyn std::error::Error + 'static)) -> String {
166167
e.sources().map(|e| format!(": {e}")).collect()
@@ -238,6 +239,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
238239
})
239240
}
240241

242+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
241243
fn get_codegen_sysroot(
242244
early_dcx: &EarlyDiagCtxt,
243245
maybe_sysroot: &Option<PathBuf>,
@@ -330,6 +332,7 @@ fn get_codegen_sysroot(
330332
}
331333
}
332334

335+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
333336
pub(crate) fn check_attr_crate_type(
334337
sess: &Session,
335338
attrs: &[ast::Attribute],

compiler/rustc_lint/src/context.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -621,12 +621,13 @@ pub trait LintContext {
621621
/// Note that this function should only be called for [`LintExpectationId`]s
622622
/// retrieved from the current lint pass. Buffered or manually created ids can
623623
/// cause ICEs.
624-
#[rustc_lint_diagnostics]
625624
fn fulfill_expectation(&self, expectation: LintExpectationId) {
626625
// We need to make sure that submitted expectation ids are correctly fulfilled suppressed
627626
// and stored between compilation sessions. To not manually do these steps, we simply create
628-
// a dummy diagnostic and emit is as usual, which will be suppressed and stored like a normal
629-
// expected lint diagnostic.
627+
// a dummy diagnostic and emit is as usual, which will be suppressed and stored like a
628+
// normal expected lint diagnostic.
629+
#[allow(rustc::diagnostic_outside_of_impl)]
630+
#[allow(rustc::untranslatable_diagnostic)]
630631
self.sess()
631632
.dcx()
632633
.struct_expect(

0 commit comments

Comments
 (0)