]> git.lizzy.rs Git - rust.git/blob - tests/ui/for-loop-while/cleanup-rvalue-during-if-and-while.rs
Move /src/test to /tests
[rust.git] / tests / ui / for-loop-while / cleanup-rvalue-during-if-and-while.rs
1 // run-pass
2 // This test verifies that temporaries created for `while`'s and `if`
3 // conditions are dropped after the condition is evaluated.
4
5 struct Temporary;
6
7 static mut DROPPED: isize = 0;
8
9 impl Drop for Temporary {
10     fn drop(&mut self) {
11         unsafe { DROPPED += 1; }
12     }
13 }
14
15 impl Temporary {
16     fn do_stuff(&self) -> bool {true}
17 }
18
19 fn borrow() -> Box<Temporary> { Box::new(Temporary) }
20
21
22 pub fn main() {
23     let mut i = 0;
24
25     // This loop's condition
26     // should call `Temporary`'s
27     // `drop` 6 times.
28     while borrow().do_stuff() {
29         i += 1;
30         unsafe { assert_eq!(DROPPED, i) }
31         if i > 5 {
32             break;
33         }
34     }
35
36     // This if condition should
37     // call it 1 time
38     if borrow().do_stuff() {
39         unsafe { assert_eq!(DROPPED, i + 1) }
40     }
41 }