]> git.proxmox.com Git - rustc.git/blame - 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
CommitLineData
ea8adc8c
XL
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
13use std::ops::Generator;
14use std::sync::atomic::{AtomicUsize, ATOMIC_USIZE_INIT, Ordering};
15
16static A: AtomicUsize = ATOMIC_USIZE_INIT;
17
18struct B;
19
20impl Drop for B {
21 fn drop(&mut self) {
22 A.fetch_add(1, Ordering::SeqCst);
23 }
24}
25
26
27fn test() -> bool { true }
28fn test2() -> bool { false }
29
30fn main() {
31 t1();
32 t2();
33}
34
35fn 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);
0531ce1d 45 unsafe { a.resume() };
ea8adc8c 46 assert_eq!(A.load(Ordering::SeqCst), n + 1);
0531ce1d 47 unsafe { a.resume() };
ea8adc8c
XL
48 assert_eq!(A.load(Ordering::SeqCst), n + 1);
49}
50
51fn 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);
0531ce1d 61 unsafe { a.resume() };
ea8adc8c 62 assert_eq!(A.load(Ordering::SeqCst), n);
0531ce1d 63 unsafe { a.resume() };
ea8adc8c
XL
64 assert_eq!(A.load(Ordering::SeqCst), n + 1);
65}