2014-02-09 06:44:10 -06:00
|
|
|
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
|
2014-02-10 05:24:32 -06:00
|
|
|
// This test verifies that temporaries created for `while`'s and `if`
|
|
|
|
// conditions are dropped after the condition is evaluated.
|
2014-02-09 06:44:10 -06:00
|
|
|
|
|
|
|
struct Temporary;
|
|
|
|
|
|
|
|
static mut DROPPED: int = 0;
|
|
|
|
|
|
|
|
impl Drop for Temporary {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
unsafe { DROPPED += 1; }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Temporary {
|
2014-02-10 17:19:27 -06:00
|
|
|
fn do_stuff(&self) -> bool {true}
|
2014-02-09 06:44:10 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn borrow() -> ~Temporary { ~Temporary }
|
|
|
|
|
|
|
|
|
|
|
|
pub fn main() {
|
|
|
|
let mut i = 0;
|
|
|
|
|
|
|
|
// This loop's condition
|
|
|
|
// should call `Temporary`'s
|
|
|
|
// `drop` 6 times.
|
2014-02-10 17:19:27 -06:00
|
|
|
while borrow().do_stuff() {
|
2014-02-09 06:44:10 -06:00
|
|
|
i += 1;
|
2014-02-10 05:24:32 -06:00
|
|
|
unsafe { assert_eq!(DROPPED, i) }
|
2014-02-09 06:44:10 -06:00
|
|
|
if i > 5 {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This if condition should
|
|
|
|
// call it 1 time
|
2014-02-10 17:19:27 -06:00
|
|
|
if borrow().do_stuff() {
|
2014-02-10 05:24:32 -06:00
|
|
|
unsafe { assert_eq!(DROPPED, i + 1) }
|
2014-02-09 06:44:10 -06:00
|
|
|
}
|
|
|
|
}
|