Skip to content

Commit d86fd83

Browse files
authored
Rollup merge of #109277 - spastorino:new-rpitit-14, r=compiler-errors
Fix generics_of for impl's RPITIT synthesized associated type The only useful commit is the last one. This makes `generics_of` for the impl side RPITIT copy from the trait's associated type and avoid the fn on the impl side which was previously wrongly used. This solution is better but we still need to fix resolution of the generated generics. r? ``@compiler-errors``
2 parents 88caa29 + 640c202 commit d86fd83

13 files changed

+60
-12
lines changed

compiler/rustc_hir_analysis/src/astconv/mod.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -3152,8 +3152,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
31523152

31533153
debug!("impl_trait_ty_to_ty: generics={:?}", generics);
31543154
let substs = InternalSubsts::for_item(tcx, def_id, |param, _| {
3155-
if let Some(i) = (param.index as usize).checked_sub(generics.parent_count) {
3156-
// Our own parameters are the resolved lifetimes.
3155+
// We use `generics.count() - lifetimes.len()` here instead of `generics.parent_count`
3156+
// since return-position impl trait in trait squashes all of the generics from its source fn
3157+
// into its own generics, so the opaque's "own" params isn't always just lifetimes.
3158+
if let Some(i) = (param.index as usize).checked_sub(generics.count() - lifetimes.len())
3159+
{
3160+
// Resolve our own lifetime parameters.
31573161
let GenericParamDefKind::Lifetime { .. } = param.kind else { bug!() };
31583162
let hir::GenericArg::Lifetime(lifetime) = &lifetimes[i] else { bug!() };
31593163
self.ast_region_to_region(lifetime, None).into()

compiler/rustc_ty_utils/src/assoc.rs

+3-7
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,8 @@ fn associated_type_for_impl_trait_in_impl(
396396
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
397397

398398
// Copy generics_of the trait's associated item but the impl as the parent.
399+
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) resolves to the trait instead of the impl
400+
// generics.
399401
impl_assoc_ty.generics_of({
400402
let trait_assoc_generics = tcx.generics_of(trait_assoc_def_id);
401403
let trait_assoc_parent_count = trait_assoc_generics.parent_count;
@@ -404,16 +406,10 @@ fn associated_type_for_impl_trait_in_impl(
404406
let parent_generics = tcx.generics_of(impl_def_id);
405407
let parent_count = parent_generics.parent_count + parent_generics.params.len();
406408

407-
let mut impl_fn_params = tcx.generics_of(impl_fn_def_id).params.clone();
408-
409409
for param in &mut params {
410-
param.index = param.index + parent_count as u32 + impl_fn_params.len() as u32
411-
- trait_assoc_parent_count as u32;
410+
param.index = param.index + parent_count as u32 - trait_assoc_parent_count as u32;
412411
}
413412

414-
impl_fn_params.extend(params);
415-
params = impl_fn_params;
416-
417413
let param_def_id_to_index =
418414
params.iter().map(|param| (param.def_id, param.index)).collect();
419415

tests/ui/async-await/in-trait/issue-102310.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-pass
22
// edition:2021
3+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
4+
// revisions: current next
35

46
#![feature(async_fn_in_trait)]
57
#![allow(incomplete_features)]

tests/ui/async-await/in-trait/lifetime-mismatch.stderr renamed to tests/ui/async-await/in-trait/lifetime-mismatch.current.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2-
--> $DIR/lifetime-mismatch.rs:3:12
2+
--> $DIR/lifetime-mismatch.rs:5:12
33
|
44
LL | #![feature(async_fn_in_trait)]
55
| ^^^^^^^^^^^^^^^^^
@@ -8,7 +8,7 @@ LL | #![feature(async_fn_in_trait)]
88
= note: `#[warn(incomplete_features)]` on by default
99

1010
error[E0195]: lifetime parameters or bounds on method `foo` do not match the trait declaration
11-
--> $DIR/lifetime-mismatch.rs:12:17
11+
--> $DIR/lifetime-mismatch.rs:14:17
1212
|
1313
LL | async fn foo<'a>(&self);
1414
| ---- lifetimes in impl do not match this method in trait
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes
2+
--> $DIR/lifetime-mismatch.rs:5:12
3+
|
4+
LL | #![feature(async_fn_in_trait)]
5+
| ^^^^^^^^^^^^^^^^^
6+
|
7+
= note: see issue #91611 <https://github.com/rust-lang/rust/issues/91611> for more information
8+
= note: `#[warn(incomplete_features)]` on by default
9+
10+
error[E0195]: lifetime parameters or bounds on method `foo` do not match the trait declaration
11+
--> $DIR/lifetime-mismatch.rs:14:17
12+
|
13+
LL | async fn foo<'a>(&self);
14+
| ---- lifetimes in impl do not match this method in trait
15+
...
16+
LL | async fn foo(&self) {}
17+
| ^ lifetimes do not match method in trait
18+
19+
error: aborting due to previous error; 1 warning emitted
20+
21+
For more information about this error, try `rustc --explain E0195`.

tests/ui/async-await/in-trait/lifetime-mismatch.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// edition:2021
2+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
3+
// revisions: current next
24

35
#![feature(async_fn_in_trait)]
46
//~^ WARN the feature `async_fn_in_trait` is incomplete and may not be safe to use and/or cause compiler crashes

tests/ui/impl-trait/in-trait/early.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-pass
22
// edition:2021
3+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
4+
// revisions: current next
35

46
#![feature(async_fn_in_trait, return_position_impl_trait_in_trait)]
57
#![allow(incomplete_features)]

tests/ui/impl-trait/in-trait/issue-102301.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// check-pass
2+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
3+
// revisions: current next
24

35
#![feature(return_position_impl_trait_in_trait)]
46
#![allow(incomplete_features)]

tests/ui/impl-trait/in-trait/opaque-in-impl.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
// check-pass
2+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
3+
// revisions: current next
24

35
#![feature(return_position_impl_trait_in_trait)]
46
#![allow(incomplete_features)]

tests/ui/impl-trait/in-trait/trait-more-generics-than-impl.stderr renamed to tests/ui/impl-trait/in-trait/trait-more-generics-than-impl.current.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error[E0049]: method `bar` has 0 type parameters but its trait declaration has 1 type parameter
2-
--> $DIR/trait-more-generics-than-impl.rs:11:11
2+
--> $DIR/trait-more-generics-than-impl.rs:14:11
33
|
44
LL | fn bar<T>() -> impl Sized;
55
| - expected 1 type parameter
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0049]: method `bar` has 0 type parameters but its trait declaration has 1 type parameter
2+
--> $DIR/trait-more-generics-than-impl.rs:14:11
3+
|
4+
LL | fn bar<T>() -> impl Sized;
5+
| - expected 1 type parameter
6+
...
7+
LL | fn bar() -> impl Sized {}
8+
| ^ found 0 type parameters
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0049`.

tests/ui/impl-trait/in-trait/trait-more-generics-than-impl.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
2+
// revisions: current next
3+
14
#![feature(return_position_impl_trait_in_trait)]
25
#![allow(incomplete_features)]
36

tests/ui/impl-trait/in-trait/where-clause.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// check-pass
22
// edition: 2021
3+
// [next] compile-flags: -Zlower-impl-trait-in-trait-to-assoc-ty
4+
// revisions: current next
35

46
#![feature(return_position_impl_trait_in_trait)]
57
#![allow(incomplete_features)]

0 commit comments

Comments
 (0)