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