-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #56044 - matthewjasper:function-param-drop-order, r=cra…
…mertj 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.
- Loading branch information
Showing
2 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/run-pass/binding/fn-arg-incomplete-pattern-drop-order.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Check that partially moved from function parameters are dropped after the | ||
// named bindings that move from them. | ||
|
||
use std::{panic, cell::RefCell}; | ||
|
||
struct LogDrop<'a>(i32, Context<'a>); | ||
|
||
#[derive(Copy, Clone)] | ||
struct Context<'a> { | ||
panic_on: i32, | ||
drops: &'a RefCell<Vec<i32>>, | ||
} | ||
|
||
impl<'a> Context<'a> { | ||
fn record_drop(self, index: i32) { | ||
self.drops.borrow_mut().push(index); | ||
if index == self.panic_on { | ||
panic!(); | ||
} | ||
} | ||
} | ||
|
||
impl<'a> Drop for LogDrop<'a> { | ||
fn drop(&mut self) { | ||
self.1.record_drop(self.0); | ||
} | ||
} | ||
|
||
fn foo((_x, _): (LogDrop, LogDrop), (_, _y): (LogDrop, LogDrop)) {} | ||
|
||
fn test_drop_order(panic_on: i32) { | ||
let context = Context { | ||
panic_on, | ||
drops: &RefCell::new(Vec::new()), | ||
}; | ||
let one = LogDrop(1, context); | ||
let two = LogDrop(2, context); | ||
let three = LogDrop(3, context); | ||
let four = LogDrop(4, context); | ||
|
||
let res = panic::catch_unwind(panic::AssertUnwindSafe(|| { | ||
foo((three, four), (two, one)); | ||
})); | ||
if panic_on == 0 { | ||
assert!(res.is_ok(), "should not have panicked"); | ||
} else { | ||
assert!(res.is_err(), "should have panicked"); | ||
} | ||
assert_eq!(*context.drops.borrow(), [1, 2, 3, 4], "incorrect drop order"); | ||
} | ||
|
||
fn main() { | ||
(0..=4).for_each(test_drop_order); | ||
} |