]> git.lizzy.rs Git - rust.git/blob - tests/ui/generator/drop-control-flow.rs
Rollup merge of #106714 - Ezrashaw:remove-e0490, r=davidtwco
[rust.git] / tests / ui / generator / drop-control-flow.rs
1 // build-pass
2 // compile-flags: -Zdrop-tracking
3
4 // A test to ensure generators capture values that were conditionally dropped,
5 // and also that values that are dropped along all paths to a yield do not get
6 // included in the generator type.
7
8 #![feature(generators, negative_impls)]
9 #![allow(unused_assignments, dead_code)]
10
11 struct Ptr;
12 impl<'a> Drop for Ptr {
13     fn drop(&mut self) {}
14 }
15
16 struct NonSend;
17 impl !Send for NonSend {}
18
19 fn assert_send<T: Send>(_: T) {}
20
21 // This test case is reduced from tests/ui/drop/dynamic-drop-async.rs
22 fn one_armed_if(arg: bool) {
23     let _ = || {
24         let arr = [Ptr];
25         if arg {
26             drop(arr);
27         }
28         yield;
29     };
30 }
31
32 fn two_armed_if(arg: bool) {
33     assert_send(|| {
34         let arr = [Ptr];
35         if arg {
36             drop(arr);
37         } else {
38             drop(arr);
39         }
40         yield;
41     })
42 }
43
44 fn if_let(arg: Option<i32>) {
45     let _ = || {
46         let arr = [Ptr];
47         if let Some(_) = arg {
48             drop(arr);
49         }
50         yield;
51     };
52 }
53
54 fn init_in_if(arg: bool) {
55     assert_send(|| {
56         let mut x = NonSend;
57         drop(x);
58         if arg {
59             x = NonSend;
60         } else {
61             yield;
62         }
63     })
64 }
65
66 fn init_in_match_arm(arg: Option<i32>) {
67     assert_send(|| {
68         let mut x = NonSend;
69         drop(x);
70         match arg {
71             Some(_) => x = NonSend,
72             None => yield,
73         }
74     })
75 }
76
77 fn reinit() {
78     let _ = || {
79         let mut arr = [Ptr];
80         drop(arr);
81         arr = [Ptr];
82         yield;
83     };
84 }
85
86 fn loop_uninit() {
87     let _ = || {
88         let mut arr = [Ptr];
89         let mut count = 0;
90         drop(arr);
91         while count < 3 {
92             yield;
93             arr = [Ptr];
94             count += 1;
95         }
96     };
97 }
98
99 fn nested_loop() {
100     let _ = || {
101         let mut arr = [Ptr];
102         let mut count = 0;
103         drop(arr);
104         while count < 3 {
105             for _ in 0..3 {
106                 yield;
107             }
108             arr = [Ptr];
109             count += 1;
110         }
111     };
112 }
113
114 fn loop_continue(b: bool) {
115     let _ = || {
116         let mut arr = [Ptr];
117         let mut count = 0;
118         drop(arr);
119         while count < 3 {
120             count += 1;
121             yield;
122             if b {
123                 arr = [Ptr];
124                 continue;
125             }
126         }
127     };
128 }
129
130 fn main() {
131     one_armed_if(true);
132     if_let(Some(41));
133     init_in_if(true);
134     init_in_match_arm(Some(41));
135     reinit();
136     loop_uninit();
137     nested_loop();
138     loop_continue(true);
139 }