1 // Issue #29793, small regression tests: do not let borrows of
2 // parameters to ever be returned (expanded with exploration of
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
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.
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
30 // (we don't call `g`; see above)
33 fn move_of_closure_params() {
34 let g = |x: usize, y:usize| {
35 let f = move |t: bool| if t { x } else { y };
38 // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
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 };
47 // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
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`)
61 // (we don't call `g`; see above)
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`)
72 // (we don't call `g`; see above)
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 };
80 // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
86 fn escaping_borrow_of_method_params_1() {
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`)
97 // (we don't call `g`; see above)
100 fn escaping_borrow_of_method_params_2() {
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`)
110 // (we don't call `g`; see above)
113 fn move_of_method_params() {
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 };
121 // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
125 // TRAIT IMPL METHODS
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>; }
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`)
139 // (we don't call `g`; see above)
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>; }
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`)
153 // (we don't call `g`; see above)
156 fn move_of_trait_impl_params() {
157 trait T { fn g<'a>(&self, x: usize, y:usize) -> Box<Fn(bool) -> usize + 'a>; }
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 };
165 // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)
169 // TRAIT DEFAULT METHODS
171 fn escaping_borrow_of_trait_default_params_1() {
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`)
182 // (we don't call `g`; see above)
185 fn escaping_borrow_of_trait_default_params_2() {
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`)
196 // (we don't call `g`; see above)
199 fn move_of_trait_default_params() {
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 };
208 // (this code is fine, so lets go ahead and ensure rustc accepts call of `g`)