]> git.proxmox.com Git - rustc.git/blob - src/test/run-pass/generator/conditional-drop.rs
New upstream version 1.26.0+dfsg1
[rustc.git] / src / test / run-pass / generator / conditional-drop.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![feature(generators, generator_trait)]
12
13 use std::ops::Generator;
14 use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
15
16 static A: AtomicUsize = ATOMIC_USIZE_INIT;
17
18 struct B;
19
20 impl Drop for B {
21 fn drop(&mut self) {
22 A.fetch_add(1, Ordering::SeqCst);
23 }
24 }
25
26
27 fn test() -> bool { true }
28 fn test2() -> bool { false }
29
30 fn main() {
31 t1();
32 t2();
33 }
34
35 fn t1() {
36 let mut a = || {
37 let b = B;
38 if test() {
39 drop(b);
40 }
41 yield;
42 };
43
44 let n = A.load(Ordering::SeqCst);
45 unsafe { a.resume() };
46 assert_eq!(A.load(Ordering::SeqCst), n + 1);
47 unsafe { a.resume() };
48 assert_eq!(A.load(Ordering::SeqCst), n + 1);
49 }
50
51 fn t2() {
52 let mut a = || {
53 let b = B;
54 if test2() {
55 drop(b);
56 }
57 yield;
58 };
59
60 let n = A.load(Ordering::SeqCst);
61 unsafe { a.resume() };
62 assert_eq!(A.load(Ordering::SeqCst), n);
63 unsafe { a.resume() };
64 assert_eq!(A.load(Ordering::SeqCst), n + 1);
65 }