1 // rust-lang/rust#45696: This test is checking that we *cannot* return
2 // mutable borrows that would be scribbled over by destructors before
5 // We will explicitly test AST-borrowck, NLL, and migration modes;
6 // thus we will also skip the automated compare-mode=nll.
8 // revisions: ast nll migrate
9 // ignore-compare-mode-nll
11 // This test is going to pass in the ast and migrate revisions,
12 // because the AST-borrowck accepted this code in the past (see notes
13 // below). So we use `#[rustc_error]` to keep the outcome as an error
14 // in all scenarios, and rely on the stderr files to show what the
15 // actual behavior is. (See rust-lang/rust#49855.)
16 #![feature(rustc_attrs)]
18 #![cfg_attr(nll, feature(nll))]
19 //[migrate]compile-flags: -Z borrowck=migrate -Z two-phase-borrows
21 struct Scribble<'a>(&'a mut u32);
23 impl<'a> Drop for Scribble<'a> { fn drop(&mut self) { *self.0 = 42; } }
25 // this is okay, in both AST-borrowck and NLL: The `Scribble` here *has*
26 // to strictly outlive `'a`
27 fn borrowed_scribble<'a>(s: &'a mut Scribble) -> &'a mut u32 {
31 // this, by analogy to previous case, is also okay.
32 fn boxed_borrowed_scribble<'a>(s: Box<&'a mut Scribble>) -> &'a mut u32 {
36 // this, by analogy to previous case, is also okay.
37 fn boxed_boxed_borrowed_scribble<'a>(s: Box<Box<&'a mut Scribble>>) -> &'a mut u32 {
41 // this is not okay: in between the time that we take the mutable
42 // borrow and the caller receives it as a return value, the drop of
43 // `s` will scribble on it, violating our aliasing guarantees.
45 // * (Maybe in the future the two-phase borrows system will be
46 // extended to support this case. But for now, it is an error in
47 // NLL, even with two-phase borrows.)
49 // In any case, the AST-borrowck was not smart enough to know that
50 // this should be an error. (Which is perhaps the essence of why
51 // rust-lang/rust#45696 arose in the first place.)
52 fn scribbled<'a>(s: Scribble<'a>) -> &'a mut u32 {
53 &mut *s.0 //[nll]~ ERROR borrow may still be in use when destructor runs [E0713]
54 //[migrate]~^ WARNING borrow may still be in use when destructor runs [E0713]
55 //[migrate]~| WARNING this error has been downgraded to a warning for backwards compatibility
56 //[migrate]~| WARNING this represents potential undefined behavior in your code
59 // This, by analogy to previous case, is *also* not okay.
61 // (But again, AST-borrowck was not smart enogh to know that this
62 // should be an error.)
63 fn boxed_scribbled<'a>(s: Box<Scribble<'a>>) -> &'a mut u32 {
64 &mut *(*s).0 //[nll]~ ERROR borrow may still be in use when destructor runs [E0713]
65 //[migrate]~^ WARNING borrow may still be in use when destructor runs [E0713]
66 //[migrate]~| WARNING this error has been downgraded to a warning for backwards compatibility
67 //[migrate]~| WARNING this represents potential undefined behavior in your code
70 // This, by analogy to previous case, is *also* not okay.
72 // (But again, AST-borrowck was not smart enogh to know that this
73 // should be an error.)
74 fn boxed_boxed_scribbled<'a>(s: Box<Box<Scribble<'a>>>) -> &'a mut u32 {
75 &mut *(**s).0 //[nll]~ ERROR borrow may still be in use when destructor runs [E0713]
76 //[migrate]~^ WARNING borrow may still be in use when destructor runs [E0713]
77 //[migrate]~| WARNING this error has been downgraded to a warning for backwards compatibility
78 //[migrate]~| WARNING this represents potential undefined behavior in your code
82 fn main() { //[ast]~ ERROR compilation successful
83 //[migrate]~^ ERROR compilation successful
86 let mut long_lived = Scribble(&mut x);
87 *borrowed_scribble(&mut long_lived) += 10;
88 // (Scribble dtor runs here, after `&mut`-borrow above ends)
91 let mut long_lived = Scribble(&mut x);
92 *boxed_borrowed_scribble(Box::new(&mut long_lived)) += 10;
93 // (Scribble dtor runs here, after `&mut`-borrow above ends)
96 let mut long_lived = Scribble(&mut x);
97 *boxed_boxed_borrowed_scribble(Box::new(Box::new(&mut long_lived))) += 10;
98 // (Scribble dtor runs here, after `&mut`-borrow above ends)
100 *scribbled(Scribble(&mut x)) += 10;
101 *boxed_scribbled(Box::new(Scribble(&mut x))) += 10;
102 *boxed_boxed_scribbled(Box::new(Box::new(Scribble(&mut x)))) += 10;