Skip to content

Commit

Permalink
Rollup merge of rust-lang#112768 - NotStirred:translatable_diag/resol…
Browse files Browse the repository at this point in the history
…ve1, 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?
  • Loading branch information
matthiaskrgr authored Jun 19, 2023
2 parents 33beedb + 2027e98 commit e6ccb11
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 25 deletions.
18 changes: 18 additions & 0 deletions compiler/rustc_resolve/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ resolve_add_as_non_derive =
add as non-Derive macro
`#[{$macro_path}]`
resolve_added_macro_use =
have you added the `#[macro_use]` on the module/import?
resolve_ampersand_used_without_explicit_lifetime_name =
`&` without an explicit lifetime name cannot be used here
.note = explicit lifetime name needed here
Expand Down Expand Up @@ -45,9 +48,18 @@ resolve_cannot_capture_dynamic_environment_in_fn_item =
can't capture dynamic environment in a fn item
.help = use the `|| {"{"} ... {"}"}` closure form instead
resolve_cannot_find_ident_in_this_scope =
cannot find {$expected} `{$ident}` in this scope
resolve_cannot_use_self_type_here =
can't use `Self` here
resolve_change_import_binding =
you can use `as` to change the binding name of the import
resolve_consider_adding_a_derive =
consider adding a derive
resolve_const_not_member_of_trait =
const `{$const_}` is not a member of trait `{$trait_}`
.label = not a member of trait `{$trait_}`
Expand All @@ -74,6 +86,9 @@ resolve_expected_found =
expected module, found {$res} `{$path_str}`
.label = not a module
resolve_explicit_unsafe_traits =
unsafe traits like `{$ident}` should be implemented explicitly
resolve_forward_declared_generic_param =
generic parameters with a default cannot use forward declared identifiers
.label = defaulted generic parameters cannot be forward declared
Expand All @@ -96,6 +111,9 @@ resolve_ident_bound_more_than_once_in_same_pattern =
resolve_imported_crate = `$crate` may not be imported
resolve_imports_cannot_refer_to =
imports cannot refer to {$what}
resolve_indeterminate =
cannot determine resolution for the visibility
Expand Down
31 changes: 12 additions & 19 deletions compiler/rustc_resolve/src/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ use rustc_span::symbol::{kw, sym, Ident, Symbol};
use rustc_span::{BytePos, Span, SyntaxContext};
use thin_vec::ThinVec;

use crate::errors::{
AddedMacroUse, ChangeImportBinding, ChangeImportBindingSuggestion, ConsiderAddingADerive,
ExplicitUnsafeTraits,
};
use crate::imports::{Import, ImportKind};
use crate::late::{PatternSource, Rib};
use crate::path_names_to_string;
Expand Down Expand Up @@ -376,16 +380,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
_ => unreachable!(),
}

let rename_msg = "you can use `as` to change the binding name of the import";
if let Some(suggestion) = suggestion {
err.span_suggestion(
binding_span,
rename_msg,
suggestion,
Applicability::MaybeIncorrect,
);
err.subdiagnostic(ChangeImportBindingSuggestion { span: binding_span, suggestion });
} else {
err.span_label(binding_span, rename_msg);
err.subdiagnostic(ChangeImportBinding { span: binding_span });
}
}

Expand Down Expand Up @@ -1382,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
Expand All @@ -1396,14 +1393,10 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
let span = self.def_span(def_id);
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);
} else {
err.span_help(
head_span,
"consider adding `#[derive(Default)]` to this enum",
);
}
err.subdiagnostic(ConsiderAddingADerive {
span: head_span.shrink_to_lo(),
suggestion: format!("#[derive(Default)]\n")
});
}
for ns in [Namespace::MacroNS, Namespace::TypeNS, Namespace::ValueNS] {
if let Ok(binding) = self.early_resolve_ident_in_lexical_scope(
Expand Down
60 changes: 60 additions & 0 deletions compiler/rustc_resolve/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,63 @@ pub(crate) enum ParamKindInEnumDiscriminant {
#[note(resolve_lifetime_param_in_enum_discriminant)]
Lifetime,
}

#[derive(Subdiagnostic)]
#[label(resolve_change_import_binding)]
pub(crate) struct ChangeImportBinding {
#[primary_span]
pub(crate) span: Span,
}

#[derive(Subdiagnostic)]
#[suggestion(
resolve_change_import_binding,
code = "{suggestion}",
applicability = "maybe-incorrect"
)]
pub(crate) struct ChangeImportBindingSuggestion {
#[primary_span]
pub(crate) span: Span,
pub(crate) suggestion: String,
}

#[derive(Diagnostic)]
#[diag(resolve_imports_cannot_refer_to)]
pub(crate) struct ImportsCannotReferTo<'a> {
#[primary_span]
pub(crate) span: Span,
pub(crate) what: &'a str,
}

#[derive(Diagnostic)]
#[diag(resolve_cannot_find_ident_in_this_scope)]
pub(crate) struct CannotFindIdentInThisScope<'a> {
#[primary_span]
pub(crate) span: Span,
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,
}
6 changes: 4 additions & 2 deletions compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! If you wonder why there's no `early.rs`, that's because it's split into three files -
//! `build_reduced_graph.rs`, `macros.rs` and `imports.rs`.

use crate::errors::ImportsCannotReferTo;
use crate::BindingKey;
use crate::{path_names_to_string, rustdoc, BindingError, Finalize, LexicalScopeBinding};
use crate::{Module, ModuleOrUniformRoot, NameBinding, ParentScope, PathResult};
Expand Down Expand Up @@ -2244,12 +2245,13 @@ impl<'a: 'ast, 'b, 'ast, 'tcx> LateResolutionVisitor<'a, 'b, 'ast, 'tcx> {
_ => &[TypeNS],
};
let report_error = |this: &Self, ns| {
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
if this.should_report_errs() {
let what = if ns == TypeNS { "type parameters" } else { "local variables" };
this.r
.tcx
.sess
.span_err(ident.span, format!("imports cannot refer to {}", what));
.create_err(ImportsCannotReferTo { span: ident.span, what })
.emit();
}
};

Expand Down
13 changes: 10 additions & 3 deletions compiler/rustc_resolve/src/macros.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! A bunch of methods and structures more or less related to resolving macros and
//! interface provided by `Resolver` to macro expander.

use crate::errors::{self, AddAsNonDerive, MacroExpectedFound, RemoveSurroundingDerive};
use crate::errors::{
self, AddAsNonDerive, CannotFindIdentInThisScope, MacroExpectedFound, RemoveSurroundingDerive,
};
use crate::Namespace::*;
use crate::{BuiltinMacroState, Determinacy};
use crate::{DeriveData, Finalize, ParentScope, ResolutionError, Resolver, ScopeSet};
Expand Down Expand Up @@ -793,8 +795,13 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
}
Err(..) => {
let expected = kind.descr_expected();
let msg = format!("cannot find {} `{}` in this scope", expected, ident);
let mut err = self.tcx.sess.struct_span_err(ident.span, msg);

let mut err = self.tcx.sess.create_err(CannotFindIdentInThisScope {
span: ident.span,
expected,
ident,
});

self.unresolved_macro_suggestions(&mut err, kind, &parent_scope, ident, krate);
err.emit();
}
Expand Down
2 changes: 1 addition & 1 deletion tests/ui/enum/suggest-default-attribute.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ LL | #[default]
help: consider adding a derive
|
LL + #[derive(Default)]
LL ~ pub enum Test {
LL | pub enum Test {
|

error: aborting due to previous error
Expand Down

0 comments on commit e6ccb11

Please sign in to comment.