summaryrefslogtreecommitdiffstats
path: root/src/test/ui/generator/drop-control-flow.rs
blob: d383680002f4b9fcdde3eadb3a3d6c7d0f923293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// build-pass
// compile-flags: -Zdrop-tracking

// A test to ensure generators capture values that were conditionally dropped,
// and also that values that are dropped along all paths to a yield do not get
// included in the generator type.

#![feature(generators, negative_impls)]
#![allow(unused_assignments, dead_code)]

struct Ptr;
impl<'a> Drop for Ptr {
    fn drop(&mut self) {}
}

struct NonSend;
impl !Send for NonSend {}

fn assert_send<T: Send>(_: T) {}

// This test case is reduced from src/test/ui/drop/dynamic-drop-async.rs
fn one_armed_if(arg: bool) {
    let _ = || {
        let arr = [Ptr];
        if arg {
            drop(arr);
        }
        yield;
    };
}

fn two_armed_if(arg: bool) {
    assert_send(|| {
        let arr = [Ptr];
        if arg {
            drop(arr);
        } else {
            drop(arr);
        }
        yield;
    })
}

fn if_let(arg: Option<i32>) {
    let _ = || {
        let arr = [Ptr];
        if let Some(_) = arg {
            drop(arr);
        }
        yield;
    };
}

fn init_in_if(arg: bool) {
    assert_send(|| {
        let mut x = NonSend;
        drop(x);
        if arg {
            x = NonSend;
        } else {
            yield;
        }
    })
}

fn init_in_match_arm(arg: Option<i32>) {
    assert_send(|| {
        let mut x = NonSend;
        drop(x);
        match arg {
            Some(_) => x = NonSend,
            None => yield,
        }
    })
}

fn reinit() {
    let _ = || {
        let mut arr = [Ptr];
        drop(arr);
        arr = [Ptr];
        yield;
    };
}

fn loop_uninit() {
    let _ = || {
        let mut arr = [Ptr];
        let mut count = 0;
        drop(arr);
        while count < 3 {
            yield;
            arr = [Ptr];
            count += 1;
        }
    };
}

fn nested_loop() {
    let _ = || {
        let mut arr = [Ptr];
        let mut count = 0;
        drop(arr);
        while count < 3 {
            for _ in 0..3 {
                yield;
            }
            arr = [Ptr];
            count += 1;
        }
    };
}

fn loop_continue(b: bool) {
    let _ = || {
        let mut arr = [Ptr];
        let mut count = 0;
        drop(arr);
        while count < 3 {
            count += 1;
            yield;
            if b {
                arr = [Ptr];
                continue;
            }
        }
    };
}

fn main() {
    one_armed_if(true);
    if_let(Some(41));
    init_in_if(true);
    init_in_match_arm(Some(41));
    reinit();
    loop_uninit();
    nested_loop();
    loop_continue(true);
}