Skip to content

Commit 4b5a5a4

Browse files
committed
Add translatable diagnostic for various strings in resolve::unresolved_macro_suggestions
1 parent 355a689 commit 4b5a5a4

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

compiler/rustc_resolve/messages.ftl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,3 +271,16 @@ resolve_imports_cannot_refer_to =
271271
272272
resolve_cannot_find_ident_in_this_scope =
273273
cannot find {$expected} `{$ident}` in this scope
274+
275+
resolve_explicit_unsafe_traits =
276+
unsafe traits like `{$ident}` should be implemented explicitly
277+
278+
resolve_added_macro_use =
279+
have you added the `#[macro_use]` on the module/import?
280+
281+
resolve_consider_adding_a_derive =
282+
consider adding a derive
283+
.suggestion = FIXME
284+
285+
resolve_consider_adding_a_derive_enum =
286+
consider adding `#[derive(Default)]` to this enum

compiler/rustc_resolve/src/diagnostics.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +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::{ChangeImportBinding, ChangeImportBindingSuggestion};
33+
use crate::errors::{
34+
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
35+
ConsiderAddingADeriveEnum, ExplicitUnsafeTraits,
36+
};
3437
use crate::imports::{Import, ImportKind};
3538
use crate::late::{PatternSource, Rib};
3639
use crate::path_names_to_string;
@@ -1377,12 +1380,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13771380
);
13781381

13791382
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
1380-
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
1381-
err.span_note(ident.span, msg);
1383+
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
13821384
return;
13831385
}
13841386
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
1385-
err.help("have you added the `#[macro_use]` on the module/import?");
1387+
err.subdiagnostic(AddedMacroUse);
13861388
return;
13871389
}
13881390
if ident.name == kw::Default
@@ -1392,12 +1394,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13921394
let source_map = self.tcx.sess.source_map();
13931395
let head_span = source_map.guess_head_span(span);
13941396
if let Ok(head) = source_map.span_to_snippet(head_span) {
1395-
err.span_suggestion(head_span, "consider adding a derive", format!("#[derive(Default)]\n{head}"), Applicability::MaybeIncorrect);
1397+
err.subdiagnostic(ConsiderAddingADerive {
1398+
span: head_span,
1399+
suggestion: format!("#[derive(Default)]\n{head}")
1400+
});
13961401
} else {
1397-
err.span_help(
1398-
head_span,
1399-
"consider adding `#[derive(Default)]` to this enum",
1400-
);
1402+
err.subdiagnostic(ConsiderAddingADeriveEnum { span: head_span });
14011403
}
14021404
}
14031405
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {

compiler/rustc_resolve/src/errors.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,3 +622,34 @@ pub(crate) struct CannotFindIdentInThisScope<'a> {
622622
pub(crate) expected: &'a str,
623623
pub(crate) ident: Ident,
624624
}
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+
}
649+
650+
#[derive(Subdiagnostic)]
651+
#[help(resolve_consider_adding_a_derive_enum)]
652+
pub(crate) struct ConsiderAddingADeriveEnum {
653+
#[primary_span]
654+
pub(crate) span: Span,
655+
}

0 commit comments

Comments
 (0)