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)]
10 #![feature(box_syntax)]
14 static mut FLAGS: u64 = 0;
16 struct Box<T> { f: T }
17 struct AddFlags { bits: u64 }
19 fn AddFlags(bits: u64) -> AddFlags {
20 AddFlags { bits: bits }
23 fn arg(exp: u64, _x: &AddFlags) {
27 fn pass<T>(v: T) -> T {
31 fn check_flags(exp: u64) {
35 println!("flags {}, expected {}", x, exp);
41 fn check_flags<'a>(&'a self, exp: u64) -> &'a AddFlags {
46 fn bits(&self) -> u64 {
51 impl Drop for AddFlags {
54 FLAGS = FLAGS + self.bits;
59 macro_rules! end_of_block {
60 ($pat:pat, $expr:expr) => (
62 println!("end_of_block({})", stringify!({let $pat = $expr;}));
65 // Destructor here does not run until exit from the block.
74 macro_rules! end_of_stmt {
75 ($pat:pat, $expr:expr) => (
77 println!("end_of_stmt({})", stringify!($expr));
80 // Destructor here run after `let` statement
93 // In all these cases, we trip over the rules designed to cover
94 // the case where we are taking addr of rvalue and storing that
95 // addr into a stack slot, either via `let ref` or via a `&` in
98 end_of_block!(_x, AddFlags(1));
99 end_of_block!(_x, &AddFlags(1));
100 end_of_block!(_x, & &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, Box { f: &AddFlags(1) });
104 end_of_block!(_x, pass(AddFlags(1)));
105 end_of_block!(ref _x, AddFlags(1));
106 end_of_block!(AddFlags { bits: ref _x }, AddFlags(1));
107 end_of_block!(&AddFlags { bits }, &AddFlags(1));
108 end_of_block!((_, ref _y), (AddFlags(1), 22));
109 end_of_block!(box ref _x, box AddFlags(1));
110 end_of_block!(box _x, box AddFlags(1));
111 end_of_block!(_, { { check_flags(0); &AddFlags(1) } });
112 end_of_block!(_, &((Box { f: AddFlags(1) }).f));
113 end_of_block!(_, &(([AddFlags(1)])[0]));
115 // LHS does not create a ref binding, so temporary lives as long
116 // as statement, and we do not move the AddFlags out:
117 end_of_stmt!(_, AddFlags(1));
118 end_of_stmt!((_, _), (AddFlags(1), 22));
120 // `&` operator appears inside an arg to a function,
121 // so it is not prolonged:
122 end_of_stmt!(ref _x, arg(0, &AddFlags(1)));
124 // autoref occurs inside receiver, so temp lifetime is not
126 end_of_stmt!(ref _x, AddFlags(1).check_flags(0).bits());
128 // No reference is created on LHS, thus RHS is moved into
129 // a temporary that lives just as long as the statement.
130 end_of_stmt!(AddFlags { bits }, AddFlags(1));