Skip to content

Commit 74dc8ae

Browse files
committed
Test that opaque types can't have themselves as a hidden type with incompatible lifetimes
1 parent c1f62a7 commit 74dc8ae

6 files changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
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>() -> Option<Opaque<'a>> {
10+
//~^ ERROR: item does not constrain
11+
None::<Opaque<'static>>
12+
//~^ ERROR lifetime may not live long enough
13+
}
14+
15+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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>() -> Option<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: lifetime may not live long enough
15+
--> $DIR/different_args_considered_equal.rs:11:5
16+
|
17+
LL | fn get_iter<'a>() -> Option<Opaque<'a>> {
18+
| -- lifetime `'a` defined here
19+
LL |
20+
LL | None::<Opaque<'static>>
21+
| ^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
22+
23+
error: aborting due to 2 previous errors
24+
Original file line numberDiff line numberDiff 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) -> Option<Opaque<'a>> {
6+
if a.is_null() {
7+
Some(a)
8+
} else {
9+
None::<Opaque<'static>>
10+
//~^ ERROR lifetime may not live long enough
11+
}
12+
}
13+
14+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/different_args_considered_equal2.rs:9:9
3+
|
4+
LL | fn get_one<'a>(a: *mut &'a str) -> Option<Opaque<'a>> {
5+
| -- lifetime `'a` defined here
6+
...
7+
LL | None::<Opaque<'static>>
8+
| ^^^^^^^^^^^^^^^^^^^^^^^ returning this value requires that `'a` must outlive `'static`
9+
10+
error: aborting due to 1 previous error
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//! Test that we don't allow coercing an opaque type with a static
2+
//! lifetime to a free version of itself. While this kind of
3+
//! conversion is fine for rigid types, the hidden type behind an opaque
4+
//! type may
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>() -> Option<Opaque<'a>> {
18+
None::<Opaque<'static>>
19+
//~^ ERROR lifetime may not live long enough
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: lifetime may not live long enough
2+
--> $DIR/different_args_considered_equal3.rs:18:5
3+
|
4+
LL | fn get_iter<'a>() -> Option<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+

0 commit comments

Comments
 (0)