Skip to content

Commit fee2dfd

Browse files
committed
base drop order on the first sub-branch
1 parent 4f1c4e2 commit fee2dfd

File tree

5 files changed

+33
-39
lines changed

5 files changed

+33
-39
lines changed

compiler/rustc_mir_build/src/builder/matches/mod.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -563,17 +563,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
563563
// we lower the guard.
564564
// As a result, we end up with the drop order of the last sub-branch we lower. To use
565565
// the drop order for the first sub-branch, we lower sub-branches in reverse (#142163).
566-
// TODO: I'm saving the breaking change for the next commit. For now, a stopgap:
567-
let sub_branch_to_use_the_drops_from =
568-
if arm_match_scope.is_some() { Position::Last } else { Position::First };
569566
let target_block = self.cfg.start_new_block();
570-
for (pos, sub_branch) in branch.sub_branches.into_iter().with_position() {
567+
for (pos, sub_branch) in branch.sub_branches.into_iter().rev().with_position() {
571568
debug_assert!(pos != Position::Only);
572-
let schedule_drops = if pos == sub_branch_to_use_the_drops_from {
573-
ScheduleDrops::Yes
574-
} else {
575-
ScheduleDrops::No
576-
};
569+
let schedule_drops =
570+
if pos == Position::Last { ScheduleDrops::Yes } else { ScheduleDrops::No };
577571
let binding_end = self.bind_and_guard_matched_candidate(
578572
sub_branch,
579573
fake_borrow_temps,

tests/mir-opt/match_arm_scopes.complicated_match.panic-unwind.SimplifyCfg-initial.after-ElaborateDrops.after.diff

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,11 @@
8585
_8 = &(_2.2: std::string::String);
8686
- _3 = &fake shallow (_2.0: bool);
8787
- _4 = &fake shallow (_2.1: bool);
88-
StorageLive(_12);
89-
StorageLive(_13);
90-
_13 = copy _1;
91-
- switchInt(move _13) -> [0: bb16, otherwise: bb15];
92-
+ switchInt(move _13) -> [0: bb13, otherwise: bb12];
88+
StorageLive(_9);
89+
StorageLive(_10);
90+
_10 = copy _1;
91+
- switchInt(move _10) -> [0: bb12, otherwise: bb11];
92+
+ switchInt(move _10) -> [0: bb9, otherwise: bb8];
9393
}
9494

9595
- bb9: {
@@ -100,11 +100,11 @@
100100
_8 = &(_2.2: std::string::String);
101101
- _3 = &fake shallow (_2.0: bool);
102102
- _4 = &fake shallow (_2.1: bool);
103-
StorageLive(_9);
104-
StorageLive(_10);
105-
_10 = copy _1;
106-
- switchInt(move _10) -> [0: bb12, otherwise: bb11];
107-
+ switchInt(move _10) -> [0: bb9, otherwise: bb8];
103+
StorageLive(_12);
104+
StorageLive(_13);
105+
_13 = copy _1;
106+
- switchInt(move _13) -> [0: bb16, otherwise: bb15];
107+
+ switchInt(move _13) -> [0: bb13, otherwise: bb12];
108108
}
109109

110110
- bb10: {
@@ -139,7 +139,7 @@
139139
- FakeRead(ForGuardBinding, _6);
140140
- FakeRead(ForGuardBinding, _8);
141141
StorageLive(_5);
142-
_5 = copy (_2.1: bool);
142+
_5 = copy (_2.0: bool);
143143
StorageLive(_7);
144144
_7 = move (_2.2: std::string::String);
145145
- goto -> bb10;
@@ -152,8 +152,8 @@
152152
StorageDead(_9);
153153
StorageDead(_8);
154154
StorageDead(_6);
155-
- falseEdge -> [real: bb1, imaginary: bb1];
156-
+ goto -> bb1;
155+
- falseEdge -> [real: bb3, imaginary: bb3];
156+
+ goto -> bb2;
157157
}
158158

159159
- bb15: {
@@ -181,7 +181,7 @@
181181
- FakeRead(ForGuardBinding, _6);
182182
- FakeRead(ForGuardBinding, _8);
183183
StorageLive(_5);
184-
_5 = copy (_2.0: bool);
184+
_5 = copy (_2.1: bool);
185185
StorageLive(_7);
186186
_7 = move (_2.2: std::string::String);
187187
- goto -> bb10;
@@ -194,8 +194,8 @@
194194
StorageDead(_12);
195195
StorageDead(_8);
196196
StorageDead(_6);
197-
- falseEdge -> [real: bb3, imaginary: bb3];
198-
+ goto -> bb2;
197+
- falseEdge -> [real: bb1, imaginary: bb1];
198+
+ goto -> bb1;
199199
}
200200

201201
- bb19: {

tests/ui/drop/or-pattern-drop-order.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,12 @@ fn main() {
6565
match (LogDrop(o, 3), Ok(LogDrop(o, 1)), LogDrop(o, 2)) { (x, Ok(y) | Err(y), z) => {} }
6666
});
6767
assert_drop_order(1..=2, |o| {
68-
// The last or-pattern alternative determines the bindings' drop order: `x`, `y`.
69-
match (true, LogDrop(o, 1), LogDrop(o, 2)) { (true, x, y) | (false, y, x) => {} }
68+
// The first or-pattern alternative determines the bindings' drop order: `y`, `x`.
69+
match (true, LogDrop(o, 2), LogDrop(o, 1)) { (true, x, y) | (false, y, x) => {} }
7070
});
7171
assert_drop_order(1..=2, |o| {
72-
// That drop order is used regardless of which or-pattern alternative matches: `x`, `y`.
73-
match (false, LogDrop(o, 2), LogDrop(o, 1)) { (true, x, y) | (false, y, x) => {} }
72+
// That drop order is used regardless of which or-pattern alternative matches: `y`, `x`.
73+
match (false, LogDrop(o, 1), LogDrop(o, 2)) { (true, x, y) | (false, y, x) => {} }
7474
});
7575

7676
// Function params are visited one-by-one, and the order of bindings within a param's pattern is

tests/ui/dropck/eager-by-ref-binding-for-guards.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,15 @@ fn main() {
1717
(mut long2, ref short2) if true => long2.0 = &short2,
1818
_ => unreachable!(),
1919
}
20-
// This depends on the binding modes of the final or-pattern alternatives (see #142163):
20+
// This depends on the binding modes of the first or-pattern alternatives:
2121
let res: &Result<u8, &u8> = &Ok(1);
2222
match (Struct(&&0), res) {
2323
(mut long3, Ok(short3) | &Err(short3)) if true => long3.0 = &short3,
24-
//~^ ERROR `short3` does not live long enough
2524
_ => unreachable!(),
2625
}
2726
match (Struct(&&0), res) {
2827
(mut long4, &Err(short4) | Ok(short4)) if true => long4.0 = &short4,
28+
//~^ ERROR `short4` does not live long enough
2929
_ => unreachable!(),
3030
}
3131
}

tests/ui/dropck/eager-by-ref-binding-for-guards.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ LL | (mut long1, ref short1) => long1.0 = &short1,
1111
|
1212
= note: values in a scope are dropped in the opposite order they are defined
1313

14-
error[E0597]: `short3` does not live long enough
15-
--> $DIR/eager-by-ref-binding-for-guards.rs:23:69
14+
error[E0597]: `short4` does not live long enough
15+
--> $DIR/eager-by-ref-binding-for-guards.rs:27:69
1616
|
17-
LL | (mut long3, Ok(short3) | &Err(short3)) if true => long3.0 = &short3,
18-
| ------ ^^^^^^-
19-
| | | |
20-
| | | `short3` dropped here while still borrowed
21-
| | | borrow might be used here, when `long3` is dropped and runs the `Drop` code for type `Struct`
22-
| binding `short3` declared here borrowed value does not live long enough
17+
LL | (mut long4, &Err(short4) | Ok(short4)) if true => long4.0 = &short4,
18+
| ------ ^^^^^^-
19+
| | | |
20+
| | | `short4` dropped here while still borrowed
21+
| | | borrow might be used here, when `long4` is dropped and runs the `Drop` code for type `Struct`
22+
| binding `short4` declared here borrowed value does not live long enough
2323
|
2424
= note: values in a scope are dropped in the opposite order they are defined
2525

0 commit comments

Comments
 (0)