Skip to content

Commit 554aceb

Browse files
committed
Simplify error reporting.
1 parent 8b2409c commit 554aceb

File tree

3 files changed

+42
-38
lines changed

3 files changed

+42
-38
lines changed

compiler/rustc_resolve/src/diagnostics.rs

+19
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,25 @@ impl<'a> Resolver<'a> {
602602

603603
err
604604
}
605+
ResolutionError::TraitImplMismatch {
606+
name,
607+
kind,
608+
code,
609+
trait_item_span,
610+
trait_path,
611+
} => {
612+
let mut err = self.session.struct_span_err_with_code(
613+
span,
614+
&format!(
615+
"item `{}` is an associated {}, which doesn't match its trait `{}`",
616+
name, kind, trait_path,
617+
),
618+
code,
619+
);
620+
err.span_label(span, "does not match trait");
621+
err.span_label(trait_item_span, "item in trait");
622+
err
623+
}
605624
}
606625
}
607626

compiler/rustc_resolve/src/late.rs

+15-38
Original file line numberDiff line numberDiff line change
@@ -1470,45 +1470,22 @@ impl<'a: 'ast, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
14701470

14711471
// The method kind does not correspond to what appeared in the trait, report.
14721472
let path = &self.current_trait_ref.as_ref().unwrap().1.path;
1473-
let path = &path_names_to_string(path);
1474-
let mut err = match kind {
1475-
AssocItemKind::Const(..) => {
1476-
rustc_errors::struct_span_err!(
1477-
self.r.session,
1478-
span,
1479-
E0323,
1480-
"item `{}` is an associated const, which doesn't match its trait `{}`",
1481-
ident,
1482-
path,
1483-
)
1484-
}
1485-
AssocItemKind::Fn(..) => {
1486-
rustc_errors::struct_span_err!(
1487-
self.r.session,
1488-
span,
1489-
E0324,
1490-
"item `{}` is an associated method, which doesn't match its trait `{}`",
1491-
ident,
1492-
path,
1493-
)
1494-
}
1495-
AssocItemKind::TyAlias(..) => {
1496-
rustc_errors::struct_span_err!(
1497-
self.r.session,
1498-
span,
1499-
E0325,
1500-
"item `{}` is an associated type, which doesn't match its trait `{}`",
1501-
ident,
1502-
path,
1503-
)
1504-
}
1505-
AssocItemKind::MacCall(..) => {
1506-
span_bug!(span, "macros should have been expanded")
1507-
}
1473+
let (code, kind) = match kind {
1474+
AssocItemKind::Const(..) => (rustc_errors::error_code!(E0323), "const"),
1475+
AssocItemKind::Fn(..) => (rustc_errors::error_code!(E0324), "method"),
1476+
AssocItemKind::TyAlias(..) => (rustc_errors::error_code!(E0325), "type"),
1477+
AssocItemKind::MacCall(..) => span_bug!(span, "unexpanded macro"),
15081478
};
1509-
err.span_label(span, "does not match trait");
1510-
err.span_label(binding.span, "item in trait");
1511-
err.emit();
1479+
self.report_error(
1480+
span,
1481+
ResolutionError::TraitImplMismatch {
1482+
name: ident.name,
1483+
kind,
1484+
code,
1485+
trait_path: path_names_to_string(path),
1486+
trait_item_span: binding.span,
1487+
},
1488+
);
15121489
}
15131490

15141491
fn resolve_params(&mut self, params: &'ast [Param]) {

compiler/rustc_resolve/src/lib.rs

+8
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ enum ResolutionError<'a> {
258258
SelfInGenericParamDefault,
259259
/// Error E0767: use of unreachable label
260260
UnreachableLabel { name: Symbol, definition_span: Span, suggestion: Option<LabelSuggestion> },
261+
/// Error E0323, E0324, E0325: mismatch between trait item and impl item.
262+
TraitImplMismatch {
263+
name: Symbol,
264+
kind: &'static str,
265+
trait_path: String,
266+
trait_item_span: Span,
267+
code: rustc_errors::DiagnosticId,
268+
},
261269
}
262270

263271
enum VisResolutionError<'a> {

0 commit comments

Comments
 (0)