|
| 1 | +// Copyright 2013 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 | +// In theory, it doesn't matter what order destructors are run in for rust |
| 12 | +// because we have explicit ownership of values meaning that there's no need to |
| 13 | +// run one before another. With unsafe code, however, there may be a safe |
| 14 | +// interface which relies on fields having their destructors run in a particular |
| 15 | +// order. At the time of this writing, std::rt::sched::Scheduler is an example |
| 16 | +// of a structure which contains unsafe handles to FFI-like types, and the |
| 17 | +// destruction order of the fields matters in the sense that some handles need |
| 18 | +// to get destroyed before others. |
| 19 | +// |
| 20 | +// In C++, destruction order happens bottom-to-top in order of field |
| 21 | +// declarations, but we currently run them top-to-bottom. I don't think the |
| 22 | +// order really matters that much as long as we define what it is. |
| 23 | + |
| 24 | +struct A; |
| 25 | +struct B; |
| 26 | +struct C { |
| 27 | + a: A, |
| 28 | + b: B, |
| 29 | +} |
| 30 | + |
| 31 | +static mut hit: bool = false; |
| 32 | + |
| 33 | +impl Drop for A { |
| 34 | + fn drop(&mut self) { |
| 35 | + unsafe { |
| 36 | + assert!(!hit); |
| 37 | + hit = true; |
| 38 | + } |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +impl Drop for B { |
| 43 | + fn drop(&mut self) { |
| 44 | + unsafe { |
| 45 | + assert!(hit); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +pub fn main() { |
| 51 | + let _c = C { a: A, b: B }; |
| 52 | +} |
0 commit comments