]> git.lizzy.rs Git - rust.git/blob - src/test/ui/issues/issue-30380.rs
Rollup merge of #80327 - PankajChaudhary5:PankajChaudhary, r=GuillaumeGomez
[rust.git] / src / test / ui / issues / issue-30380.rs
1 // check that panics in destructors during assignment do not leave
2 // destroyed values lying around for other destructors to observe.
3
4 // run-fail
5 // error-pattern:panicking destructors ftw!
6 // ignore-emscripten no processes
7
8 struct Observer<'a>(&'a mut FilledOnDrop);
9
10 struct FilledOnDrop(u32);
11 impl Drop for FilledOnDrop {
12     fn drop(&mut self) {
13         if self.0 == 0 {
14             // this is only set during the destructor - safe
15             // code should not be able to observe this.
16             self.0 = 0x1c1c1c1c;
17             panic!("panicking destructors ftw!");
18         }
19     }
20 }
21
22 impl<'a> Drop for Observer<'a> {
23     fn drop(&mut self) {
24         assert_eq!(self.0 .0, 1);
25     }
26 }
27
28 fn foo(b: &mut Observer) {
29     *b.0 = FilledOnDrop(1);
30 }
31
32 fn main() {
33     let mut bomb = FilledOnDrop(0);
34     let mut observer = Observer(&mut bomb);
35     foo(&mut observer);
36 }