Skip to content

Commit

Permalink
Prettify mismatched types error message in a special case
Browse files Browse the repository at this point in the history
Type parameters are referenced in the error message after the previous
few commits (using span label). But when the main error message already
references the very same type parameter it becomes clumsy. Do not show
the additional label in this case as per code review comment by
@estebank.

Also this contains a small style fix.
  • Loading branch information
dkadashev committed Nov 2, 2019
1 parent 4e10b75 commit 774e60b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/librustc/infer/error_reporting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1194,10 +1194,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
// it's a actual definition. According to the comments (e.g. in
// librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter
// is relied upon by some other code. This might (or might not) need cleanup.
let body_owner_def_id = match self.tcx.hir().opt_local_def_id(cause.body_id) {
Some(def_id) => def_id,
None => self.tcx.hir().body_owner_def_id(hir::BodyId{hir_id: cause.body_id}),
};
let body_owner_def_id = self.tcx.hir().opt_local_def_id(cause.body_id)
.unwrap_or_else(|| {
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
});
self.check_and_note_conflicting_crates(diag, terr, span);
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id);

Expand Down
24 changes: 12 additions & 12 deletions src/librustc/ty/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,14 @@ impl<'tcx> TyCtxt<'tcx> {
},
(ty::Param(expected), ty::Param(found)) => {
let generics = self.generics_of(body_owner_def_id);
db.span_label(
self.def_span(generics.type_param(expected, self).def_id),
"expected type parameter"
);
db.span_label(
self.def_span(generics.type_param(found, self).def_id),
"found type parameter"
);
let e_span = self.def_span(generics.type_param(expected, self).def_id);
if !sp.contains(e_span) {
db.span_label(e_span, "expected type parameter");
}
let f_span = self.def_span(generics.type_param(found, self).def_id);
if !sp.contains(f_span) {
db.span_label(f_span, "found type parameter");
}
db.note("a type parameter was expected, but a different one was found; \
you might be missing a type parameter or trait bound");
db.note("for more information, visit \
Expand All @@ -313,10 +313,10 @@ impl<'tcx> TyCtxt<'tcx> {
}
(ty::Param(p), _) | (_, ty::Param(p)) => {
let generics = self.generics_of(body_owner_def_id);
db.span_label(
self.def_span(generics.type_param(p, self).def_id),
"this type parameter"
);
let p_span = self.def_span(generics.type_param(p, self).def_id);
if !sp.contains(p_span) {
db.span_label(p_span, "this type parameter");
}
db.help("type parameters must be constrained to match other types");
if self.sess.teach(&db.get_code().unwrap()) {
db.help("given a type parameter `T` and a method `foo`:
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/impl-trait/impl-generic-mismatch-ab.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ LL | fn foo<A: Debug>(&self, a: &A, b: &impl Debug);
| -- type in trait
...
LL | fn foo<B: Debug>(&self, a: &impl Debug, b: &B) { }
| - ^----------
| | ||
| | |found type parameter
| | expected type parameter `B`, found type parameter `impl Debug`
| - ^^^^^^^^^^^ expected type parameter `B`, found type parameter `impl Debug`
| |
| expected type parameter
|
= note: expected type `fn(&(), &B, &impl Debug)`
Expand Down

0 comments on commit 774e60b

Please sign in to comment.