From 554aceba49c527d344bc3acab853ee58d0eff5d3 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Sat, 8 Jan 2022 14:49:38 +0100 Subject: [PATCH] Simplify error reporting. --- compiler/rustc_resolve/src/diagnostics.rs | 19 ++++++++ compiler/rustc_resolve/src/late.rs | 53 +++++++---------------- compiler/rustc_resolve/src/lib.rs | 8 ++++ 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/compiler/rustc_resolve/src/diagnostics.rs b/compiler/rustc_resolve/src/diagnostics.rs index 4feeae5cab1de..941c2a70d11ef 100644 --- a/compiler/rustc_resolve/src/diagnostics.rs +++ b/compiler/rustc_resolve/src/diagnostics.rs @@ -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 + } } } diff --git a/compiler/rustc_resolve/src/late.rs b/compiler/rustc_resolve/src/late.rs index 24ff04b8853d4..a15c08da04b21 100644 --- a/compiler/rustc_resolve/src/late.rs +++ b/compiler/rustc_resolve/src/late.rs @@ -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]) { diff --git a/compiler/rustc_resolve/src/lib.rs b/compiler/rustc_resolve/src/lib.rs index b46a93c06734b..17f3af317840f 100644 --- a/compiler/rustc_resolve/src/lib.rs +++ b/compiler/rustc_resolve/src/lib.rs @@ -258,6 +258,14 @@ enum ResolutionError<'a> { SelfInGenericParamDefault, /// Error E0767: use of unreachable label UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option }, + /// 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> {