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 `*s.0` does not live long enough [E0597]
64 //[migrate]~^ WARNING `*s.0` does not live long enough [E0597]
65 //[migrate]~| WARNING This error has been downgraded to a warning for backwards compatibility
68 // This, by analogy to previous case, is *also* not okay.
70 // (But again, AST-borrowck was not smart enogh to know that this
71 // should be an error.)
72 fn boxed_scribbled<'a>(s: Box<Scribble<'a>>) -> &'a mut u32 {
73 &mut *(*s).0 //[nll]~ ERROR `*s.0` does not live long enough [E0597]
74 //[migrate]~^ WARNING `*s.0` does not live long enough [E0597]
75 //[migrate]~| WARNING This error has been downgraded to a warning for backwards compatibility
78 // This, by analogy to previous case, is *also* not okay.
80 // (But again, AST-borrowck was not smart enogh to know that this
81 // should be an error.)
82 fn boxed_boxed_scribbled<'a>(s: Box<Box<Scribble<'a>>>) -> &'a mut u32 {
83 &mut *(**s).0 //[nll]~ ERROR `*s.0` does not live long enough [E0597]
84 //[migrate]~^ WARNING `*s.0` does not live long enough [E0597]
85 //[migrate]~| WARNING This error has been downgraded to a warning for backwards compatibility
89 fn main() { //[ast]~ ERROR compilation successful
90 //[migrate]~^ ERROR compilation successful
93 let mut long_lived = Scribble(&mut x);
94 *borrowed_scribble(&mut long_lived) += 10;
95 // (Scribble dtor runs here, after `&mut`-borrow above ends)
98 let mut long_lived = Scribble(&mut x);
99 *boxed_borrowed_scribble(Box::new(&mut long_lived)) += 10;
100 // (Scribble dtor runs here, after `&mut`-borrow above ends)
103 let mut long_lived = Scribble(&mut x);
104 *boxed_boxed_borrowed_scribble(Box::new(Box::new(&mut long_lived))) += 10;
105 // (Scribble dtor runs here, after `&mut`-borrow above ends)
107 *scribbled(Scribble(&mut x)) += 10;
108 *boxed_scribbled(Box::new(Scribble(&mut x))) += 10;
109 *boxed_boxed_scribbled(Box::new(Box::new(Scribble(&mut x)))) += 10;