2 #![allow(unused_braces)]
3 #![allow(non_snake_case)]
4 #![allow(unused_variables)]
5 // Test that destructors for rvalue temporaries run either at end of
6 // statement or end of block, as appropriate given the temporary
9 #![feature(box_patterns)]
13 static mut FLAGS: u64 = 0;
15 struct Box<T> { f: T }
16 struct AddFlags { bits: u64 }
18 fn AddFlags(bits: u64) -> AddFlags {
19 AddFlags { bits: bits }
22 fn arg(exp: u64, _x: &AddFlags) {
26 fn pass<T>(v: T) -> T {
30 fn check_flags(exp: u64) {
34 println!("flags {}, expected {}", x, exp);
40 fn check_flags<'a>(&'a self, exp: u64) -> &'a AddFlags {
45 fn bits(&self) -> u64 {
50 impl Drop for AddFlags {
53 FLAGS = FLAGS + self.bits;
58 macro_rules! end_of_block {
59 ($pat:pat, $expr:expr) => (
61 println!("end_of_block({})", stringify!({let $pat = $expr;}));
64 // Destructor here does not run until exit from the block.
73 macro_rules! end_of_stmt {
74 ($pat:pat, $expr:expr) => (
76 println!("end_of_stmt({})", stringify!($expr));
79 // Destructor here run after `let` statement
92 // In all these cases, we trip over the rules designed to cover
93 // the case where we are taking addr of rvalue and storing that
94 // addr into a stack slot, either via `let ref` or via a `&` in
97 end_of_block!(_x, AddFlags(1));
98 end_of_block!(_x, &AddFlags(1));
99 end_of_block!(_x, & &AddFlags(1));
100 end_of_block!(_x, Box { f: AddFlags(1) });
101 end_of_block!(_x, Box { f: &AddFlags(1) });
102 end_of_block!(_x, Box { f: &AddFlags(1) });
103 end_of_block!(_x, pass(AddFlags(1)));
104 end_of_block!(ref _x, AddFlags(1));
105 end_of_block!(AddFlags { bits: ref _x }, AddFlags(1));
106 end_of_block!(&AddFlags { bits }, &AddFlags(1));
107 end_of_block!((_, ref _y), (AddFlags(1), 22));
108 end_of_block!(box ref _x, std::boxed::Box::new(AddFlags(1)));
109 end_of_block!(box _x, std::boxed::Box::new(AddFlags(1)));
110 end_of_block!(_, { { check_flags(0); &AddFlags(1) } });
111 end_of_block!(_, &((Box { f: AddFlags(1) }).f));
112 end_of_block!(_, &(([AddFlags(1)])[0]));
114 // LHS does not create a ref binding, so temporary lives as long
115 // as statement, and we do not move the AddFlags out:
116 end_of_stmt!(_, AddFlags(1));
117 end_of_stmt!((_, _), (AddFlags(1), 22));
119 // `&` operator appears inside an arg to a function,
120 // so it is not prolonged:
121 end_of_stmt!(ref _x, arg(0, &AddFlags(1)));
123 // autoref occurs inside receiver, so temp lifetime is not
125 end_of_stmt!(ref _x, AddFlags(1).check_flags(0).bits());
127 // No reference is created on LHS, thus RHS is moved into
128 // a temporary that lives just as long as the statement.
129 end_of_stmt!(AddFlags { bits }, AddFlags(1));