]> git.lizzy.rs Git - rust.git/blob - src/test/ui/for-loop-while/for-loop-lifetime-of-unbound-values.rs
Auto merge of #84589 - In-line:zircon-thread-name, r=JohnTitor
[rust.git] / src / test / ui / for-loop-while / for-loop-lifetime-of-unbound-values.rs
1 // run-pass
2 // Test when destructors run in a for loop. The intention is
3 // that the value for each iteration is dropped *after* the loop
4 // body has executed. This is true even when the value is assigned
5 // to a `_` pattern (and hence ignored).
6
7 use std::cell::Cell;
8
9 struct Flag<'a>(&'a Cell<bool>);
10
11 impl<'a> Drop for Flag<'a> {
12     fn drop(&mut self) {
13         self.0.set(false)
14     }
15 }
16
17 fn main() {
18     let alive2 = Cell::new(true);
19     for _i in std::iter::once(Flag(&alive2)) {
20         // The Flag value should be alive in the for loop body
21         assert_eq!(alive2.get(), true);
22     }
23     // The Flag value should be dead outside of the loop
24     assert_eq!(alive2.get(), false);
25
26     let alive = Cell::new(true);
27     for _ in std::iter::once(Flag(&alive)) {
28         // The Flag value should be alive in the for loop body even if it wasn't
29         // bound by the for loop
30         assert_eq!(alive.get(), true);
31     }
32     // The Flag value should be dead outside of the loop
33     assert_eq!(alive.get(), false);
34 }