about summary refs log tree commit diff
path: root/tests/ui/drop/struct-field-drop-order.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2025-08-10 14:17:41 +0000
committerbors <bors@rust-lang.org>2025-08-10 14:17:41 +0000
commit18eeac04fc5c2a4c4a8020dbdf1c652077ad0e4e (patch)
tree3bb0db94131d71006af1bfd0151bb9d530cf1315 /tests/ui/drop/struct-field-drop-order.rs
parent7f7b8ef27d86c865a7ab20c7c42f50811c6a914d (diff)
parent934cb10f1b21be3a855951fb9b1f38094946aac8 (diff)
downloadrust-18eeac04fc5c2a4c4a8020dbdf1c652077ad0e4e.tar.gz
rust-18eeac04fc5c2a4c4a8020dbdf1c652077ad0e4e.zip
Auto merge of #145210 - Zalathar:rollup-dm4reb2, r=Zalathar
Rollup of 17 pull requests

Successful merges:

 - rust-lang/rust#141624 (unstable-book: Add stubs for environment variables; document some of the important ones)
 - rust-lang/rust#143093 (Simplify polonius location-sensitive analysis)
 - rust-lang/rust#144402 (Stabilize loongarch32 inline asm)
 - rust-lang/rust#144403 (`tests/ui/issues/`: The Issues Strike Back [4/N])
 - rust-lang/rust#144739 (Use new public libtest `ERROR_EXIT_CODE` constant in rustdoc)
 - rust-lang/rust#145089 (Improve error output when a command fails in bootstrap)
 - rust-lang/rust#145112 ([win][arm64ec] Partial fix for raw-dylib-link-ordinal on Arm64EC)
 - rust-lang/rust#145129 ([win][arm64ec] Add `/machine:arm64ec` when linking LLVM as Arm64EC)
 - rust-lang/rust#145130 (improve "Documentation problem" issue template.)
 - rust-lang/rust#145135 (Stabilize `duration_constructors_lite` feature)
 - rust-lang/rust#145145 (some `derive_more` refactors)
 - rust-lang/rust#145147 (rename `TraitRef::from_method` to `from_assoc`)
 - rust-lang/rust#145156 (Override custom Cargo `build-dir` in bootstrap)
 - rust-lang/rust#145160 (Change days-threshold to 28 in [behind-upstream])
 - rust-lang/rust#145162 (`{BTree,Hash}Map`: add "`Entry` API" section heading)
 - rust-lang/rust#145187 (Fix an unstable feature comment that wasn't a doc comment)
 - rust-lang/rust#145191 (`suggest_borrow_generic_arg`: use the correct generic args)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'tests/ui/drop/struct-field-drop-order.rs')
-rw-r--r--tests/ui/drop/struct-field-drop-order.rs69
1 files changed, 69 insertions, 0 deletions
diff --git a/tests/ui/drop/struct-field-drop-order.rs b/tests/ui/drop/struct-field-drop-order.rs
new file mode 100644
index 00000000000..80e3ea616a5
--- /dev/null
+++ b/tests/ui/drop/struct-field-drop-order.rs
@@ -0,0 +1,69 @@
+//! Regression test for https://github.com/rust-lang/rust/issues/16492
+
+//@ run-pass
+#![allow(non_snake_case)]
+
+use std::rc::Rc;
+use std::cell::Cell;
+
+struct Field {
+    number: usize,
+    state: Rc<Cell<usize>>
+}
+
+impl Field {
+    fn new(number: usize, state: Rc<Cell<usize>>) -> Field {
+        Field {
+            number: number,
+            state: state
+        }
+    }
+}
+
+impl Drop for Field {
+    fn drop(&mut self) {
+        println!("Dropping field {}", self.number);
+        assert_eq!(self.state.get(), self.number);
+        self.state.set(self.state.get()+1);
+    }
+}
+
+struct NoDropImpl {
+    _one: Field,
+    _two: Field,
+    _three: Field
+}
+
+struct HasDropImpl {
+    _one: Field,
+    _two: Field,
+    _three: Field
+}
+
+impl Drop for HasDropImpl {
+    fn drop(&mut self) {
+        println!("HasDropImpl.drop()");
+        assert_eq!(self._one.state.get(), 0);
+        self._one.state.set(1);
+    }
+}
+
+pub fn main() {
+    let state = Rc::new(Cell::new(1));
+    let noImpl = NoDropImpl {
+        _one: Field::new(1, state.clone()),
+        _two: Field::new(2, state.clone()),
+        _three: Field::new(3, state.clone())
+    };
+    drop(noImpl);
+    assert_eq!(state.get(), 4);
+
+    state.set(0);
+    let hasImpl = HasDropImpl {
+        _one: Field::new(1, state.clone()),
+        _two: Field::new(2, state.clone()),
+        _three: Field::new(3, state.clone())
+    };
+    drop(hasImpl);
+    assert_eq!(state.get(), 4);
+}