]> git.lizzy.rs Git - rust.git/blob - tests/ui/impl-trait/feature-self-return-type.rs
Rollup merge of #106097 - mejrs:mir_build2, r=oli-obk
[rust.git] / tests / ui / impl-trait / feature-self-return-type.rs
1 // edition:2018
2 #![feature(impl_trait_projections)]
3
4 // This test checks that we emit the correct borrowck error when `Self` or a projection is used as
5 // a return type.  See #61949 for context.
6
7 mod with_self {
8     pub struct Foo<'a> {
9         pub bar: &'a i32,
10     }
11
12     impl<'a> Foo<'a> {
13         pub fn new(_bar: &'a i32) -> impl Into<Self> {
14             Foo {
15                 bar: &22
16             }
17         }
18     }
19
20     fn foo() {
21         let x = {
22             let bar = 22;
23             Foo::new(&bar).into()
24             //~^ ERROR `bar` does not live long enough
25         };
26         drop(x);
27     }
28 }
29
30 struct Foo<T>(T);
31
32 trait FooLike {
33     type Output;
34 }
35
36 impl<T> FooLike for Foo<T> {
37     type Output = T;
38 }
39
40 mod impl_trait {
41     use super::*;
42
43     trait Trait {
44         type Assoc;
45
46         fn make_assoc(self) -> Self::Assoc;
47     }
48
49     /// `T::Assoc` can't be normalized any further here.
50     fn foo<T: Trait>(x: T) -> impl FooLike<Output = T::Assoc> {
51         Foo(x.make_assoc())
52     }
53
54     impl<'a> Trait for &'a () {
55         type Assoc = &'a ();
56
57         fn make_assoc(self) -> &'a () { &() }
58     }
59
60     fn usage() {
61         let x = {
62             let y = ();
63             foo(&y)
64             //~^ ERROR `y` does not live long enough
65         };
66         drop(x);
67     }
68 }
69
70 // Same with lifetimes in the trait
71
72 mod lifetimes {
73     use super::*;
74
75     trait Trait<'a> {
76         type Assoc;
77
78         fn make_assoc(self) -> Self::Assoc;
79     }
80
81     /// Missing bound constraining `Assoc`, `T::Assoc` can't be normalized further.
82     fn foo<'a, T: Trait<'a>>(x: T) -> impl FooLike<Output = T::Assoc> {
83         Foo(x.make_assoc())
84     }
85
86     impl<'a> Trait<'a> for &'a () {
87         type Assoc = &'a ();
88
89         fn make_assoc(self) -> &'a () { &() }
90     }
91
92     fn usage() {
93         let x = {
94             let y = ();
95             foo(&y)
96             //~^ ERROR `y` does not live long enough
97         };
98         drop(x);
99     }
100 }
101
102 fn main() { }