1
Fork 0

Add translatable diagnostic for various strings in resolve::unresolved_macro_suggestions

This commit is contained in:
Tom Martin 2023-06-18 12:26:31 +01:00
parent 355a689542
commit 4b5a5a4529
No known key found for this signature in database
GPG key ID: 73A733F9629F5AC5
3 changed files with 55 additions and 9 deletions

View file

@ -271,3 +271,16 @@ resolve_imports_cannot_refer_to =
resolve_cannot_find_ident_in_this_scope =
cannot find {$expected} `{$ident}` in this scope
resolve_explicit_unsafe_traits =
unsafe traits like `{$ident}` should be implemented explicitly
resolve_added_macro_use =
have you added the `#[macro_use]` on the module/import?
resolve_consider_adding_a_derive =
consider adding a derive
.suggestion = FIXME
resolve_consider_adding_a_derive_enum =
consider adding `#[derive(Default)]` to this enum

View file

@ -30,7 +30,10 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Span, SyntaxContext};
use thin_vec::ThinVec;
use crate::errors::{ChangeImportBinding, ChangeImportBindingSuggestion};
use crate::errors::{
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
ConsiderAddingADeriveEnum, ExplicitUnsafeTraits,
};
use crate::imports::{Import, ImportKind};
use crate::late::{PatternSource, Rib};
use crate::path_names_to_string;
@ -1377,12 +1380,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
);
if macro_kind == MacroKind::Derive && (ident.name == sym::Send || ident.name == sym::Sync) {
let msg = format!("unsafe traits like `{}` should be implemented explicitly", ident);
err.span_note(ident.span, msg);
err.subdiagnostic(ExplicitUnsafeTraits { span: ident.span, ident });
return;
}
if self.macro_names.contains(&ident.normalize_to_macros_2_0()) {
err.help("have you added the `#[macro_use]` on the module/import?");
err.subdiagnostic(AddedMacroUse);
return;
}
if ident.name == kw::Default
@ -1392,12 +1394,12 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let source_map = self.tcx.sess.source_map();
let head_span = source_map.guess_head_span(span);
if let Ok(head) = source_map.span_to_snippet(head_span) {
err.span_suggestion(head_span, "consider adding a derive", format!("#[derive(Default)]\n{head}"), Applicability::MaybeIncorrect);
err.subdiagnostic(ConsiderAddingADerive {
span: head_span,
suggestion: format!("#[derive(Default)]\n{head}")
});
} else {
err.span_help(
head_span,
"consider adding `#[derive(Default)]` to this enum",
);
err.subdiagnostic(ConsiderAddingADeriveEnum { span: head_span });
}
}
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {

View file

@ -622,3 +622,34 @@ pub(crate) struct CannotFindIdentInThisScope<'a> {
pub(crate) expected: &'a str,
pub(crate) ident: Ident,
}
#[derive(Subdiagnostic)]
#[note(resolve_explicit_unsafe_traits)]
pub(crate) struct ExplicitUnsafeTraits {
#[primary_span]
pub(crate) span: Span,
pub(crate) ident: Ident,
}
#[derive(Subdiagnostic)]
#[help(resolve_added_macro_use)]
pub(crate) struct AddedMacroUse;
#[derive(Subdiagnostic)]
#[suggestion(
resolve_consider_adding_a_derive,
code = "{suggestion}",
applicability = "maybe-incorrect"
)]
pub(crate) struct ConsiderAddingADerive {
#[primary_span]
pub(crate) span: Span,
pub(crate) suggestion: String,
}
#[derive(Subdiagnostic)]
#[help(resolve_consider_adding_a_derive_enum)]
pub(crate) struct ConsiderAddingADeriveEnum {
#[primary_span]
pub(crate) span: Span,
}