Prettify mismatched types error message in a special case

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.
This commit is contained in:
Dmitry Kadashev 2019-11-02 14:51:10 +07:00
parent 4e10b75951
commit 774e60b0c1
3 changed files with 18 additions and 20 deletions

View File

@ -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);

View File

@ -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 \
@ -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`:

View File

@ -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)`