]>
Commit | Line | Data |
---|---|---|
416331ca XL |
1 | // run-pass |
2 | ||
0bf4aa26 XL |
3 | #![allow(non_snake_case)] |
4 | #![allow(dead_code)] | |
5 | #![allow(unused_variables)] | |
1a4d82fc JJ |
6 | // Test that the lifetime of rvalues in for loops is extended |
7 | // to the for loop itself. | |
1a4d82fc JJ |
8 | static mut FLAGS: u64 = 0; |
9 | ||
10 | struct Box<T> { f: T } | |
11 | struct AddFlags { bits: u64 } | |
12 | ||
13 | fn AddFlags(bits: u64) -> AddFlags { | |
14 | AddFlags { bits: bits } | |
15 | } | |
16 | ||
17 | fn arg(exp: u64, _x: &AddFlags) { | |
18 | check_flags(exp); | |
19 | } | |
20 | ||
21 | fn pass<T>(v: T) -> T { | |
22 | v | |
23 | } | |
24 | ||
25 | fn check_flags(exp: u64) { | |
26 | unsafe { | |
27 | let x = FLAGS; | |
28 | FLAGS = 0; | |
29 | println!("flags {}, expected {}", x, exp); | |
30 | assert_eq!(x, exp); | |
31 | } | |
32 | } | |
33 | ||
34 | impl AddFlags { | |
35 | fn check_flags(&self, exp: u64) -> &AddFlags { | |
36 | check_flags(exp); | |
37 | self | |
38 | } | |
39 | ||
40 | fn bits(&self) -> u64 { | |
41 | self.bits | |
42 | } | |
43 | } | |
44 | ||
45 | impl Drop for AddFlags { | |
46 | fn drop(&mut self) { | |
47 | unsafe { | |
48 | FLAGS = FLAGS + self.bits; | |
49 | } | |
50 | } | |
51 | } | |
52 | ||
53 | pub fn main() { | |
54 | // The array containing [AddFlags] should not be dropped until | |
55 | // after the for loop: | |
85aaf69f | 56 | for x in &[AddFlags(1)] { |
1a4d82fc JJ |
57 | check_flags(0); |
58 | } | |
59 | check_flags(1); | |
60 | } |