]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-45696-scribble-on-boxed-borrow.rs
Auto merge of #56962 - nivkner:fixme_fixup4, r=pnkfelix
[rust.git] / src / test / ui / issues / issue-45696-scribble-on-boxed-borrow.rs
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.
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 // rust-lang/rust#45696: This test is checking that we *cannot* return
12 // mutable borrows that would be scribbled over by destructors before
13 // the return occurs.
14 //
15 // We will explicitly test AST-borrowck, NLL, and migration modes;
16 // thus we will also skip the automated compare-mode=nll.
17
18 // revisions: ast nll migrate
19 // ignore-compare-mode-nll
20
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)]
27
28 #![cfg_attr(nll, feature(nll))]
29 //[migrate]compile-flags: -Z borrowck=migrate -Z two-phase-borrows
30
31 struct Scribble<'a>(&'a mut u32);
32
33 impl<'a> Drop for Scribble<'a> { fn drop(&mut self) { *self.0 = 42; } }
34
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 {
38     &mut *s.0
39 }
40
41 // this, by analogy to previous case, is also okay.
42 fn boxed_borrowed_scribble<'a>(s: Box<&'a mut Scribble>) -> &'a mut u32 {
43     &mut *(*s).0
44 }
45
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 {
48     &mut *(**s).0
49 }
50
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.
54 //
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.)
58 //
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
67 }
68
69 // This, by analogy to previous case, is *also* not okay.
70 //
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
78 }
79
80 // This, by analogy to previous case, is *also* not okay.
81 //
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
89 }
90
91 #[rustc_error]
92 fn main() { //[ast]~ ERROR compilation successful
93      //[migrate]~^ ERROR compilation successful
94     let mut x = 1;
95     {
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)
99     }
100     {
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)
104     }
105     {
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)
109     }
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;
113 }