]> git.lizzy.rs Git - rust.git/blob - src/test/ui/cleanup-rvalue-scopes-cf.rs
Rollup merge of #93663 - sunfishcode:sunfishcode/as-raw-name, r=joshtriplett
[rust.git] / src / test / ui / cleanup-rvalue-scopes-cf.rs
1 // Test that the borrow checker prevents pointers to temporaries
2 // with statement lifetimes from escaping.
3
4 use std::ops::Drop;
5
6 static mut FLAGS: u64 = 0;
7
8 struct StackBox<T> { f: T }
9 struct AddFlags { bits: u64 }
10
11 fn AddFlags(bits: u64) -> AddFlags {
12     AddFlags { bits: bits }
13 }
14
15 fn arg(x: &AddFlags) -> &AddFlags {
16     x
17 }
18
19 impl AddFlags {
20     fn get(&self) -> &AddFlags {
21         self
22     }
23 }
24
25 pub fn main() {
26     let x1 = arg(&AddFlags(1)); //~ ERROR temporary value dropped while borrowed
27     let x2 = AddFlags(1).get(); //~ ERROR temporary value dropped while borrowed
28     let x3 = &*arg(&AddFlags(1)); //~ ERROR temporary value dropped while borrowed
29     let ref x4 = *arg(&AddFlags(1)); //~ ERROR temporary value dropped while borrowed
30     let &ref x5 = arg(&AddFlags(1)); //~ ERROR temporary value dropped while borrowed
31     let x6 = AddFlags(1).get(); //~ ERROR temporary value dropped while borrowed
32     let StackBox { f: x7 } = StackBox { f: AddFlags(1).get() };
33     //~^ ERROR temporary value dropped while borrowed
34     (x1, x2, x3, x4, x5, x6, x7);
35 }