Skip to content

Commit ee63d09

Browse files
authored
Rollup merge of #100483 - compiler-errors:point-to-projection-too, r=jyn514
Point to generic or arg if it's the self type of unsatisfied projection predicate We do this for `TraitPredicate`s in `point_at_type_arg_instead_of_call_if_possible` and `point_at_arg_instead_of_call_if_possible`, so also do it for `ProjectionPredicate`. Improves spans for a lot of unit tests.
2 parents 965ed81 + 54edf18 commit ee63d09

16 files changed

+155
-92
lines changed

compiler/rustc_typeck/src/check/fn_ctxt/checks.rs

+27-23
Original file line numberDiff line numberDiff line change
@@ -1664,12 +1664,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16641664
ObligationCauseCode::ImplDerivedObligation(code) => {
16651665
code.derived.parent_trait_pred.self_ty().skip_binder().into()
16661666
}
1667-
_ if let ty::PredicateKind::Trait(predicate) =
1668-
error.obligation.predicate.kind().skip_binder() =>
1669-
{
1670-
predicate.self_ty().into()
1671-
}
1672-
_ => continue,
1667+
_ => match error.obligation.predicate.kind().skip_binder() {
1668+
ty::PredicateKind::Trait(predicate) => predicate.self_ty().into(),
1669+
ty::PredicateKind::Projection(predicate) => {
1670+
predicate.projection_ty.self_ty().into()
1671+
}
1672+
_ => continue,
1673+
},
16731674
};
16741675
let self_ = self.resolve_vars_if_possible(self_);
16751676
let ty_matches_self = |ty: Ty<'tcx>| ty.walk().any(|arg| arg == self_);
@@ -1759,25 +1760,28 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
17591760
if let hir::ExprKind::Call(path, _) = &call_expr.kind {
17601761
if let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = &path.kind {
17611762
for error in errors {
1762-
if let ty::PredicateKind::Trait(predicate) =
1763-
error.obligation.predicate.kind().skip_binder()
1763+
let self_ty = match error.obligation.predicate.kind().skip_binder() {
1764+
ty::PredicateKind::Trait(predicate) => predicate.self_ty(),
1765+
ty::PredicateKind::Projection(predicate) => {
1766+
predicate.projection_ty.self_ty()
1767+
}
1768+
_ => continue,
1769+
};
1770+
// If any of the type arguments in this path segment caused the
1771+
// `FulfillmentError`, point at its span (#61860).
1772+
for arg in path
1773+
.segments
1774+
.iter()
1775+
.filter_map(|seg| seg.args.as_ref())
1776+
.flat_map(|a| a.args.iter())
17641777
{
1765-
// If any of the type arguments in this path segment caused the
1766-
// `FulfillmentError`, point at its span (#61860).
1767-
for arg in path
1768-
.segments
1769-
.iter()
1770-
.filter_map(|seg| seg.args.as_ref())
1771-
.flat_map(|a| a.args.iter())
1778+
if let hir::GenericArg::Type(hir_ty) = &arg
1779+
&& let Some(ty) =
1780+
self.typeck_results.borrow().node_type_opt(hir_ty.hir_id)
1781+
&& self.resolve_vars_if_possible(ty) == self_ty
17721782
{
1773-
if let hir::GenericArg::Type(hir_ty) = &arg
1774-
&& let Some(ty) =
1775-
self.typeck_results.borrow().node_type_opt(hir_ty.hir_id)
1776-
&& self.resolve_vars_if_possible(ty) == predicate.self_ty()
1777-
{
1778-
error.obligation.cause.span = hir_ty.span;
1779-
break;
1780-
}
1783+
error.obligation.cause.span = hir_ty.span;
1784+
break;
17811785
}
17821786
}
17831787
}

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

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0271]: type mismatch resolving `<ModelT as Vehicle>::Color == Blue`
2-
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:31:10
2+
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:31:19
33
|
44
LL | fn b() { blue_car(ModelT); }
5-
| ^^^^^^^^ type mismatch resolving `<ModelT as Vehicle>::Color == Blue`
5+
| -------- ^^^^^^ type mismatch resolving `<ModelT as Vehicle>::Color == Blue`
6+
| |
7+
| required by a bound introduced by this call
68
|
79
note: expected this to be `Blue`
810
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:16:40
@@ -16,10 +18,12 @@ LL | fn blue_car<C:Car<Color=Blue>>(c: C) {
1618
| ^^^^^^^^^^ required by this bound in `blue_car`
1719

1820
error[E0271]: type mismatch resolving `<ModelU as Vehicle>::Color == Black`
19-
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:10
21+
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:32:20
2022
|
2123
LL | fn c() { black_car(ModelU); }
22-
| ^^^^^^^^^ type mismatch resolving `<ModelU as Vehicle>::Color == Black`
24+
| --------- ^^^^^^ type mismatch resolving `<ModelU as Vehicle>::Color == Black`
25+
| |
26+
| required by a bound introduced by this call
2327
|
2428
note: expected this to be `Black`
2529
--> $DIR/associated-types-binding-to-type-defined-in-supertrait.rs:21:40

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ LL | fn foo2<I: Foo<A = Bar>>(x: I) {
1414
| +++++++++
1515

1616
error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
17-
--> $DIR/associated-types-eq-3.rs:38:5
17+
--> $DIR/associated-types-eq-3.rs:38:10
1818
|
1919
LL | foo1(a);
20-
| ^^^^ type mismatch resolving `<isize as Foo>::A == Bar`
20+
| ---- ^ type mismatch resolving `<isize as Foo>::A == Bar`
21+
| |
22+
| required by a bound introduced by this call
2123
|
2224
note: expected this to be `Bar`
2325
--> $DIR/associated-types-eq-3.rs:12:14
@@ -34,7 +36,9 @@ error[E0271]: type mismatch resolving `<isize as Foo>::A == Bar`
3436
--> $DIR/associated-types-eq-3.rs:40:9
3537
|
3638
LL | baz(&a);
37-
| ^^ type mismatch resolving `<isize as Foo>::A == Bar`
39+
| --- ^^ type mismatch resolving `<isize as Foo>::A == Bar`
40+
| |
41+
| required by a bound introduced by this call
3842
|
3943
note: expected this to be `Bar`
4044
--> $DIR/associated-types-eq-3.rs:12:14

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0271]: type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
2-
--> $DIR/associated-types-eq-hr.rs:87:5
2+
--> $DIR/associated-types-eq-hr.rs:87:11
33
|
44
LL | foo::<UintStruct>();
5-
| ^^^^^^^^^^^^^^^^^ type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
5+
| ^^^^^^^^^^ type mismatch resolving `for<'x> <UintStruct as TheTrait<&'x isize>>::A == &'x isize`
66
|
77
note: expected this to be `&isize`
88
--> $DIR/associated-types-eq-hr.rs:26:14
@@ -21,10 +21,10 @@ LL | T: for<'x> TheTrait<&'x isize, A = &'x isize>,
2121
| ^^^^^^^^^^^^^ required by this bound in `foo`
2222

2323
error[E0271]: type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
24-
--> $DIR/associated-types-eq-hr.rs:91:5
24+
--> $DIR/associated-types-eq-hr.rs:91:11
2525
|
2626
LL | bar::<IntStruct>();
27-
| ^^^^^^^^^^^^^^^^ type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
27+
| ^^^^^^^^^ type mismatch resolving `for<'x> <IntStruct as TheTrait<&'x isize>>::A == &'x usize`
2828
|
2929
note: expected this to be `&usize`
3030
--> $DIR/associated-types-eq-hr.rs:14:14

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
error[E0271]: type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<T>`
2-
--> $DIR/associated-types-issue-20346.rs:34:5
2+
--> $DIR/associated-types-issue-20346.rs:34:36
33
|
44
LL | fn test_adapter<T, I: Iterator<Item=Option<T>>>(it: I) {
55
| - this type parameter
66
...
77
LL | is_iterator_of::<Option<T>, _>(&adapter);
8-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<T>`
8+
| ------------------------------ ^^^^^^^^ type mismatch resolving `<Adapter<I> as Iterator>::Item == Option<T>`
9+
| |
10+
| required by a bound introduced by this call
911
|
1012
note: expected this to be `Option<T>`
1113
--> $DIR/associated-types-issue-20346.rs:23:17

src/test/ui/associated-types/associated-types-multiple-types-one-trait.stderr

+8-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
error[E0271]: type mismatch resolving `<T as Foo>::Y == i32`
2-
--> $DIR/associated-types-multiple-types-one-trait.rs:13:5
2+
--> $DIR/associated-types-multiple-types-one-trait.rs:13:12
33
|
44
LL | want_y(t);
5-
| ^^^^^^ expected `i32`, found associated type
5+
| ------ ^ expected `i32`, found associated type
6+
| |
7+
| required by a bound introduced by this call
68
|
79
= note: expected type `i32`
810
found associated type `<T as Foo>::Y`
@@ -17,10 +19,12 @@ LL | fn have_x_want_y<T:Foo<X=u32, Y = i32>>(t: &T)
1719
| +++++++++
1820

1921
error[E0271]: type mismatch resolving `<T as Foo>::X == u32`
20-
--> $DIR/associated-types-multiple-types-one-trait.rs:18:5
22+
--> $DIR/associated-types-multiple-types-one-trait.rs:18:12
2123
|
2224
LL | want_x(t);
23-
| ^^^^^^ expected `u32`, found associated type
25+
| ------ ^ expected `u32`, found associated type
26+
| |
27+
| required by a bound introduced by this call
2428
|
2529
= note: expected type `u32`
2630
found associated type `<T as Foo>::X`

0 commit comments

Comments
 (0)