]> git.lizzy.rs Git - rust.git/blob - src/test/run-fail/issue-30380.rs
Rollup merge of #35863 - matthew-piziak:shl-example, r=steveklabnik
[rust.git] / src / test / run-fail / issue-30380.rs
1 // Copyright 2016 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 // check that panics in destructors during assignment do not leave
12 // destroyed values lying around for other destructors to observe.
13
14 // error-pattern:panicking destructors ftw!
15
16 struct Observer<'a>(&'a mut FilledOnDrop);
17
18 struct FilledOnDrop(u32);
19 impl Drop for FilledOnDrop {
20     fn drop(&mut self) {
21         if self.0 == 0 {
22             // this is only set during the destructor - safe
23             // code should not be able to observe this.
24             self.0 = 0x1c1c1c1c;
25             panic!("panicking destructors ftw!");
26         }
27     }
28 }
29
30 impl<'a> Drop for Observer<'a> {
31     fn drop(&mut self) {
32         assert_eq!(self.0 .0, 1);
33     }
34 }
35
36 fn foo(b: &mut Observer) {
37     *b.0 = FilledOnDrop(1);
38 }
39
40 fn main() {
41     let mut bomb = FilledOnDrop(0);
42     let mut observer = Observer(&mut bomb);
43     foo(&mut observer);
44 }