Skip to content

Commit 9139ed0

Browse files
committed
Fix impl_trait_ty_to_ty substs
1 parent 03b01c5 commit 9139ed0

File tree

7 files changed

+47
-5
lines changed

7 files changed

+47
-5
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()

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/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

0 commit comments

Comments
 (0)