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.
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.
11 // Issue #29793, big regression test: do not let borrows of
12 // parameters to ever be returned (expanded with exploration of
15 // This is the version of the test that actually exposed unsound
16 // behavior (because the improperly accepted closure was actually
17 // able to be invoked).
19 // ignore-tidy-linelength
21 //[mir]compile-flags: -Z borrowck=mir
23 struct WrapA<F>(Option<F>);
26 fn new() -> WrapA<F> {
29 fn set(mut self, f: F) -> Self {
35 struct WrapB<F>(Option<F>);
38 fn new() -> WrapB<F> {
41 fn set(mut self, f: F) -> Self {
47 trait DoStuff : Sized {
51 impl<F, T> DoStuff for WrapA<F>
52 where F: FnMut(usize, usize) -> T, T: DoStuff {
54 if let Some(ref mut f) = self.0 {
56 let _foo = [0usize; 16];
62 impl<F> DoStuff for WrapB<F> where F: FnMut(bool) -> usize {
64 if let Some(ref mut f) = self.0 {
65 println!("{}", f(true));
71 where F: FnMut(usize, usize) -> T, T: DoStuff {
72 fn handle_ref(&mut self) {
73 if let Some(ref mut f) = self.0 {
80 let mut w = WrapA::new().set(|x: usize, y: usize| {
81 WrapB::new().set(|t: bool| if t { x } else { y }) // (separate errors for `x` vs `y`)
82 //[ast]~^ ERROR `x` does not live long enough
83 //[ast]~| ERROR `y` does not live long enough
84 //[mir]~^^^ ERROR `x` does not live long enough
85 //[mir]~| ERROR `y` does not live long enough
88 w.handle(); // This works
89 // w.handle_ref(); // This doesn't