1 // Copyright 2014 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 // Test that destructors for rvalue temporaries run either at end of
12 // statement or end of block, as appropriate given the temporary
15 #![feature(macro_rules)]
19 static mut FLAGS: u64 = 0;
21 struct Box<T> { f: T }
22 struct AddFlags { bits: u64 }
24 fn AddFlags(bits: u64) -> AddFlags {
25 AddFlags { bits: bits }
28 fn arg(exp: u64, _x: &AddFlags) {
32 fn pass<T>(v: T) -> T {
36 fn check_flags(exp: u64) {
40 println!("flags {}, expected {}", x, exp);
46 fn check_flags<'a>(&'a self, exp: u64) -> &'a AddFlags {
51 fn bits(&self) -> u64 {
56 impl Drop for AddFlags {
59 FLAGS = FLAGS + self.bits;
64 macro_rules! end_of_block(
65 ($pat:pat, $expr:expr) => (
67 println!("end_of_block({})", stringify!({let $pat = $expr;}));
70 // Destructor here does not run until exit from the block.
79 macro_rules! end_of_stmt(
80 ($pat:pat, $expr:expr) => (
82 println!("end_of_stmt({})", stringify!($expr));
85 // Destructor here run after `let` statement
98 // In all these cases, we trip over the rules designed to cover
99 // the case where we are taking addr of rvalue and storing that
100 // addr into a stack slot, either via `let ref` or via a `&` in
103 end_of_block!(_x, AddFlags(1));
104 end_of_block!(_x, &AddFlags(1));
105 end_of_block!(_x, & &AddFlags(1));
106 end_of_block!(_x, Box { f: AddFlags(1) });
107 end_of_block!(_x, Box { f: &AddFlags(1) });
108 end_of_block!(_x, Box { f: &AddFlags(1) });
109 end_of_block!(_x, pass(AddFlags(1)));
110 end_of_block!(ref _x, AddFlags(1));
111 end_of_block!(AddFlags { bits: ref _x }, AddFlags(1));
112 end_of_block!(&AddFlags { bits }, &AddFlags(1));
113 end_of_block!((_, ref _y), (AddFlags(1), 22i));
114 end_of_block!(box ref _x, box AddFlags(1));
115 end_of_block!(box _x, box AddFlags(1));
116 end_of_block!(_, { { check_flags(0); &AddFlags(1) } });
117 end_of_block!(_, &((Box { f: AddFlags(1) }).f));
118 end_of_block!(_, &(([AddFlags(1)])[0]));
120 // LHS does not create a ref binding, so temporary lives as long
121 // as statement, and we do not move the AddFlags out:
122 end_of_stmt!(_, AddFlags(1));
123 end_of_stmt!((_, _), (AddFlags(1), 22i));
125 // `&` operator appears inside an arg to a function,
126 // so it is not prolonged:
127 end_of_stmt!(ref _x, arg(0, &AddFlags(1)));
129 // autoref occurs inside receiver, so temp lifetime is not
131 end_of_stmt!(ref _x, AddFlags(1).check_flags(0).bits());
133 // No reference is created on LHS, thus RHS is moved into
134 // a temporary that lives just as long as the statement.
135 end_of_stmt!(AddFlags { bits }, AddFlags(1));