]> git.lizzy.rs Git - rust.git/blob - tests/ui/cleanup-rvalue-for-scope.rs
Rollup merge of #106692 - eggyal:mv-binary_heap.rs-binary_heap/mod.rs, r=Mark-Simulacrum
[rust.git] / tests / ui / cleanup-rvalue-for-scope.rs
1 // run-pass
2
3 #![allow(non_snake_case)]
4 #![allow(dead_code)]
5 #![allow(unused_variables)]
6 // Test that the lifetime of rvalues in for loops is extended
7 // to the for loop itself.
8
9 use std::ops::Drop;
10
11 static mut FLAGS: u64 = 0;
12
13 struct Box<T> { f: T }
14 struct AddFlags { bits: u64 }
15
16 fn AddFlags(bits: u64) -> AddFlags {
17     AddFlags { bits: bits }
18 }
19
20 fn arg(exp: u64, _x: &AddFlags) {
21     check_flags(exp);
22 }
23
24 fn pass<T>(v: T) -> T {
25     v
26 }
27
28 fn check_flags(exp: u64) {
29     unsafe {
30         let x = FLAGS;
31         FLAGS = 0;
32         println!("flags {}, expected {}", x, exp);
33         assert_eq!(x, exp);
34     }
35 }
36
37 impl AddFlags {
38     fn check_flags(&self, exp: u64) -> &AddFlags {
39         check_flags(exp);
40         self
41     }
42
43     fn bits(&self) -> u64 {
44         self.bits
45     }
46 }
47
48 impl Drop for AddFlags {
49     fn drop(&mut self) {
50         unsafe {
51             FLAGS = FLAGS + self.bits;
52         }
53     }
54 }
55
56 pub fn main() {
57     // The array containing [AddFlags] should not be dropped until
58     // after the for loop:
59     for x in &[AddFlags(1)] {
60         check_flags(0);
61     }
62     check_flags(1);
63 }