Skip to content

Commit 7dc0dd2

Browse files
committed
Drop function parameters in expected order
Given the function fn foo((_x, _): (LogDrop, LogDrop), (_, _y): (LogDrop, LogDrop)) {} Prior to 1.12 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.
1 parent f37247f commit 7dc0dd2

File tree

2 files changed

+61
-7
lines changed

2 files changed

+61
-7
lines changed

src/librustc_mir/build/mod.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,13 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
915915
let place = Place::Local(local);
916916
let &ArgInfo(ty, opt_ty_info, pattern, ref self_binding) = arg_info;
917917

918+
// Make sure we drop (parts of) the argument even when not matched on.
919+
self.schedule_drop(
920+
pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
921+
argument_scope, &place, ty,
922+
DropKind::Value { cached_block: CachedBlock::default() },
923+
);
924+
918925
if let Some(pattern) = pattern {
919926
let pattern = self.hir.pattern_from_hir(pattern);
920927
let span = pattern.span;
@@ -946,13 +953,6 @@ impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
946953
}
947954
}
948955
}
949-
950-
// Make sure we drop (parts of) the argument even when not matched on.
951-
self.schedule_drop(
952-
pattern.as_ref().map_or(ast_body.span, |pat| pat.span),
953-
argument_scope, &place, ty,
954-
DropKind::Value { cached_block: CachedBlock::default() },
955-
);
956956
}
957957

958958
// Enter the argument pattern bindings source scope, if it exists.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Check that partially moved from function parameters are dropped after the
2+
// named bindings that move from them.
3+
4+
use std::{panic, cell::RefCell};
5+
6+
struct LogDrop<'a>(i32, Context<'a>);
7+
8+
#[derive(Copy, Clone)]
9+
struct Context<'a> {
10+
panic_on: i32,
11+
drops: &'a RefCell<Vec<i32>>,
12+
}
13+
14+
impl<'a> Context<'a> {
15+
fn record_drop(self, index: i32) {
16+
self.drops.borrow_mut().push(index);
17+
if index == self.panic_on {
18+
panic!();
19+
}
20+
}
21+
}
22+
23+
impl<'a> Drop for LogDrop<'a> {
24+
fn drop(&mut self) {
25+
self.1.record_drop(self.0);
26+
}
27+
}
28+
29+
fn foo((_x, _): (LogDrop, LogDrop), (_, _y): (LogDrop, LogDrop)) {}
30+
31+
fn test_drop_order(panic_on: i32) {
32+
let context = Context {
33+
panic_on,
34+
drops: &RefCell::new(Vec::new()),
35+
};
36+
let one = LogDrop(1, context);
37+
let two = LogDrop(2, context);
38+
let three = LogDrop(3, context);
39+
let four = LogDrop(4, context);
40+
41+
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| {
42+
foo((three, four), (two, one));
43+
}));
44+
if panic_on == 0 {
45+
assert!(res.is_ok(), "should not have panicked");
46+
} else {
47+
assert!(res.is_err(), "should have panicked");
48+
}
49+
assert_eq!(*context.drops.borrow(), [1, 2, 3, 4], "incorrect drop order");
50+
}
51+
52+
fn main() {
53+
(0..=4).for_each(test_drop_order);
54+
}

0 commit comments

Comments
 (0)