]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/cleanup-rvalue-scopes.rs
6d1c3aab66257bfd1c0c1dc56bed484e12b9486b
[rust.git] / src / test / run-pass / cleanup-rvalue-scopes.rs
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.
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 // Test that destructors for rvalue temporaries run either at end of
12 // statement or end of block, as appropriate given the temporary
13 // lifetime rules.
14
15 #[feature(macro_rules)];
16
17 use std::ops::Drop;
18
19 static mut FLAGS: u64 = 0;
20
21 struct Box<T> { f: T }
22 struct AddFlags { bits: u64 }
23
24 fn AddFlags(bits: u64) -> AddFlags {
25     AddFlags { bits: bits }
26 }
27
28 fn arg(exp: u64, _x: &AddFlags) {
29     check_flags(exp);
30 }
31
32 fn pass<T>(v: T) -> T {
33     v
34 }
35
36 fn check_flags(exp: u64) {
37     unsafe {
38         let x = FLAGS;
39         FLAGS = 0;
40         println!("flags {}, expected {}", x, exp);
41         assert_eq!(x, exp);
42     }
43 }
44
45 impl AddFlags {
46     fn check_flags<'a>(&'a self, exp: u64) -> &'a AddFlags {
47         check_flags(exp);
48         self
49     }
50
51     fn bits(&self) -> u64 {
52         self.bits
53     }
54 }
55
56 impl Drop for AddFlags {
57     fn drop(&mut self) {
58         unsafe {
59             FLAGS = FLAGS + self.bits;
60         }
61     }
62 }
63
64 macro_rules! end_of_block(
65     ($pat:pat, $expr:expr) => (
66         {
67             println!("end_of_block({})", stringify!({let $pat = $expr;}));
68
69             {
70                 // Destructor here does not run until exit from the block.
71                 let $pat = $expr;
72                 check_flags(0);
73             }
74             check_flags(1);
75         }
76     )
77 )
78
79 macro_rules! end_of_stmt(
80     ($pat:pat, $expr:expr) => (
81         {
82             println!("end_of_stmt({})", stringify!($expr));
83
84             {
85                 // Destructor here run after `let` statement
86                 // terminates.
87                 let $pat = $expr;
88                 check_flags(1);
89             }
90
91             check_flags(0);
92         }
93     )
94 )
95
96 pub fn main() {
97
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
101     // the initializer.
102
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), 22));
114     end_of_block!(~ref _x, ~AddFlags(1));
115     end_of_block!(~_x, ~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]));
119     end_of_block!(_, &((&vec!(AddFlags(1)))[0]));
120
121     // LHS does not create a ref binding, so temporary lives as long
122     // as statement, and we do not move the AddFlags out:
123     end_of_stmt!(_, AddFlags(1));
124     end_of_stmt!((_, _), (AddFlags(1), 22));
125
126     // `&` operator appears inside an arg to a function,
127     // so it is not prolonged:
128     end_of_stmt!(ref _x, arg(0, &AddFlags(1)));
129
130     // autoref occurs inside receiver, so temp lifetime is not
131     // prolonged:
132     end_of_stmt!(ref _x, AddFlags(1).check_flags(0).bits());
133
134     // No reference is created on LHS, thus RHS is moved into
135     // a temporary that lives just as long as the statement.
136     end_of_stmt!(AddFlags { bits }, AddFlags(1));
137 }