Skip to content

Commit f8a3c6a

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. Finally, it also: - Expands and corrects some comments. - Does some minor formatting improvements. - Adds missing `DecorateLint` cases to `tests/ui-fulldeps/internal-lints/diagnostics.rs`.
1 parent d6a4931 commit f8a3c6a

File tree

35 files changed

+253
-60
lines changed

35 files changed

+253
-60
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
@@ -1499,6 +1499,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
14991499
}
15001500
}
15011501

1502+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
15021503
fn lower_expr_yield(&mut self, span: Span, opt_expr: Option<&Expr>) -> hir::ExprKind<'hir> {
15031504
let is_async_gen = match self.coroutine_kind {
15041505
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
@@ -201,6 +201,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
201201
// For generic associated types (GATs) which implied 'static requirement
202202
// from higher-ranked trait bounds (HRTB). Try to locate span of the trait
203203
// and the span which bounded to the trait for adding 'static lifetime suggestion
204+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
204205
fn suggest_static_lifetime_for_gat_from_hrtb(
205206
&self,
206207
diag: &mut DiagnosticBuilder<'_>,
@@ -825,6 +826,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
825826
/// LL | fn iter_values_anon(&self) -> impl Iterator<Item=u32> + 'a {
826827
/// | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
827828
/// ```
829+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
828830
fn add_static_impl_trait_suggestion(
829831
&self,
830832
diag: &mut DiagnosticBuilder<'_>,
@@ -975,6 +977,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
975977
self.suggest_constrain_dyn_trait_in_impl(diag, &visitor.0, ident, self_ty);
976978
}
977979

980+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
978981
#[instrument(skip(self, err), level = "debug")]
979982
fn suggest_constrain_dyn_trait_in_impl(
980983
&self,
@@ -1042,6 +1045,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
10421045
suggest_adding_lifetime_params(self.infcx.tcx, sub, ty_sup, ty_sub, diag);
10431046
}
10441047

1048+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
10451049
fn suggest_move_on_borrowing_closure(&self, diag: &mut DiagnosticBuilder<'_>) {
10461050
let map = self.infcx.tcx.hir();
10471051
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
@@ -1487,6 +1487,8 @@ fn pretty_printing_compatibility_hack(item: &Item, sess: &Session) -> bool {
14871487
};
14881488

14891489
if crate_matches {
1490+
// FIXME: make this translatable
1491+
#[allow(rustc::untranslatable_diagnostic)]
14901492
sess.parse_sess.buffer_lint_with_diagnostic(
14911493
PROC_MACRO_BACK_COMPAT,
14921494
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
@@ -788,6 +788,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
788788
})
789789
}
790790

791+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
791792
fn gate_proc_macro_attr_item(&self, span: Span, item: &Annotatable) {
792793
let kind = match item {
793794
Annotatable::Item(_)
@@ -830,6 +831,7 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
830831
}
831832

832833
impl<'ast, 'a> Visitor<'ast> for GateProcMacroInput<'a> {
834+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
833835
fn visit_item(&mut self, item: &'ast ast::Item) {
834836
match &item.kind {
835837
ItemKind::Mod(_, mod_kind)
@@ -1694,6 +1696,7 @@ impl<'a, 'b> InvocationCollector<'a, 'b> {
16941696

16951697
// Detect use of feature-gated or invalid attributes on macro invocations
16961698
// since they will not be detected after macro expansion.
1699+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
16971700
fn check_attributes(&self, attrs: &[ast::Attribute], call: &ast::MacCall) {
16981701
let features = self.cx.ecfg.features;
16991702
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
@@ -517,6 +517,9 @@ impl server::FreeFunctions for Rustc<'_, '_> {
517517
DiagnosticBuilder::new(&self.sess().dcx, diagnostic.level.to_internal(), message);
518518
diag.span(MultiSpan::from_spans(diagnostic.spans));
519519
for child in diagnostic.children {
520+
// This message comes from another diagnostic, and we are just reconstructing the
521+
// diagnostic, so there's no need for translation.
522+
#[allow(rustc::untranslatable_diagnostic)]
520523
diag.sub(child.level.to_internal(), child.message, MultiSpan::from_spans(child.spans));
521524
}
522525
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_feature/src/builtin_attrs.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,8 @@ pub const BUILTIN_ATTRIBUTES: &[BuiltinAttribute] = &[
650650
// might not be stable during incremental compilation.
651651
rustc_attr!(rustc_lint_query_instability, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
652652
// Used by the `rustc::untranslatable_diagnostic` and `rustc::diagnostic_outside_of_impl` lints
653-
// to assist in changes to diagnostic APIs.
653+
// to assist in changes to diagnostic APIs. Any function with this attribute will be checked by
654+
// those lints.
654655
rustc_attr!(rustc_lint_diagnostics, Normal, template!(Word), WarnFollowing, INTERNAL_UNSTABLE),
655656
// Used by the `rustc::bad_opt_access` lint to identify `DebuggingOptions` and `CodegenOptions`
656657
// types (as well as any others in future).

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
@@ -160,6 +160,7 @@ pub(crate) fn run_in_thread_pool_with_globals<F: FnOnce() -> R + Send, R: Send>(
160160
})
161161
}
162162

163+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
163164
fn load_backend_from_dylib(early_dcx: &EarlyDiagCtxt, path: &Path) -> MakeBackendFn {
164165
match unsafe { load_symbol_from_dylib::<MakeBackendFn>(path, "__rustc_codegen_backend") } {
165166
Ok(backend_sym) => backend_sym,
@@ -227,6 +228,7 @@ fn get_rustc_path_inner(bin_path: &str) -> Option<PathBuf> {
227228
})
228229
}
229230

231+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
230232
fn get_codegen_sysroot(
231233
early_dcx: &EarlyDiagCtxt,
232234
maybe_sysroot: &Option<PathBuf>,
@@ -319,6 +321,7 @@ fn get_codegen_sysroot(
319321
}
320322
}
321323

324+
#[allow(rustc::untranslatable_diagnostic)] // FIXME: make this translatable
322325
pub(crate) fn check_attr_crate_type(
323326
sess: &Session,
324327
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 it 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)