Skip to content

Commit fbf62ca

Browse files
authored
Unrolled build for rust-lang#132935
Rollup merge of rust-lang#132935 - compiler-errors:arg-math, r=nnethercote Make sure to ignore elided lifetimes when pointing at args for fulfillment errors See the comment I left in the code. --- If we have something like: ``` fn foo<'a, T: 'a + BoundThatIsNotSatisfied>() {} ``` And the user turbofishes just the type args: ``` foo::<()>(); ``` Then if we try pointing at `()` (i.e. the type argument for `T`), we don't actually consider the possibility that the lifetimes may have been left out of the turbofish. We try indexing incorrectly into the HIR args, and bail on the suggestion.
2 parents 9a9dadd + d128c80 commit fbf62ca

7 files changed

+42
-13
lines changed

compiler/rustc_hir_typeck/src/fn_ctxt/adjust_fulfillment_errors.rs

+16-4
Original file line numberDiff line numberDiff line change
@@ -316,12 +316,24 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
316316
.tcx
317317
.generics_of(def_id)
318318
.own_args(ty::GenericArgs::identity_for_item(self.tcx, def_id));
319-
let Some((index, _)) =
320-
own_args.iter().enumerate().find(|(_, arg)| **arg == param_to_point_at)
321-
else {
319+
let Some(mut index) = own_args.iter().position(|arg| *arg == param_to_point_at) else {
322320
return false;
323321
};
324-
let Some(arg) = segment.args().args.get(index) else {
322+
// SUBTLE: We may or may not turbofish lifetime arguments, which will
323+
// otherwise be elided. if our "own args" starts with a lifetime, but
324+
// the args list does not, then we should chop off all of the lifetimes,
325+
// since they're all elided.
326+
let segment_args = segment.args().args;
327+
if matches!(own_args[0].unpack(), ty::GenericArgKind::Lifetime(_))
328+
&& segment_args.first().is_some_and(|arg| arg.is_ty_or_const())
329+
&& let Some(offset) = own_args.iter().position(|arg| {
330+
matches!(arg.unpack(), ty::GenericArgKind::Type(_) | ty::GenericArgKind::Const(_))
331+
})
332+
&& let Some(new_index) = index.checked_sub(offset)
333+
{
334+
index = new_index;
335+
}
336+
let Some(arg) = segment_args.get(index) else {
325337
return false;
326338
};
327339
error.obligation.cause.span = arg

tests/ui/associated-types/hr-associated-type-bound-param-3.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ LL | for<'b> <T as X<'b, T>>::U: Clone,
1515
| ^^^^^ required by this bound in `X`
1616

1717
error[E0277]: the trait bound `str: Clone` is not satisfied
18-
--> $DIR/hr-associated-type-bound-param-3.rs:18:5
18+
--> $DIR/hr-associated-type-bound-param-3.rs:18:18
1919
|
2020
LL | <(i32,) as X<(i32,)>>::f("abc");
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `str`
21+
| ^^^^^^ the trait `Clone` is not implemented for `str`
2222
|
2323
= help: the trait `Clone` is implemented for `String`
2424
note: required by a bound in `X::f`

tests/ui/associated-types/hr-associated-type-bound-param-4.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ LL | for<'b> <(T,) as X<'b, T>>::U: Clone,
1515
| ^^^^^ required by this bound in `X`
1616

1717
error[E0277]: the trait bound `str: Clone` is not satisfied
18-
--> $DIR/hr-associated-type-bound-param-4.rs:18:5
18+
--> $DIR/hr-associated-type-bound-param-4.rs:18:18
1919
|
2020
LL | <(i32,) as X<i32>>::f("abc");
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `str`
21+
| ^^^ the trait `Clone` is not implemented for `str`
2222
|
2323
= help: the trait `Clone` is implemented for `String`
2424
note: required by a bound in `X::f`

tests/ui/associated-types/hr-associated-type-bound-param-5.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ LL | for<'b> <T::Next as X<'b, T::Next>>::U: Clone,
3131
| ^^^^^ required by this bound in `X`
3232

3333
error[E0277]: the trait bound `str: Clone` is not satisfied
34-
--> $DIR/hr-associated-type-bound-param-5.rs:36:5
34+
--> $DIR/hr-associated-type-bound-param-5.rs:36:15
3535
|
3636
LL | <i32 as X<Box<i32>>>::f("abc");
37-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Clone` is not implemented for `str`
37+
| ^^^^^^^^ the trait `Clone` is not implemented for `str`
3838
|
3939
= help: the trait `Clone` is implemented for `String`
4040
note: required by a bound in `X::f`

tests/ui/associated-types/hr-associated-type-bound-param-6.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ impl<S, T> X<'_, T> for (S,) {
1717
pub fn main() {
1818
<(i32,) as X<i32>>::f("abc");
1919
//~^ ERROR the trait bound `for<'b> i32: X<'b, i32>` is not satisfied
20+
//~| ERROR the trait bound `for<'b> i32: X<'b, i32>` is not satisfied
2021
//~| ERROR the trait bound `i32: X<'_, i32>` is not satisfied
2122
}

tests/ui/associated-types/hr-associated-type-bound-param-6.stderr

+17-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,22 @@ LL | <(i32,) as X<i32>>::f("abc");
1717
|
1818
= help: the trait `X<'_, T>` is implemented for `(S,)`
1919

20+
error[E0277]: the trait bound `for<'b> i32: X<'b, i32>` is not satisfied
21+
--> $DIR/hr-associated-type-bound-param-6.rs:18:18
22+
|
23+
LL | <(i32,) as X<i32>>::f("abc");
24+
| ^^^ the trait `for<'b> X<'b, i32>` is not implemented for `i32`
25+
|
26+
= help: the trait `X<'_, T>` is implemented for `(S,)`
27+
note: required by a bound in `X::f`
28+
--> $DIR/hr-associated-type-bound-param-6.rs:3:16
29+
|
30+
LL | for<'b> T: X<'b, T>,
31+
| ^^^^^^^^ required by this bound in `X::f`
32+
...
33+
LL | fn f(x: &<T as X<'_, T>>::U) {
34+
| - required by a bound in this associated function
35+
2036
error[E0277]: the trait bound `i32: X<'_, i32>` is not satisfied
2137
--> $DIR/hr-associated-type-bound-param-6.rs:18:27
2238
|
@@ -25,6 +41,6 @@ LL | <(i32,) as X<i32>>::f("abc");
2541
|
2642
= help: the trait `X<'_, T>` is implemented for `(S,)`
2743

28-
error: aborting due to 3 previous errors
44+
error: aborting due to 4 previous errors
2945

3046
For more information about this error, try `rustc --explain E0277`.

tests/ui/higher-ranked/leak-check/candidate-from-env-universe-err-2.next.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0277]: the trait bound `for<'a> T: Trait<'a, '_>` is not satisfied
2-
--> $DIR/candidate-from-env-universe-err-2.rs:15:5
2+
--> $DIR/candidate-from-env-universe-err-2.rs:15:15
33
|
44
LL | impl_hr::<T>();
5-
| ^^^^^^^^^^^^^^ the trait `for<'a> Trait<'a, '_>` is not implemented for `T`
5+
| ^ the trait `for<'a> Trait<'a, '_>` is not implemented for `T`
66
|
77
note: required by a bound in `impl_hr`
88
--> $DIR/candidate-from-env-universe-err-2.rs:12:19

0 commit comments

Comments
 (0)