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