]> git.lizzy.rs Git - rust.git/blob - src/test/ui/regions/region-borrow-params-issue-29793-small.rs
Rollup merge of #57132 - daxpedda:master, r=steveklabnik
[rust.git] / src / test / ui / regions / region-borrow-params-issue-29793-small.rs
1 // Issue #29793, small regression tests: do not let borrows of
2 // parameters to ever be returned (expanded with exploration of
3 // variations).
4
5 // CLOSURES
6
7 fn escaping_borrow_of_closure_params_1() {
8     let g = |x: usize, y:usize| {
9         let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
10         //~^ ERROR `x` does not live long enough
11         //~| ERROR `y` does not live long enough
12         return f;
13     };
14
15     // We delberately do not call `g`; this small version of the test,
16     // after adding such a call, was (properly) rejected even when the
17     // system still suffered from issue #29793.
18
19     // g(10, 20)(true);
20 }
21
22 fn escaping_borrow_of_closure_params_2() {
23     let g = |x: usize, y:usize| {
24         let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
25         //~^ ERROR `x` does not live long enough
26         //~| ERROR `y` does not live long enough
27         f
28     };
29
30     // (we don't call `g`; see above)
31 }
32
33 fn move_of_closure_params() {
34     let g = |x: usize, y:usize| {
35         let f = move |t: bool| if t { x } else { y };
36         f;
37     };
38     // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
39     (g(1,2));
40 }
41
42 fn ok_borrow_of_fn_params(a: usize, b:usize) {
43     let g = |x: usize, y:usize| {
44         let f = |t: bool| if t { a } else { b };
45         return f;
46     };
47     // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
48     (g(1,2))(true);
49 }
50
51 // TOP-LEVEL FN'S
52
53 fn escaping_borrow_of_fn_params_1() {
54     fn g<'a>(x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
55         let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
56         //~^ ERROR E0373
57         //~| ERROR E0373
58         return Box::new(f);
59     };
60
61     // (we don't call `g`; see above)
62 }
63
64 fn escaping_borrow_of_fn_params_2() {
65     fn g<'a>(x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
66         let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
67         //~^ ERROR E0373
68         //~| ERROR E0373
69         Box::new(f)
70     };
71
72     // (we don't call `g`; see above)
73 }
74
75 fn move_of_fn_params() {
76     fn g<'a>(x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
77         let f = move |t: bool| if t { x } else { y };
78         return Box::new(f);
79     };
80     // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
81     (g(1,2))(true);
82 }
83
84 // INHERENT METHODS
85
86 fn escaping_borrow_of_method_params_1() {
87     struct S;
88     impl S {
89         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
90             let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
91             //~^ ERROR E0373
92             //~| ERROR E0373
93             return Box::new(f);
94         }
95     }
96
97     // (we don't call `g`; see above)
98 }
99
100 fn escaping_borrow_of_method_params_2() {
101     struct S;
102     impl S {
103         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
104             let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
105             //~^ ERROR E0373
106             //~| ERROR E0373
107             Box::new(f)
108         }
109     }
110     // (we don't call `g`; see above)
111 }
112
113 fn move_of_method_params() {
114     struct S;
115     impl S {
116         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
117             let f = move |t: bool| if t { x } else { y };
118             return Box::new(f);
119         }
120     }
121     // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
122     (S.g(1,2))(true);
123 }
124
125 // TRAIT IMPL METHODS
126
127 fn escaping_borrow_of_trait_impl_params_1() {
128     trait T { fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a>; }
129     struct S;
130     impl T for S {
131         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
132             let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
133             //~^ ERROR E0373
134             //~| ERROR E0373
135             return Box::new(f);
136         }
137     }
138
139     // (we don't call `g`; see above)
140 }
141
142 fn escaping_borrow_of_trait_impl_params_2() {
143     trait T { fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a>; }
144     struct S;
145     impl T for S {
146         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
147             let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
148             //~^ ERROR E0373
149             //~| ERROR E0373
150             Box::new(f)
151         }
152     }
153     // (we don't call `g`; see above)
154 }
155
156 fn move_of_trait_impl_params() {
157     trait T { fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a>; }
158     struct S;
159     impl T for S {
160         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
161             let f = move |t: bool| if t { x } else { y };
162             return Box::new(f);
163         }
164     }
165     // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
166     (S.g(1,2))(true);
167 }
168
169 // TRAIT DEFAULT METHODS
170
171 fn escaping_borrow_of_trait_default_params_1() {
172     struct S;
173     trait T {
174         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
175             let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
176             //~^ ERROR E0373
177             //~| ERROR E0373
178             return Box::new(f);
179         }
180     }
181     impl T for S {}
182     // (we don't call `g`; see above)
183 }
184
185 fn escaping_borrow_of_trait_default_params_2() {
186     struct S;
187     trait T {
188         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
189             let f = |t: bool| if t { x } else { y }; // (separate errors for `x` vs `y`)
190             //~^ ERROR E0373
191             //~| ERROR E0373
192             Box::new(f)
193         }
194     }
195     impl T for S {}
196     // (we don't call `g`; see above)
197 }
198
199 fn move_of_trait_default_params() {
200     struct S;
201     trait T {
202         fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a> {
203             let f = move |t: bool| if t { x } else { y };
204             return Box::new(f);
205         }
206     }
207     impl T for S {}
208     // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
209     (S.g(1,2))(true);
210 }
211
212 fn main() { }
213