Simplify error reporting.

This commit is contained in:
Camille GILLOT 2022-01-08 14:49:38 +01:00
parent 8b2409cd7d
commit 554aceba49
3 changed files with 42 additions and 38 deletions

View File

@ -602,6 +602,25 @@ impl<'a> Resolver<'a> {
err
}
ResolutionError::TraitImplMismatch {
name,
kind,
code,
trait_item_span,
trait_path,
} => {
let mut err = self.session.struct_span_err_with_code(
span,
&format!(
"item `{}` is an associated {}, which doesn't match its trait `{}`",
name, kind, trait_path,
),
code,
);
err.span_label(span, "does not match trait");
err.span_label(trait_item_span, "item in trait");
err
}
}
}

View File

@ -1470,45 +1470,22 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
// The method kind does not correspond to what appeared in the trait, report.
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
let path = &path_names_to_string(path);
let mut err = match kind {
AssocItemKind::Const(..) => {
rustc_errors::struct_span_err!(
self.r.session,
span,
E0323,
"item `{}` is an associated const, which doesn't match its trait `{}`",
ident,
path,
)
}
AssocItemKind::Fn(..) => {
rustc_errors::struct_span_err!(
self.r.session,
span,
E0324,
"item `{}` is an associated method, which doesn't match its trait `{}`",
ident,
path,
)
}
AssocItemKind::TyAlias(..) => {
rustc_errors::struct_span_err!(
self.r.session,
span,
E0325,
"item `{}` is an associated type, which doesn't match its trait `{}`",
ident,
path,
)
}
AssocItemKind::MacCall(..) => {
span_bug!(span, "macros should have been expanded")
}
let (code, kind) = match kind {
AssocItemKind::Const(..) => (rustc_errors::error_code!(E0323), "const"),
AssocItemKind::Fn(..) => (rustc_errors::error_code!(E0324), "method"),
AssocItemKind::TyAlias(..) => (rustc_errors::error_code!(E0325), "type"),
AssocItemKind::MacCall(..) => span_bug!(span, "unexpanded macro"),
};
err.span_label(span, "does not match trait");
err.span_label(binding.span, "item in trait");
err.emit();
self.report_error(
span,
ResolutionError::TraitImplMismatch {
name: ident.name,
kind,
code,
trait_path: path_names_to_string(path),
trait_item_span: binding.span,
},
);
}
fn resolve_params(&mut self, params: &'ast [Param]) {

View File

@ -258,6 +258,14 @@ enum ResolutionError<'a> {
SelfInGenericParamDefault,
/// Error E0767: use of unreachable label
UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option<LabelSuggestion> },
/// Error E0323, E0324, E0325: mismatch between trait item and impl item.
TraitImplMismatch {
name: Symbol,
kind: &'static str,
trait_path: String,
trait_item_span: Span,
code: rustc_errors::DiagnosticId,
},
}
enum VisResolutionError<'a> {