Skip to content

Commit 6de869f

Browse files
authored
Rollup merge of #112768 - NotStirred:translatable_diag/resolve1, r=WaffleLapkin
Rewrite various resolve/diagnostics errors as translatable diagnostics additional question: For trivial strings is it ever accepted to use `fluent_generated::foo` in a `label` for example? Or is an empty struct `Diagnostic` preferred?
2 parents 7d072fa + 2027e98 commit 6de869f

File tree

6 files changed

+105
-25
lines changed

6 files changed

+105
-25
lines changed

compiler/rustc_resolve/messages.ftl

+18
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ resolve_add_as_non_derive =
55
add as non-Derive macro
66
`#[{$macro_path}]`
77
8+
resolve_added_macro_use =
9+
have you added the `#[macro_use]` on the module/import?
10+
811
resolve_ampersand_used_without_explicit_lifetime_name =
912
`&` without an explicit lifetime name cannot be used here
1013
.note = explicit lifetime name needed here
@@ -45,9 +48,18 @@ resolve_cannot_capture_dynamic_environment_in_fn_item =
4548
can't capture dynamic environment in a fn item
4649
.help = use the `|| {"{"} ... {"}"}` closure form instead
4750
51+
resolve_cannot_find_ident_in_this_scope =
52+
cannot find {$expected} `{$ident}` in this scope
53+
4854
resolve_cannot_use_self_type_here =
4955
can't use `Self` here
5056
57+
resolve_change_import_binding =
58+
you can use `as` to change the binding name of the import
59+
60+
resolve_consider_adding_a_derive =
61+
consider adding a derive
62+
5163
resolve_const_not_member_of_trait =
5264
const `{$const_}` is not a member of trait `{$trait_}`
5365
.label = not a member of trait `{$trait_}`
@@ -74,6 +86,9 @@ resolve_expected_found =
7486
expected module, found {$res} `{$path_str}`
7587
.label = not a module
7688
89+
resolve_explicit_unsafe_traits =
90+
unsafe traits like `{$ident}` should be implemented explicitly
91+
7792
resolve_forward_declared_generic_param =
7893
generic parameters with a default cannot use forward declared identifiers
7994
.label = defaulted generic parameters cannot be forward declared
@@ -96,6 +111,9 @@ resolve_ident_bound_more_than_once_in_same_pattern =
96111
97112
resolve_imported_crate = `$crate` may not be imported
98113
114+
resolve_imports_cannot_refer_to =
115+
imports cannot refer to {$what}
116+
99117
resolve_indeterminate =
100118
cannot determine resolution for the visibility
101119

compiler/rustc_resolve/src/diagnostics.rs

+12-19
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
3030
use rustc_span::{BytePos, Span, SyntaxContext};
3131
use thin_vec::ThinVec;
3232

33+
use crate::errors::{
34+
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
35+
ExplicitUnsafeTraits,
36+
};
3337
use crate::imports::{Import, ImportKind};
3438
use crate::late::{PatternSource, Rib};
3539
use crate::path_names_to_string;
@@ -376,16 +380,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
376380
_ => unreachable!(),
377381
}
378382

379-
let rename_msg = "you can use `as` to change the binding name of the import";
380383
if let Some(suggestion) = suggestion {
381-
err.span_suggestion(
382-
binding_span,
383-
rename_msg,
384-
suggestion,
385-
Applicability::MaybeIncorrect,
386-
);
384+
err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion });
387385
} else {
388-
err.span_label(binding_span, rename_msg);
386+
err.subdiagnostic(ChangeImportBinding { span: binding_span });
389387
}
390388
}
391389

@@ -1382,12 +1380,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13821380
);
13831381

13841382
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
1385-
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
1386-
err.span_note(ident.span, msg);
1383+
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
13871384
return;
13881385
}
13891386
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
1390-
err.help("have you added the `#[macro_use]` on the module/import?");
1387+
err.subdiagnostic(AddedMacroUse);
13911388
return;
13921389
}
13931390
if ident.name == kw::Default
@@ -1396,14 +1393,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13961393
let span = self.def_span(def_id);
13971394
let source_map = self.tcx.sess.source_map();
13981395
let head_span = source_map.guess_head_span(span);
1399-
if let Ok(head) = source_map.span_to_snippet(head_span) {
1400-
err.span_suggestion(head_span, "consider adding a derive", format!("#[derive(Default)]\n{head}"), Applicability::MaybeIncorrect);
1401-
} else {
1402-
err.span_help(
1403-
head_span,
1404-
"consider adding `#[derive(Default)]` to this enum",
1405-
);
1406-
}
1396+
err.subdiagnostic(ConsiderAddingADerive {
1397+
span: head_span.shrink_to_lo(),
1398+
suggestion: format!("#[derive(Default)]\n")
1399+
});
14071400
}
14081401
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
14091402
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(

compiler/rustc_resolve/src/errors.rs

+60
Original file line numberDiff line numberDiff line change
@@ -586,3 +586,63 @@ pub(crate) enum ParamKindInEnumDiscriminant {
586586
#[note(resolve_lifetime_param_in_enum_discriminant)]
587587
Lifetime,
588588
}
589+
590+
#[derive(Subdiagnostic)]
591+
#[label(resolve_change_import_binding)]
592+
pub(crate) struct ChangeImportBinding {
593+
#[primary_span]
594+
pub(crate) span: Span,
595+
}
596+
597+
#[derive(Subdiagnostic)]
598+
#[suggestion(
599+
resolve_change_import_binding,
600+
code = "{suggestion}",
601+
applicability = "maybe-incorrect"
602+
)]
603+
pub(crate) struct ChangeImportBindingSuggestion {
604+
#[primary_span]
605+
pub(crate) span: Span,
606+
pub(crate) suggestion: String,
607+
}
608+
609+
#[derive(Diagnostic)]
610+
#[diag(resolve_imports_cannot_refer_to)]
611+
pub(crate) struct ImportsCannotReferTo<'a> {
612+
#[primary_span]
613+
pub(crate) span: Span,
614+
pub(crate) what: &'a str,
615+
}
616+
617+
#[derive(Diagnostic)]
618+
#[diag(resolve_cannot_find_ident_in_this_scope)]
619+
pub(crate) struct CannotFindIdentInThisScope<'a> {
620+
#[primary_span]
621+
pub(crate) span: Span,
622+
pub(crate) expected: &'a str,
623+
pub(crate) ident: Ident,
624+
}
625+
626+
#[derive(Subdiagnostic)]
627+
#[note(resolve_explicit_unsafe_traits)]
628+
pub(crate) struct ExplicitUnsafeTraits {
629+
#[primary_span]
630+
pub(crate) span: Span,
631+
pub(crate) ident: Ident,
632+
}
633+
634+
#[derive(Subdiagnostic)]
635+
#[help(resolve_added_macro_use)]
636+
pub(crate) struct AddedMacroUse;
637+
638+
#[derive(Subdiagnostic)]
639+
#[suggestion(
640+
resolve_consider_adding_a_derive,
641+
code = "{suggestion}",
642+
applicability = "maybe-incorrect"
643+
)]
644+
pub(crate) struct ConsiderAddingADerive {
645+
#[primary_span]
646+
pub(crate) span: Span,
647+
pub(crate) suggestion: String,
648+
}

compiler/rustc_resolve/src/late.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
//! If you wonder why there's no `early.rs`, that's because it's split into three files -
77
//! `build_reduced_graph.rs`, `macros.rs` and `imports.rs`.
88
9+
use crate::errors::ImportsCannotReferTo;
910
use crate::BindingKey;
1011
use crate::{path_names_to_string, rustdoc, BindingError, Finalize, LexicalScopeBinding};
1112
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
@@ -2244,12 +2245,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
22442245
_ => &[TypeNS],
22452246
};
22462247
let report_error = |this: &Self, ns| {
2247-
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
22482248
if this.should_report_errs() {
2249+
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
22492250
this.r
22502251
.tcx
22512252
.sess
2252-
.span_err(ident.span, format!("imports cannot refer to {}", what));
2253+
.create_err(ImportsCannotReferTo { span: ident.span, what })
2254+
.emit();
22532255
}
22542256
};
22552257

compiler/rustc_resolve/src/macros.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
//! A bunch of methods and structures more or less related to resolving macros and
22
//! interface provided by `Resolver` to macro expander.
33
4-
use crate::errors::{self, AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
4+
use crate::errors::{
5+
self, AddAsNonDerive, CannotFindIdentInThisScope, MacroExpectedFound, RemoveSurroundingDerive,
6+
};
57
use crate::Namespace::*;
68
use crate::{BuiltinMacroState, Determinacy};
79
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
@@ -793,8 +795,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
793795
}
794796
Err(..) => {
795797
let expected = kind.descr_expected();
796-
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
797-
let mut err = self.tcx.sess.struct_span_err(ident.span, msg);
798+
799+
let mut err = self.tcx.sess.create_err(CannotFindIdentInThisScope {
800+
span: ident.span,
801+
expected,
802+
ident,
803+
});
804+
798805
self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
799806
err.emit();
800807
}

tests/ui/enum/suggest-default-attribute.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | #[default]
77
help: consider adding a derive
88
|
99
LL + #[derive(Default)]
10-
LL ~ pub enum Test {
10+
LL | pub enum Test {
1111
|
1212

1313
error: aborting due to previous error

0 commit comments

Comments
 (0)