2019-07-27 00:54:25 +03:00
|
|
|
// run-pass
|
2014-02-10 06:24:32 -05:00
|
|
|
// This test verifies that temporaries created for `while`'s and `if`
|
|
|
|
// conditions are dropped after the condition is evaluated.
|
2014-02-09 13:44:10 +01:00
|
|
|
|
|
|
|
struct Temporary;
|
|
|
|
|
2015-03-25 17:06:52 -07:00
|
|
|
static mut DROPPED: isize = 0;
|
2014-02-09 13:44:10 +01:00
|
|
|
|
|
|
|
impl Drop for Temporary {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { DROPPED += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Temporary {
|
2014-02-11 00:19:27 +01:00
|
|
|
fn do_stuff(&self) -> bool {true}
|
2014-02-09 13:44:10 +01:00
|
|
|
}
|
|
|
|
|
2021-08-25 02:39:40 +02:00
|
|
|
fn borrow() -> Box<Temporary> { Box::new(Temporary) }
|
2014-02-09 13:44:10 +01:00
|
|
|
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
// This loop's condition
|
|
|
|
// should call `Temporary`'s
|
|
|
|
// `drop` 6 times.
|
2014-02-11 00:19:27 +01:00
|
|
|
while borrow().do_stuff() {
|
2014-02-09 13:44:10 +01:00
|
|
|
i += 1;
|
2014-02-10 06:24:32 -05:00
|
|
|
unsafe { assert_eq!(DROPPED, i) }
|
2014-02-09 13:44:10 +01:00
|
|
|
if i > 5 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This if condition should
|
|
|
|
// call it 1 time
|
2014-02-11 00:19:27 +01:00
|
|
|
if borrow().do_stuff() {
|
2014-02-10 06:24:32 -05:00
|
|
|
unsafe { assert_eq!(DROPPED, i + 1) }
|
2014-02-09 13:44:10 +01:00
|
|
|
}
|
|
|
|
}
|