summaryrefslogtreecommitdiffstats
path: root/tests/ui/type-alias-impl-trait/wf-nested.rs
blob: de38832948918c82627c07afa95630156e800cf8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Well-formedness of nested opaque types, i.e. `impl Sized` in
// `type Outer = impl Trait<Assoc = impl Sized>`.
// See the comments below.
//
// revisions: pass pass_sound fail
// [pass] check-pass
// [pass_sound] check-fail
// [fail] check-fail

#![feature(type_alias_impl_trait)]

struct IsStatic<T: 'static>(T);

trait Trait<In> {
    type Out;

    fn get(&self) -> Result<Self::Out, ()> {
        Err(())
    }
}

impl<T> Trait<&'static T> for () {
    type Out = IsStatic<T>;
}

// The hidden type for `impl Sized` is `IsStatic<T>`, which requires `T: 'static`.
// We know it is well-formed because it can *only* be referenced as a projection:
// <OuterOpaque<T> as Trait<&'static T>>::Out`.
// So any instantiation of the type already requires proving `T: 'static`.
#[cfg(pass)]
mod pass {
    use super::*;
    type OuterOpaque<T> = impl Trait<&'static T, Out = impl Sized>;
    fn define<T>() -> OuterOpaque<T> {}
}

// Test the soundness of `pass` - We should require `T: 'static` at the use site.
#[cfg(pass_sound)]
mod pass_sound {
    use super::*;
    type OuterOpaque<T> = impl Trait<&'static T, Out = impl Sized>;
    fn define<T>() -> OuterOpaque<T> {}

    fn test<T>() {
        let outer = define::<T>();
        let _ = outer.get(); //[pass_sound]~ ERROR `T` may not live long enough
    }
}

// Similar to `pass` but here `impl Sized` can be referenced directly as
// InnerOpaque<T>, so we require an explicit bound `T: 'static`.
#[cfg(fail)]
mod fail {
    use super::*;
    type InnerOpaque<T> = impl Sized; //[fail]~ ERROR `T` may not live long enough
    type OuterOpaque<T> = impl Trait<&'static T, Out = InnerOpaque<T>>;
    fn define<T>() -> OuterOpaque<T> {}
}

fn main() {}