Skip to content

Commit ac02e40

Browse files
committed
Auto merge of rust-lang#114616 - oli-obk:gotta_capture_'em_all, r=compiler-errors
Capture all lifetimes for TAITs and impl trait in associated types This reverts commit cb94675, reversing changes made to 57781b2. (This is only true for the tests, the change itself was done from scratch, as the compiler has diverged sufficiently for a revert to not make sense anymore). This implements the lang team decision from this meeting: https://hackmd.io/sFaSIMJOQcuwCdnUvCxtuQ?view r? `@cjgillot` on the impl
2 parents 26089ba + e82ccd5 commit ac02e40

24 files changed

+207
-80
lines changed

compiler/rustc_hir_analysis/src/variance/mod.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,15 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
129129

130130
// By default, RPIT are invariant wrt type and const generics, but they are bivariant wrt
131131
// lifetime generics.
132-
let mut variances: Vec<_> = std::iter::repeat(ty::Invariant).take(generics.count()).collect();
132+
let variances = std::iter::repeat(ty::Invariant).take(generics.count());
133+
134+
let mut variances: Vec<_> = match tcx.opaque_type_origin(item_def_id) {
135+
rustc_hir::OpaqueTyOrigin::FnReturn(_) | rustc_hir::OpaqueTyOrigin::AsyncFn(_) => {
136+
variances.collect()
137+
}
138+
// But TAIT are invariant for all generics
139+
rustc_hir::OpaqueTyOrigin::TyAlias { .. } => return tcx.arena.alloc_from_iter(variances),
140+
};
133141

134142
// Mark all lifetimes from parent generics as unused (Bivariant).
135143
// This will be overridden later if required.

tests/ui/impl-trait/issue-108591.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ impl MyTy<'_> {
1313
}
1414
}
1515

16-
type Opaque<'a> = impl Sized;
16+
type Opaque2 = impl Sized;
17+
type Opaque<'a> = Opaque2;
1718
fn define<'a>() -> Opaque<'a> {}
1819

1920
fn test<'a>() {

tests/ui/impl-trait/issue-108592.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ fn test_closure() {
1111
closure(&opaque());
1212
}
1313

14-
type Opaque<'a> = impl Sized;
14+
type Opaque2 = impl Sized;
15+
type Opaque<'a> = Opaque2;
1516
fn define<'a>() -> Opaque<'a> {}
1617

1718
fn test_tait(_: &Opaque<'_>) {

tests/ui/impl-trait/issue-86465.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#![feature(type_alias_impl_trait)]
22

3-
pub trait Captures<'a> {}
4-
5-
impl<'a, T: ?Sized> Captures<'a> for T {}
6-
7-
type X<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
3+
type X<'a, 'b> = impl std::fmt::Debug;
84

95
fn f<'t, 'u>(a: &'t u32, b: &'u u32) -> (X<'t, 'u>, X<'u, 't>) {
106
(a, a)

tests/ui/impl-trait/issue-86465.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: concrete type differs from previous defining opaque type use
2-
--> $DIR/issue-86465.rs:10:5
2+
--> $DIR/issue-86465.rs:6:5
33
|
44
LL | (a, a)
55
| ^^^^^^

tests/ui/type-alias-impl-trait/different_lifetimes_defining_uses.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
#![feature(type_alias_impl_trait)]
22
#![allow(dead_code)]
33

4-
pub trait Captures<'a> {}
5-
6-
impl<'a, T: ?Sized> Captures<'a> for T {}
7-
8-
type OneLifetime<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
4+
type OneLifetime<'a, 'b> = impl std::fmt::Debug;
95

106
fn foo<'a, 'b>(a: &'a u32, b: &'b u32) -> OneLifetime<'a, 'b> {
117
a

tests/ui/type-alias-impl-trait/different_lifetimes_defining_uses.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
error: concrete type differs from previous defining opaque type use
2-
--> $DIR/different_lifetimes_defining_uses.rs:15:5
2+
--> $DIR/different_lifetimes_defining_uses.rs:11:5
33
|
44
LL | b
55
| ^ expected `&'a u32`, got `&'b u32`
66
|
77
note: previous use here
8-
--> $DIR/different_lifetimes_defining_uses.rs:11:5
8+
--> $DIR/different_lifetimes_defining_uses.rs:7:5
99
|
1010
LL | a
1111
| ^

tests/ui/type-alias-impl-trait/generic_duplicate_lifetime_param.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
fn main() {}
44

5-
pub trait Captures<'a> {}
6-
7-
impl<'a, T: ?Sized> Captures<'a> for T {}
8-
9-
type Two<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
5+
type Two<'a, 'b> = impl std::fmt::Debug;
106

117
fn one<'a>(t: &'a ()) -> Two<'a, 'a> {
128
//~^ ERROR non-defining opaque type use

tests/ui/type-alias-impl-trait/generic_duplicate_lifetime_param.stderr

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
error: non-defining opaque type use in defining scope
2-
--> $DIR/generic_duplicate_lifetime_param.rs:11:26
2+
--> $DIR/generic_duplicate_lifetime_param.rs:7:26
33
|
44
LL | fn one<'a>(t: &'a ()) -> Two<'a, 'a> {
55
| ^^^^^^^^^^^ generic argument `'a` used twice
66
|
77
note: for this opaque type
8-
--> $DIR/generic_duplicate_lifetime_param.rs:9:20
8+
--> $DIR/generic_duplicate_lifetime_param.rs:5:20
99
|
10-
LL | type Two<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
11-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10+
LL | type Two<'a, 'b> = impl std::fmt::Debug;
11+
| ^^^^^^^^^^^^^^^^^^^^
1212

1313
error: non-defining opaque type use in defining scope
14-
--> $DIR/generic_duplicate_lifetime_param.rs:13:5
14+
--> $DIR/generic_duplicate_lifetime_param.rs:9:5
1515
|
1616
LL | t
1717
| ^
1818
|
1919
note: lifetime used multiple times
20-
--> $DIR/generic_duplicate_lifetime_param.rs:9:10
20+
--> $DIR/generic_duplicate_lifetime_param.rs:5:10
2121
|
22-
LL | type Two<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
22+
LL | type Two<'a, 'b> = impl std::fmt::Debug;
2323
| ^^ ^^
2424

2525
error: aborting due to 2 previous errors

tests/ui/type-alias-impl-trait/generic_duplicate_param_use.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,7 @@ fn main() {}
1414
// test that unused generic parameters are ok
1515
type TwoTys<T, U> = impl Debug;
1616

17-
pub trait Captures<'a> {}
18-
19-
impl<'a, T: ?Sized> Captures<'a> for T {}
20-
21-
type TwoLifetimes<'a, 'b> = impl Debug + Captures<'a> + Captures<'b>;
17+
type TwoLifetimes<'a, 'b> = impl Debug;
2218

2319
type TwoConsts<const X: usize, const Y: usize> = impl Debug;
2420

tests/ui/type-alias-impl-trait/generic_duplicate_param_use.stderr

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: non-defining opaque type use in defining scope
2-
--> $DIR/generic_duplicate_param_use.rs:25:30
2+
--> $DIR/generic_duplicate_param_use.rs:21:30
33
|
44
LL | fn one_ty<T: Debug>(t: T) -> TwoTys<T, T> {
55
| ^^^^^^^^^^^^ generic argument `T` used twice
@@ -11,7 +11,7 @@ LL | type TwoTys<T, U> = impl Debug;
1111
| ^^^^^^^^^^
1212

1313
error: non-defining opaque type use in defining scope
14-
--> $DIR/generic_duplicate_param_use.rs:27:5
14+
--> $DIR/generic_duplicate_param_use.rs:23:5
1515
|
1616
LL | t
1717
| ^
@@ -23,49 +23,49 @@ LL | type TwoTys<T, U> = impl Debug;
2323
| ^ ^
2424

2525
error: non-defining opaque type use in defining scope
26-
--> $DIR/generic_duplicate_param_use.rs:31:36
26+
--> $DIR/generic_duplicate_param_use.rs:27:36
2727
|
2828
LL | fn one_lifetime<'a>(t: &'a u32) -> TwoLifetimes<'a, 'a> {
2929
| ^^^^^^^^^^^^^^^^^^^^ generic argument `'a` used twice
3030
|
3131
note: for this opaque type
32-
--> $DIR/generic_duplicate_param_use.rs:21:29
32+
--> $DIR/generic_duplicate_param_use.rs:17:29
3333
|
34-
LL | type TwoLifetimes<'a, 'b> = impl Debug + Captures<'a> + Captures<'b>;
35-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
34+
LL | type TwoLifetimes<'a, 'b> = impl Debug;
35+
| ^^^^^^^^^^
3636

3737
error: non-defining opaque type use in defining scope
38-
--> $DIR/generic_duplicate_param_use.rs:33:5
38+
--> $DIR/generic_duplicate_param_use.rs:29:5
3939
|
4040
LL | t
4141
| ^
4242
|
4343
note: lifetime used multiple times
44-
--> $DIR/generic_duplicate_param_use.rs:21:19
44+
--> $DIR/generic_duplicate_param_use.rs:17:19
4545
|
46-
LL | type TwoLifetimes<'a, 'b> = impl Debug + Captures<'a> + Captures<'b>;
46+
LL | type TwoLifetimes<'a, 'b> = impl Debug;
4747
| ^^ ^^
4848

4949
error: non-defining opaque type use in defining scope
50-
--> $DIR/generic_duplicate_param_use.rs:37:50
50+
--> $DIR/generic_duplicate_param_use.rs:33:50
5151
|
5252
LL | fn one_const<const N: usize>(t: *mut [u8; N]) -> TwoConsts<N, N> {
5353
| ^^^^^^^^^^^^^^^ generic argument `N` used twice
5454
|
5555
note: for this opaque type
56-
--> $DIR/generic_duplicate_param_use.rs:23:50
56+
--> $DIR/generic_duplicate_param_use.rs:19:50
5757
|
5858
LL | type TwoConsts<const X: usize, const Y: usize> = impl Debug;
5959
| ^^^^^^^^^^
6060

6161
error: non-defining opaque type use in defining scope
62-
--> $DIR/generic_duplicate_param_use.rs:39:5
62+
--> $DIR/generic_duplicate_param_use.rs:35:5
6363
|
6464
LL | t
6565
| ^
6666
|
6767
note: constant used multiple times
68-
--> $DIR/generic_duplicate_param_use.rs:23:16
68+
--> $DIR/generic_duplicate_param_use.rs:19:16
6969
|
7070
LL | type TwoConsts<const X: usize, const Y: usize> = impl Debug;
7171
| ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^

tests/ui/type-alias-impl-trait/generic_lifetime_param.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
// check-pass
1+
// build-pass (FIXME(62277): could be check-pass?)
22

33
#![feature(type_alias_impl_trait)]
44

55
fn main() {}
66

7-
type Region<'a> = impl std::fmt::Debug + 'a;
8-
7+
type Region<'a> = impl std::fmt::Debug;
98

109
fn region<'b>(a: &'b ()) -> Region<'b> {
1110
a

tests/ui/type-alias-impl-trait/implied_lifetime_wf_check3.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#![feature(type_alias_impl_trait)]
22

33
mod test_lifetime_param {
4-
type Ty<'a> = impl Sized + 'a;
4+
type Ty<'a> = impl Sized;
55
fn defining(a: &str) -> Ty<'_> { a }
66
fn assert_static<'a: 'static>() {}
77
fn test<'a>() where Ty<'a>: 'static { assert_static::<'a>() }
88
//~^ ERROR: lifetime may not live long enough
99
}
1010

1111
mod test_higher_kinded_lifetime_param {
12-
type Ty<'a> = impl Sized + 'a;
12+
type Ty<'a> = impl Sized;
1313
fn defining(a: &str) -> Ty<'_> { a }
1414
fn assert_static<'a: 'static>() {}
1515
fn test<'a>() where for<'b> Ty<'b>: 'a { assert_static::<'a>() }

tests/ui/type-alias-impl-trait/imply_bounds_from_bounds.rs

+12-8
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
// check-pass
22

3-
#![feature(impl_trait_in_assoc_type)]
3+
#![feature(impl_trait_in_assoc_type, type_alias_impl_trait)]
44

5-
trait Callable {
6-
type Output;
7-
fn call() -> Self::Output;
8-
}
5+
mod foo {
6+
pub trait Callable {
7+
type Output;
8+
fn call() -> Self::Output;
9+
}
910

10-
impl<'a> Callable for &'a () {
11-
type Output = impl Sized;
12-
fn call() -> Self::Output {}
11+
pub type OutputHelper = impl Sized;
12+
impl<'a> Callable for &'a () {
13+
type Output = OutputHelper;
14+
fn call() -> Self::Output {}
15+
}
1316
}
17+
use foo::*;
1418

1519
fn test<'a>() -> impl Sized {
1620
<&'a () as Callable>::call()

tests/ui/type-alias-impl-trait/issue-89686.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use std::future::Future;
66

7-
type G<'a, T> = impl Future<Output = ()> + 'a;
7+
type G<'a, T> = impl Future<Output = ()>;
88

99
trait Trait {
1010
type F: Future<Output = ()>;

tests/ui/type-alias-impl-trait/issue-89686.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | async move { self.f().await }
66
|
77
help: consider restricting type parameter `T`
88
|
9-
LL | type G<'a, T: Trait> = impl Future<Output = ()> + 'a;
9+
LL | type G<'a, T: Trait> = impl Future<Output = ()>;
1010
| +++++++
1111

1212
error: aborting due to previous error
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#![feature(type_alias_impl_trait)]
22

3-
type Opaque<'a, T> = impl Sized;
3+
type Opaque2<T> = impl Sized;
4+
type Opaque<'a, T> = Opaque2<T>;
45
fn defining<'a, T>(x: &'a i32) -> Opaque<T> { x }
5-
//~^ ERROR: hidden type for `Opaque<'a, T>` captures lifetime that does not appear in bounds
6+
//~^ ERROR: hidden type for `Opaque2<T>` captures lifetime that does not appear in bounds
67

78
fn main() {}

tests/ui/type-alias-impl-trait/missing_lifetime_bound.stderr

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
error[E0700]: hidden type for `Opaque<'a, T>` captures lifetime that does not appear in bounds
2-
--> $DIR/missing_lifetime_bound.rs:4:47
1+
error[E0700]: hidden type for `Opaque2<T>` captures lifetime that does not appear in bounds
2+
--> $DIR/missing_lifetime_bound.rs:5:47
33
|
4-
LL | type Opaque<'a, T> = impl Sized;
5-
| ---------- opaque type defined here
4+
LL | type Opaque2<T> = impl Sized;
5+
| ---------- opaque type defined here
6+
LL | type Opaque<'a, T> = Opaque2<T>;
67
LL | fn defining<'a, T>(x: &'a i32) -> Opaque<T> { x }
78
| -- ^
89
| |

tests/ui/type-alias-impl-trait/multiple-def-uses-in-one-fn-lifetimes.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
#![feature(type_alias_impl_trait)]
22

3-
pub trait Captures<'a> {}
4-
5-
impl<'a, T: ?Sized> Captures<'a> for T {}
6-
7-
type Foo<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
3+
type Foo<'a, 'b> = impl std::fmt::Debug;
84

95
fn foo<'x, 'y>(i: &'x i32, j: &'y i32) -> (Foo<'x, 'y>, Foo<'y, 'x>) {
106
(i, i) //~ ERROR concrete type differs from previous

tests/ui/type-alias-impl-trait/multiple-def-uses-in-one-fn-lifetimes.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: concrete type differs from previous defining opaque type use
2-
--> $DIR/multiple-def-uses-in-one-fn-lifetimes.rs:10:5
2+
--> $DIR/multiple-def-uses-in-one-fn-lifetimes.rs:6:5
33
|
44
LL | (i, i)
55
| ^^^^^^

tests/ui/type-alias-impl-trait/multiple-def-uses-in-one-fn-pass.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ fn f<A: ToString + Clone, B: ToString + Clone>(a: A, b: B) -> (X<A, B>, X<A, B>)
77
(a.clone(), a)
88
}
99

10-
pub trait Captures<'a> {}
11-
12-
impl<'a, T: ?Sized> Captures<'a> for T {}
13-
14-
type Foo<'a, 'b> = impl std::fmt::Debug + Captures<'a> + Captures<'b>;
10+
type Foo<'a, 'b> = impl std::fmt::Debug;
1511

1612
fn foo<'x, 'y>(i: &'x i32, j: &'y i32) -> (Foo<'x, 'y>, Foo<'y, 'x>) {
1713
(i, j)

tests/ui/type-alias-impl-trait/self_implication.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ fn bar() {
2222
}
2323

2424
// desugared
25-
type FooX<'a> = impl Sized;
25+
type FooX = impl Sized;
2626
impl<'a> Foo<'a> {
27-
fn foo(&self) -> FooX<'a> {}
27+
fn foo(&self) -> FooX {}
2828
}
2929

3030
// use site

0 commit comments

Comments
 (0)