Skip to content

Commit ca7454d

Browse files
authored
Rollup merge of rust-lang#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 a35c927 + 640c202 commit ca7454d

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
@@ -3141,8 +3141,12 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
31413141

31423142
debug!("impl_trait_ty_to_ty: generics={:?}", generics);
31433143
let substs = InternalSubsts::for_item(tcx, def_id, |param, _| {
3144-
if let Some(i) = (param.index as usize).checked_sub(generics.parent_count) {
3145-
// Our own parameters are the resolved lifetimes.
3144+
// We use `generics.count() - lifetimes.len()` here instead of `generics.parent_count`
3145+
// since return-position impl trait in trait squashes all of the generics from its source fn
3146+
// into its own generics, so the opaque's "own" params isn't always just lifetimes.
3147+
if let Some(i) = (param.index as usize).checked_sub(generics.count() - lifetimes.len())
3148+
{
3149+
// Resolve our own lifetime parameters.
31463150
let GenericParamDefKind::Lifetime { .. } = param.kind else { bug!() };
31473151
let hir::GenericArg::Lifetime(lifetime) = &lifetimes[i] else { bug!() };
31483152
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
@@ -383,6 +383,8 @@ fn impl_associated_item_for_impl_trait_in_trait(
383383
impl_assoc_ty.impl_defaultness(tcx.impl_defaultness(impl_fn_def_id));
384384

385385
// Copy generics_of the trait's associated item but the impl as the parent.
386+
// FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) resolves to the trait instead of the impl
387+
// generics.
386388
impl_assoc_ty.generics_of({
387389
let trait_assoc_generics = tcx.generics_of(trait_assoc_def_id);
388390
let trait_assoc_parent_count = trait_assoc_generics.parent_count;
@@ -391,16 +393,10 @@ fn impl_associated_item_for_impl_trait_in_trait(
391393
let parent_generics = tcx.generics_of(impl_def_id);
392394
let parent_count = parent_generics.parent_count + parent_generics.params.len();
393395

394-
let mut impl_fn_params = tcx.generics_of(impl_fn_def_id).params.clone();
395-
396396
for param in &mut params {
397-
param.index = param.index + parent_count as u32 + impl_fn_params.len() as u32
398-
- trait_assoc_parent_count as u32;
397+
param.index = param.index + parent_count as u32 - trait_assoc_parent_count as u32;
399398
}
400399

401-
impl_fn_params.extend(params);
402-
params = impl_fn_params;
403-
404400
let param_def_id_to_index =
405401
params.iter().map(|param| (param.def_id, param.index)).collect();
406402

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)