Skip to content

Commit c4b8735

Browse files
authored
Rollup merge of rust-lang#56044 - matthewjasper:function-param-drop-order, r=cramertj
Drop partially bound function parameters in the expected order Given the function ```rust fn foo((_x, _): (LogDrop, LogDrop), (_, _y): (LogDrop, LogDrop)) {} ``` Prior to 1.12.0 we dropped both `_x` and `_y` before the rest of their respective parameters, since then we dropped `_x` and `_y` after. The original order appears to be the correct order, as the value created later is dropped first, so we revert to that order and add a test for it. While this is technically a breaking change, I can't work out how anyone could be relying on this without making their code very brittle. If this is considered to be too likely to break real world code then I can revert the change and change the test to check for the current order.
2 parents 33e6df4 + 914515f commit c4b8735

File tree

2 files changed

+75
-7
lines changed

2 files changed

+75
-7
lines changed

src/librustc_mir/build/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -910,6 +910,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
910910
let place = Place::Local(local);
911911
let &ArgInfo(ty, opt_ty_info, pattern, ref self_binding) = arg_info;
912912

913+
// Make sure we drop (parts of) the argument even when not matched on.
914+
self.schedule_drop(
915+
pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
916+
argument_scope, &place, ty,
917+
DropKind::Value { cached_block: CachedBlock::default() },
918+
);
919+
913920
if let Some(pattern) = pattern {
914921
let pattern = self.hir.pattern_from_hir(pattern);
915922
let span = pattern.span;
@@ -941,13 +948,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
941948
}
942949
}
943950
}
944-
945-
// Make sure we drop (parts of) the argument even when not matched on.
946-
self.schedule_drop(
947-
pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
948-
argument_scope, &place, ty,
949-
DropKind::Value { cached_block: CachedBlock::default() },
950-
);
951951
}
952952

953953
// Enter the argument pattern bindings source scope, if it exists.
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Check that partially moved from function parameters are dropped after the
2+
// named bindings that move from them.
3+
4+
// ignore-wasm32-bare compiled with panic=abort by default
5+
6+
use std::{panic, cell::RefCell};
7+
8+
struct LogDrop<'a>(i32, Context<'a>);
9+
10+
#[derive(Copy, Clone)]
11+
struct Context<'a> {
12+
panic_on: i32,
13+
drops: &'a RefCell<Vec<i32>>,
14+
}
15+
16+
impl<'a> Context<'a> {
17+
fn record_drop(self, index: i32) {
18+
self.drops.borrow_mut().push(index);
19+
if index == self.panic_on {
20+
panic!();
21+
}
22+
}
23+
}
24+
25+
impl<'a> Drop for LogDrop<'a> {
26+
fn drop(&mut self) {
27+
self.1.record_drop(self.0);
28+
}
29+
}
30+
31+
fn bindings_in_params((_x, _): (LogDrop, LogDrop), (_, _y): (LogDrop, LogDrop)) {}
32+
fn bindings_with_let(a: (LogDrop, LogDrop), b: (LogDrop, LogDrop)) {
33+
// Drop order in foo is the same as the following bindings.
34+
// _temp2 is declared after _x to avoid a difference between `_: T` and
35+
// `x: T` in function parameters.
36+
let _temp1 = a;
37+
let (_x, _) = _temp1;
38+
39+
let _temp2 = b;
40+
let (_, _y) = _temp2;
41+
}
42+
43+
fn test_drop_order(panic_on: i32, fun: fn((LogDrop, LogDrop), (LogDrop, LogDrop))) {
44+
let context = Context {
45+
panic_on,
46+
drops: &RefCell::new(Vec::new()),
47+
};
48+
let one = LogDrop(1, context);
49+
let two = LogDrop(2, context);
50+
let three = LogDrop(3, context);
51+
let four = LogDrop(4, context);
52+
53+
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
54+
fun((three, four), (two, one));
55+
}));
56+
if panic_on == 0 {
57+
assert!(res.is_ok(), "should not have panicked");
58+
} else {
59+
assert!(res.is_err(), "should have panicked");
60+
}
61+
assert_eq!(*context.drops.borrow(), [1, 2, 3, 4], "incorrect drop order");
62+
}
63+
64+
fn main() {
65+
(0..=4).for_each(|i| test_drop_order(i, bindings_in_params));
66+
(0..=4).for_each(|i| test_drop_order(i, bindings_with_let));
67+
(0..=4).for_each(|i| test_drop_order(i, |(_x, _), (_, _y)| {}));
68+
}

0 commit comments

Comments
 (0)