summaryrefslogtreecommitdiffstats
path: root/src/test/ui/impl-trait/feature-self-return-type.rs
blob: 51877e9cc3c436287219d3fcb18b4a3ae2f84a48 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
// edition:2018
#![feature(impl_trait_projections)]

// This test checks that we emit the correct borrowck error when `Self` or a projection is used as
// a return type.  See #61949 for context.

mod with_self {
    pub struct Foo<'a> {
        pub bar: &'a i32,
    }

    impl<'a> Foo<'a> {
        pub fn new(_bar: &'a i32) -> impl Into<Self> {
            Foo {
                bar: &22
            }
        }
    }

    fn foo() {
        let x = {
            let bar = 22;
            Foo::new(&bar).into()
            //~^ ERROR `bar` does not live long enough
        };
        drop(x);
    }
}

struct Foo<T>(T);

trait FooLike {
    type Output;
}

impl<T> FooLike for Foo<T> {
    type Output = T;
}

mod impl_trait {
    use super::*;

    trait Trait {
        type Assoc;

        fn make_assoc(self) -> Self::Assoc;
    }

    /// `T::Assoc` can't be normalized any further here.
    fn foo<T: Trait>(x: T) -> impl FooLike<Output = T::Assoc> {
        Foo(x.make_assoc())
    }

    impl<'a> Trait for &'a () {
        type Assoc = &'a ();

        fn make_assoc(self) -> &'a () { &() }
    }

    fn usage() {
        let x = {
            let y = ();
            foo(&y)
            //~^ ERROR `y` does not live long enough
        };
        drop(x);
    }
}

// Same with lifetimes in the trait

mod lifetimes {
    use super::*;

    trait Trait<'a> {
        type Assoc;

        fn make_assoc(self) -> Self::Assoc;
    }

    /// Missing bound constraining `Assoc`, `T::Assoc` can't be normalized further.
    fn foo<'a, T: Trait<'a>>(x: T) -> impl FooLike<Output = T::Assoc> {
        Foo(x.make_assoc())
    }

    impl<'a> Trait<'a> for &'a () {
        type Assoc = &'a ();

        fn make_assoc(self) -> &'a () { &() }
    }

    fn usage() {
        let x = {
            let y = ();
            foo(&y)
            //~^ ERROR `y` does not live long enough
        };
        drop(x);
    }
}

fn main() { }