Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion compiler/rustc_mir_transform/src/elaborate_drop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1373,7 +1373,6 @@ where
}
let fields = drop_ranges
.iter()
.rev()
.map(|p| {
let (project, path) = match p {
ProjectionKind::Drop(r) => (
Expand Down
79 changes: 79 additions & 0 deletions tests/ui/drop/array-partial-move-drop-order.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
//! Array elements are dropped first to last even when an array pattern may have moved some out.
//! Regression test for <https://github.com/rust-lang/rust/issues/164809>.
//@ run-pass
//@ needs-unwind

use std::cell::RefCell;
use std::panic::{AssertUnwindSafe, catch_unwind};

struct D<'a>(u32, &'a RefCell<Vec<u32>>, bool);

impl Drop for D<'_> {
fn drop(&mut self) {
self.1.borrow_mut().push(self.0);
if self.2 {
panic!();
}
}
}

fn main() {
let log = RefCell::new(vec![]);
let d = |n| D(n, &log, false);
let no = std::hint::black_box(false);

{
let a = [d(1), d(2)];
if no {
let [_x, _] = a;
}
}
assert_eq!(log.take(), [1, 2]);

{
let a = [d(1), d(2), d(3)];
if no {
let [_x, _, _] = a;
}
}
assert_eq!(log.take(), [1, 2, 3]);

{
let a = [d(1), d(2), d(3)];
if no {
let [_, _x, _] = a;
}
}
assert_eq!(log.take(), [1, 2, 3]);

{
let a = [d(1), d(2), d(3), d(4), d(5)];
if no {
let [_, _x, .., _y] = a;
}
}
assert_eq!(log.take(), [1, 2, 3, 4, 5]);

{
let a = [d(1), d(2), d(3), d(4)];
if no {
let [_x, _y @ .., _] = a;
}
}
assert_eq!(log.take(), [1, 2, 3, 4]);

{
let a = [d(1), d(2), d(3)];
let [_, _x, _] = a;
}
assert_eq!(log.take(), [2, 1, 3]);

let r = catch_unwind(AssertUnwindSafe(|| {
let a = [d(1), D(2, &log, true), d(3), d(4)];
if no {
let [_, _, _x, _] = a;
}
}));
assert!(r.is_err());
assert_eq!(log.take(), [1, 2, 3, 4]);
}
2 changes: 1 addition & 1 deletion tests/ui/drop/if-let-guards.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn main() {
// binding declaration order, and they're dropped in the arm's scope.
match [o.log(6), o.log(5)] {
// Partially move from the guard temporary to test drops both for temps and the binding.
[_x, _y] if let [_, _z, _] = [o.log(4), o.log(2), o.log(3)]
[_x, _y] if let [_, _z, _] = [o.log(3), o.log(2), o.log(4)]
&& true => { let _a = o.log(1); }
_ => unreachable!(),
}
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/drop/or-pattern-drop-order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ fn main() {
assert_drop_order(1..=6, |o| {
// The `LogDrop`s that aren't moved into bindings are dropped last.
match [
[LogDrop(o, 6), LogDrop(o, 4)],
[LogDrop(o, 5), LogDrop(o, 4)],
[LogDrop(o, 3), LogDrop(o, 2)],
[LogDrop(o, 1), LogDrop(o, 5)],
[LogDrop(o, 1), LogDrop(o, 6)],
] {
[
[_ | _, w | w] | [w | w, _ | _],
Expand Down
Loading