Skip to content

Commit

Permalink
Simplify error reporting.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Jan 8, 2022
1 parent 8b2409c commit 554aceb
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 38 deletions.
19 changes: 19 additions & 0 deletions compiler/rustc_resolve/src/diagnostics.rs
Expand Up @@ -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
}
}
}

Expand Down
53 changes: 15 additions & 38 deletions compiler/rustc_resolve/src/late.rs
Expand Up @@ -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]) {
Expand Down
8 changes: 8 additions & 0 deletions compiler/rustc_resolve/src/lib.rs
Expand Up @@ -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> {
Expand Down

0 comments on commit 554aceb

Please sign in to comment.