Skip to content

Commit 774e60b

Browse files
committed
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.
1 parent 4e10b75 commit 774e60b

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/librustc/infer/error_reporting/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1194,10 +1194,10 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
11941194
// it's a actual definition. According to the comments (e.g. in
11951195
// librustc_typeck/check/compare_method.rs:compare_predicate_entailment) the latter
11961196
// is relied upon by some other code. This might (or might not) need cleanup.
1197-
let body_owner_def_id = match self.tcx.hir().opt_local_def_id(cause.body_id) {
1198-
Some(def_id) => def_id,
1199-
None => self.tcx.hir().body_owner_def_id(hir::BodyId{hir_id: cause.body_id}),
1200-
};
1197+
let body_owner_def_id = self.tcx.hir().opt_local_def_id(cause.body_id)
1198+
.unwrap_or_else(|| {
1199+
self.tcx.hir().body_owner_def_id(hir::BodyId { hir_id: cause.body_id })
1200+
});
12011201
self.check_and_note_conflicting_crates(diag, terr, span);
12021202
self.tcx.note_and_explain_type_err(diag, terr, span, body_owner_def_id);
12031203

src/librustc/ty/error.rs

+12-12
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ impl<'tcx> TyCtxt<'tcx> {
291291
},
292292
(ty::Param(expected), ty::Param(found)) => {
293293
let generics = self.generics_of(body_owner_def_id);
294-
db.span_label(
295-
self.def_span(generics.type_param(expected, self).def_id),
296-
"expected type parameter"
297-
);
298-
db.span_label(
299-
self.def_span(generics.type_param(found, self).def_id),
300-
"found type parameter"
301-
);
294+
let e_span = self.def_span(generics.type_param(expected, self).def_id);
295+
if !sp.contains(e_span) {
296+
db.span_label(e_span, "expected type parameter");
297+
}
298+
let f_span = self.def_span(generics.type_param(found, self).def_id);
299+
if !sp.contains(f_span) {
300+
db.span_label(f_span, "found type parameter");
301+
}
302302
db.note("a type parameter was expected, but a different one was found; \
303303
you might be missing a type parameter or trait bound");
304304
db.note("for more information, visit \
@@ -313,10 +313,10 @@ impl<'tcx> TyCtxt<'tcx> {
313313
}
314314
(ty::Param(p), _) | (_, ty::Param(p)) => {
315315
let generics = self.generics_of(body_owner_def_id);
316-
db.span_label(
317-
self.def_span(generics.type_param(p, self).def_id),
318-
"this type parameter"
319-
);
316+
let p_span = self.def_span(generics.type_param(p, self).def_id);
317+
if !sp.contains(p_span) {
318+
db.span_label(p_span, "this type parameter");
319+
}
320320
db.help("type parameters must be constrained to match other types");
321321
if self.sess.teach(&db.get_code().unwrap()) {
322322
db.help("given a type parameter `T` and a method `foo`:

src/test/ui/impl-trait/impl-generic-mismatch-ab.stderr

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ LL | fn foo<A: Debug>(&self, a: &A, b: &impl Debug);
55
| -- type in trait
66
...
77
LL | fn foo<B: Debug>(&self, a: &impl Debug, b: &B) { }
8-
| - ^----------
9-
| | ||
10-
| | |found type parameter
11-
| | expected type parameter `B`, found type parameter `impl Debug`
8+
| - ^^^^^^^^^^^ expected type parameter `B`, found type parameter `impl Debug`
9+
| |
1210
| expected type parameter
1311
|
1412
= note: expected type `fn(&(), &B, &impl Debug)`

0 commit comments

Comments
 (0)