Skip to content

Commit 0ced102

Browse files
committed
Tweak spans and labels
1 parent 3540312 commit 0ced102

34 files changed

+93
-189
lines changed

compiler/rustc_infer/src/infer/error_reporting/mod.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1428,6 +1428,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
14281428
secondary_span: Option<(Span, String)>,
14291429
mut values: Option<ValuePairs<'tcx>>,
14301430
terr: &TypeError<'tcx>,
1431+
swap_secondary_and_primary: bool,
14311432
) {
14321433
let span = cause.span(self.tcx);
14331434
debug!("note_type_err cause={:?} values={:?}, terr={:?}", cause, values, terr);
@@ -1582,9 +1583,16 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
15821583
match terr {
15831584
TypeError::ObjectUnsafeCoercion(_) => {}
15841585
_ => {
1585-
diag.span_label(span, terr.to_string());
15861586
if let Some((sp, msg)) = secondary_span {
1587-
diag.span_label(sp, msg);
1587+
if swap_secondary_and_primary {
1588+
diag.span_label(sp, terr.to_string());
1589+
diag.span_label(span, msg);
1590+
} else {
1591+
diag.span_label(span, terr.to_string());
1592+
diag.span_label(sp, msg);
1593+
}
1594+
} else {
1595+
diag.span_label(span, terr.to_string());
15881596
}
15891597
}
15901598
};
@@ -1971,7 +1979,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
19711979
struct_span_err!(self.tcx.sess, span, E0644, "{}", failure_str)
19721980
}
19731981
};
1974-
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr);
1982+
self.note_type_err(&mut diag, &trace.cause, None, Some(trace.values), terr, false);
19751983
diag
19761984
}
19771985

compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,40 +1260,47 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
12601260
"type mismatch resolving `{}`",
12611261
predicate
12621262
);
1263-
self.note_type_err(&mut diag, &obligation.cause, None, values, err);
1263+
let secondary_span = match predicate.kind().skip_binder() {
1264+
ty::PredicateKind::Projection(proj) => self
1265+
.tcx
1266+
.opt_associated_item(proj.projection_ty.item_def_id)
1267+
.and_then(|trait_assoc_item| {
1268+
self.tcx
1269+
.trait_of_item(proj.projection_ty.item_def_id)
1270+
.map(|id| (trait_assoc_item, id))
1271+
})
1272+
.and_then(|(trait_assoc_item, id)| {
1273+
self.tcx.find_map_relevant_impl(
1274+
id,
1275+
proj.projection_ty.self_ty(),
1276+
|did| {
1277+
self.tcx
1278+
.associated_items(did)
1279+
.in_definition_order()
1280+
.filter(|assoc| assoc.ident == trait_assoc_item.ident)
1281+
.next()
1282+
},
1283+
)
1284+
})
1285+
.and_then(|item| match self.tcx.hir().get_if_local(item.def_id) {
1286+
Some(
1287+
hir::Node::TraitItem(hir::TraitItem {
1288+
kind: hir::TraitItemKind::Type(_, Some(ty)),
1289+
..
1290+
})
1291+
| hir::Node::ImplItem(hir::ImplItem {
1292+
kind: hir::ImplItemKind::TyAlias(ty),
1293+
..
1294+
}),
1295+
) => {
1296+
Some((ty.span, format!("type mismatch resolving `{}`", predicate)))
1297+
}
1298+
_ => None,
1299+
}),
1300+
_ => None,
1301+
};
1302+
self.note_type_err(&mut diag, &obligation.cause, secondary_span, values, err, true);
12641303
self.note_obligation_cause(&mut diag, obligation);
1265-
match predicate.kind().skip_binder() {
1266-
ty::PredicateKind::Projection(proj) => {
1267-
let item = self
1268-
.tcx
1269-
.opt_associated_item(proj.projection_ty.item_def_id)
1270-
.and_then(|trait_assoc_item| {
1271-
self.tcx
1272-
.trait_of_item(proj.projection_ty.item_def_id)
1273-
.map(|id| (trait_assoc_item, id))
1274-
})
1275-
.and_then(|(trait_assoc_item, id)| {
1276-
self.tcx.find_map_relevant_impl(
1277-
id,
1278-
proj.projection_ty.self_ty(),
1279-
|did| {
1280-
self.tcx
1281-
.associated_items(did)
1282-
.in_definition_order()
1283-
.filter(|assoc| assoc.ident == trait_assoc_item.ident)
1284-
.next()
1285-
},
1286-
)
1287-
});
1288-
if let Some(item) = item {
1289-
diag.span_label(
1290-
item.ident.span,
1291-
&format!("type mismatch with `{}` here", proj.ty),
1292-
);
1293-
}
1294-
}
1295-
_ => {}
1296-
}
12971304
diag.emit();
12981305
}
12991306
});

compiler/rustc_typeck/src/check/compare_method.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,7 @@ fn compare_predicate_entailment<'tcx>(
374374
found: impl_fty,
375375
})),
376376
&terr,
377+
false,
377378
);
378379
diag.emit();
379380
return Err(ErrorReported);
@@ -748,8 +749,7 @@ fn compare_number_of_method_arguments<'tcx>(
748749
tcx.sess,
749750
impl_span,
750751
E0050,
751-
"method `{}` has {} but the declaration in \
752-
trait `{}` has {}",
752+
"method `{}` has {} but the declaration in trait `{}` has {}",
753753
trait_m.ident,
754754
potentially_plural_count(impl_number_args, "parameter"),
755755
tcx.def_path_str(trait_m.def_id),
@@ -1023,6 +1023,7 @@ crate fn compare_const_impl<'tcx>(
10231023
found: impl_ty,
10241024
})),
10251025
&terr,
1026+
false,
10261027
);
10271028
diag.emit();
10281029
}

src/test/ui/associated-types/associated-types-binding-to-type-defined-in-supertrait.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,25 @@ error[E0271]: type mismatch resolving `<ModelT as Vehicle>::Color == Blue`
22
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:31:10
33
|
44
LL | impl Vehicle for ModelT { type Color = Black; }
5-
| ----- type mismatch with `Blue` here
5+
| ----- expected struct `Blue`, found struct `Black`
66
...
77
LL | fn blue_car<C:Car<Color=Blue>>(c: C) {
88
| ---------- required by this bound in `blue_car`
99
...
1010
LL | fn b() { blue_car(ModelT); }
11-
| ^^^^^^^^ expected struct `Blue`, found struct `Black`
11+
| ^^^^^^^^ type mismatch resolving `<ModelT as Vehicle>::Color == Blue`
1212

1313
error[E0271]: type mismatch resolving `<ModelU as Vehicle>::Color == Black`
1414
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10
1515
|
1616
LL | impl Vehicle for ModelU { type Color = Blue; }
17-
| ----- type mismatch with `Black` here
17+
| ---- expected struct `Black`, found struct `Blue`
1818
...
1919
LL | fn black_car<C:Car<Color=Black>>(c: C) {
2020
| ----------- required by this bound in `black_car`
2121
...
2222
LL | fn c() { black_car(ModelU); }
23-
| ^^^^^^^^^ expected struct `Black`, found struct `Blue`
23+
| ^^^^^^^^^ type mismatch resolving `<ModelU as Vehicle>::Color == Black`
2424

2525
error: aborting due to 2 previous errors
2626

src/test/ui/associated-types/associated-types-eq-3.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ pub fn main() {
3737
let a = 42;
3838
foo1(a);
3939
//~^ ERROR type mismatch resolving
40-
//~| expected struct `Bar`, found `usize`
4140
baz(&a);
4241
//~^ ERROR type mismatch resolving
43-
//~| expected struct `Bar`, found `usize`
4442
}

src/test/ui/associated-types/associated-types-eq-3.stderr

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,22 @@ error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
1717
--> $DIR/associated-types-eq-3.rs:38:5
1818
|
1919
LL | type A = usize;
20-
| - type mismatch with `Bar` here
20+
| ----- expected struct `Bar`, found `usize`
2121
...
2222
LL | fn foo1<I: Foo<A=Bar>>(x: I) {
2323
| ----- required by this bound in `foo1`
2424
...
2525
LL | foo1(a);
26-
| ^^^^ expected struct `Bar`, found `usize`
26+
| ^^^^ type mismatch resolving `<isize as Foo>::A == Bar`
2727

2828
error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
29-
--> $DIR/associated-types-eq-3.rs:41:9
29+
--> $DIR/associated-types-eq-3.rs:40:9
3030
|
3131
LL | type A = usize;
32-
| - type mismatch with `Bar` here
32+
| ----- expected struct `Bar`, found `usize`
3333
...
3434
LL | baz(&a);
35-
| ^^ expected struct `Bar`, found `usize`
35+
| ^^ type mismatch resolving `<isize as Foo>::A == Bar`
3636
|
3737
= note: required for the cast to the object type `dyn Foo<A = Bar>`
3838

src/test/ui/associated-types/associated-types-eq-hr.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0271]: type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize
22
--> $DIR/associated-types-eq-hr.rs:87:5
33
|
44
LL | type A = &'a usize;
5-
| - type mismatch with `&'x isize` here
5+
| --------- expected `isize`, found `usize`
66
...
77
LL | fn foo<T>()
88
| --- required by a bound in this
@@ -11,7 +11,7 @@ LL | T: for<'x> TheTrait<&'x isize, A = &'x isize>,
1111
| ------------- required by this bound in `foo`
1212
...
1313
LL | foo::<UintStruct>();
14-
| ^^^^^^^^^^^^^^^^^ expected `isize`, found `usize`
14+
| ^^^^^^^^^^^^^^^^^ type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
1515
|
1616
= note: expected reference `&isize`
1717
found reference `&usize`
@@ -20,7 +20,7 @@ error[E0271]: type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>
2020
--> $DIR/associated-types-eq-hr.rs:91:5
2121
|
2222
LL | type A = &'a isize;
23-
| - type mismatch with `&'x usize` here
23+
| --------- expected `usize`, found `isize`
2424
...
2525
LL | fn bar<T>()
2626
| --- required by a bound in this
@@ -29,7 +29,7 @@ LL | T: for<'x> TheTrait<&'x isize, A = &'x usize>,
2929
| ------------- required by this bound in `bar`
3030
...
3131
LL | bar::<IntStruct>();
32-
| ^^^^^^^^^^^^^^^^ expected `usize`, found `isize`
32+
| ^^^^^^^^^^^^^^^^ type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
3333
|
3434
= note: expected reference `&usize`
3535
found reference `&isize`

src/test/ui/associated-types/associated-types-issue-20346.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ LL | fn is_iterator_of<A, I: Iterator<Item=A>>(_: &I) {}
55
| ------ required by this bound in `is_iterator_of`
66
...
77
LL | type Item = T;
8-
| ---- type mismatch with `Option<T>` here
8+
| - expected enum `Option`, found type parameter `T`
99
...
1010
LL | fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
1111
| - this type parameter
1212
...
1313
LL | is_iterator_of::<Option<T>, _>(&adapter);
14-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected enum `Option`, found type parameter `T`
14+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<T>`
1515
|
1616
= note: expected enum `Option<T>`
1717
found type `T`

src/test/ui/associated-types/associated-types-overridden-binding-2.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@ error[E0271]: type mismatch resolving `<std::vec::IntoIter<u32> as Iterator>::It
33
|
44
LL | let _: &dyn I32Iterator<Item = u32> = &vec![42].into_iter();
55
| ^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found `u32`
6-
|
7-
::: $SRC_DIR/core/src/iter/traits/iterator.rs:LL:COL
8-
|
9-
LL | type Item = I::Item;
10-
| ---- type mismatch with `i32` here
116
|
127
= note: required for the cast to the object type `dyn Iterator<Item = u32, Item = i32>`
138

src/test/ui/associated-types/issue-44153.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | fn visit() {}
55
| ---------- required by `Visit::visit`
66
...
77
LL | type Element = ();
8-
| ------- type mismatch with `&()` here
8+
| -- expected `&()`, found `()`
99
...
1010
LL | <() as Visit>::visit();
11-
| ^^^^^^^^^^^^^^^^^^^^ expected `&()`, found `()`
11+
| ^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<() as Array>::Element == &()`
1212
|
1313
note: required because of the requirements on the impl of `Visit` for `()`
1414
--> $DIR/issue-44153.rs:13:10

src/test/ui/associated-types/issue-72806.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | type Sibling: Bar2<Ok=char>;
55
| ------- required by this bound in `Bar::Sibling`
66
...
77
LL | type Sibling = Foo2;
8-
| ^^^^ expected `char`, found `u32`
8+
| ^^^^ type mismatch resolving `<Foo2 as Bar2>::Ok == char`
99
...
1010
LL | type Ok = u32;
11-
| -- type mismatch with `char` here
11+
| --- expected `char`, found `u32`
1212

1313
error: aborting due to previous error
1414

src/test/ui/associated-types/point-at-type-on-obligation-failure.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | type Sibling: Bar2<Ok=Self::Ok>;
55
| ----------- required by this bound in `Bar::Sibling`
66
...
77
LL | type Sibling = Foo2;
8-
| ^^^^ expected `()`, found `u32`
8+
| ^^^^ type mismatch resolving `<Foo2 as Bar2>::Ok == ()`
99
...
1010
LL | type Ok = u32;
11-
| -- type mismatch with `()` here
11+
| --- expected `()`, found `u32`
1212

1313
error: aborting due to previous error
1414

src/test/ui/async-await/async-block-control-flow-static-semantics.stderr

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ error[E0271]: type mismatch resolving `<impl Future as Future>::Output == ()`
3636
|
3737
LL | let _: &dyn Future<Output = ()> = &block;
3838
| ^^^^^^ expected `()`, found `u8`
39-
|
40-
::: $SRC_DIR/core/src/future/future.rs:LL:COL
41-
|
42-
LL | type Output = F::Output;
43-
| ------ type mismatch with `()` here
4439
|
4540
= note: required for the cast to the object type `dyn Future<Output = ()>`
4641

@@ -57,11 +52,6 @@ error[E0271]: type mismatch resolving `<impl Future as Future>::Output == ()`
5752
|
5853
LL | let _: &dyn Future<Output = ()> = &block;
5954
| ^^^^^^ expected `()`, found `u8`
60-
|
61-
::: $SRC_DIR/core/src/future/future.rs:LL:COL
62-
|
63-
LL | type Output = F::Output;
64-
| ------ type mismatch with `()` here
6555
|
6656
= note: required for the cast to the object type `dyn Future<Output = ()>`
6757

src/test/ui/error-codes/E0271.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ LL | fn foo<T>(t: T) where T: Trait<AssociatedType=u32> {
55
| ------------------ required by this bound in `foo`
66
...
77
LL | impl Trait for i8 { type AssociatedType = &'static str; }
8-
| -------------- type mismatch with `u32` here
8+
| ------------ expected `u32`, found `&str`
99
...
1010
LL | foo(3_i8);
11-
| ^^^ expected `u32`, found `&str`
11+
| ^^^ type mismatch resolving `<i8 as Trait>::AssociatedType == u32`
1212

1313
error: aborting due to previous error
1414

src/test/ui/generator/generator-yielding-or-returning-itself.stderr

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@ LL | where T: Generator<Yield = (), Return = T>
88
...
99
LL | want_cyclic_generator_return(|| {
1010
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
11-
|
12-
::: $SRC_DIR/core/src/ops/generator.rs:LL:COL
13-
|
14-
LL | type Return = G::Return;
15-
| ------ type mismatch with `[generator@$DIR/generator-yielding-or-returning-itself.rs:15:34: 19:6]` here
1611
|
1712
= note: closures cannot capture themselves or take themselves as argument;
1813
this error may be the result of a recent compiler bug-fix,
@@ -29,11 +24,6 @@ LL | where T: Generator<Yield = T, Return = ()>
2924
...
3025
LL | want_cyclic_generator_yield(|| {
3126
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^ cyclic type of infinite size
32-
|
33-
::: $SRC_DIR/core/src/ops/generator.rs:LL:COL
34-
|
35-
LL | type Yield = G::Yield;
36-
| ----- type mismatch with `[generator@$DIR/generator-yielding-or-returning-itself.rs:28:33: 32:6]` here
3727
|
3828
= note: closures cannot capture themselves or take themselves as argument;
3929
this error may be the result of a recent compiler bug-fix,

src/test/ui/generator/type-mismatch-signature-deduction.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ error[E0271]: type mismatch resolving `<[generator@$DIR/type-mismatch-signature-
1717
|
1818
LL | fn foo() -> impl Generator<Return = i32> {
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `i32`, found enum `Result`
20-
|
21-
::: $SRC_DIR/core/src/ops/generator.rs:LL:COL
22-
|
23-
LL | type Return = G::Return;
24-
| ------ type mismatch with `i32` here
2520
|
2621
= note: expected type `i32`
2722
found enum `Result<{integer}, _>`

src/test/ui/generic-associated-types/issue-68656-unsized-values.stderr

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ LL | impl<T: Copy + std::ops::Deref> UnsafeCopy<T> for T {
1717
| - this type parameter
1818
LL | type Item<'a> = T;
1919
| ^ expected type parameter `T`, found associated type
20-
|
21-
::: $SRC_DIR/core/src/ops/deref.rs:LL:COL
22-
|
23-
LL | type Target = T;
24-
| ------ type mismatch with `T` here
2520
|
2621
= note: expected type parameter `T`
2722
found associated type `<T as Deref>::Target`

0 commit comments

Comments
 (0)