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