Test that opaque types can't have themselves as a hidden type with in… · rust-lang/rust@de473a5 (original) (raw)

File tree

6 files changed

lines changed

6 files changed

lines changed

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1 +#![feature(type_alias_impl_trait)]
2 +
3 +pub type Opaque<'a> = impl Sized;
4 +
5 +fn get_one<'a>(a: *mut &'a str) -> Opaque<'a> {
6 + a
7 +}
8 +
9 +fn get_iter<'a>() -> impl IntoIterator<Item = Opaque<'a>> {
10 +//~^ ERROR: item does not constrain
11 +None::<Opaque<'static>>
12 +}
13 +
14 +fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
1 +error: item does not constrain `Opaque::{opaque#0}`, but has it in its signature
2 + --> $DIR/different_args_considered_equal.rs:9:4
3 + |
4 +LL | fn get_iter<'a>() -> impl IntoIterator<Item = Opaque<'a>> {
5 + | ^^^^^^^^
6 + |
7 + = note: consider moving the opaque type's declaration and defining uses into a separate module
8 +note: this opaque type is in the signature
9 + --> $DIR/different_args_considered_equal.rs:3:23
10 + |
11 +LL | pub type Opaque<'a> = impl Sized;
12 + | ^^^^^^^^^^
13 +
14 +error: aborting due to 1 previous error
15 +
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
1 +#![feature(type_alias_impl_trait)]
2 +
3 +pub type Opaque<'a> = impl Sized;
4 +
5 +fn get_one<'a>(a: *mut &'a str) -> impl IntoIterator<Item = Opaque<'a>> {
6 +if a.is_null() {
7 +Some(a)
8 +} else {
9 +None::<Opaque<'static>>
10 +//~^ ERROR hidden type for `Opaque<'static>` captures lifetime that does not appear in bounds
11 +}
12 +}
13 +
14 +fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
1 +error[E0700]: hidden type for `Opaque<'static>` captures lifetime that does not appear in bounds
2 + --> $DIR/different_args_considered_equal2.rs:9:9
3 + |
4 +LL | pub type Opaque<'a> = impl Sized;
5 + | ---------- opaque type defined here
6 +LL |
7 +LL | fn get_one<'a>(a: *mut &'a str) -> impl IntoIterator<Item = Opaque<'a>> {
8 + | -- hidden type `*mut &'a str` captures the lifetime `'a` as defined here
9 +...
10 +LL | None::<Opaque<'static>>
11 + | ^^^^^^^^^^^^^^^^^^^^^^^
12 + |
13 +help: to declare that `impl IntoIterator<Item = Opaque<'a>>` captures `'a`, you can add an explicit `'a` lifetime bound
14 + |
15 +LL | fn get_one<'a>(a: *mut &'a str) -> impl IntoIterator<Item = Opaque<'a>> + 'a {
16 + | ++++
17 +
18 +error: aborting due to 1 previous error
19 +
20 +For more information about this error, try `rustc --explain E0700`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
1 +//! Test that we don't allow coercing an opaque type with a non-static
2 +//! lifetime to one with a static lifetime. While `get_iter` looks like
3 +//! it would be doing the opposite, the way we're handling projections
4 +//! makes `Opaque<'a>` the hidden type of `Opaque<'static>`.
5 +
6 +#![feature(type_alias_impl_trait)]
7 +
8 +mod defining_scope {
9 +pub type Opaque<'a> = impl Sized;
10 +
11 +fn get_one<'a>(a: *mut &'a str) -> Opaque<'a> {
12 + a
13 +}
14 +}
15 +use defining_scope::Opaque;
16 +
17 +fn get_iter<'a>() -> impl IntoIterator<Item = Opaque<'a>> {
18 +None::<Opaque<'static>>
19 +//~^ ERROR lifetime may not live long enough
20 +}
21 +
22 +fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
1 +error: lifetime may not live long enough
2 + --> $DIR/different_args_considered_equal3.rs🔞5
3 + |
4 +LL | fn get_iter<'a>() -> impl IntoIterator<Item = Opaque<'a>> {
5 + | -- lifetime `'a` defined here
6 +LL | None::<Opaque<'static>>
7 + | ^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
8 +
9 +error: aborting due to 1 previous error
10 +