Skip to content

Commit 09d4613

Browse files
committed
Put new bindings first in refutable cases too
1 parent e902878 commit 09d4613

18 files changed

+229
-209
lines changed

compiler/rustc_mir_build/src/build/matches/simplify.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
8787
// let z = (x as Variant).copy_field;
8888
// // and raises an error
8989
// }
90-
let mut original_bindings = mem::take(&mut candidate.bindings);
90+
let original_bindings = mem::take(&mut candidate.bindings);
9191
let mut new_bindings = Vec::new();
9292
// Repeatedly simplify match pairs until fixed point is reached
9393
loop {
@@ -115,9 +115,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
115115
}
116116

117117
// Restore original bindings and append the new ones.
118-
// This does: candidate.bindings = original_bindings ++ new_bindings
119-
mem::swap(&mut candidate.bindings, &mut original_bindings);
120-
candidate.bindings.extend_from_slice(&new_bindings);
118+
// This does: candidate.bindings = new_bindings ++ original_bindings
119+
mem::swap(&mut candidate.bindings, &mut new_bindings);
120+
candidate.bindings.extend_from_slice(&original_bindings);
121121

122122
let did_expand_or =
123123
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place }] =

tests/mir-opt/early_otherwise_branch.opt1.EarlyOtherwiseBranch.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151
- }
5252
-
5353
- bb3: {
54-
StorageLive(_8);
55-
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
5654
StorageLive(_9);
5755
_9 = (((_3.1: std::option::Option<u32>) as Some).0: u32);
56+
StorageLive(_8);
57+
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
5858
_0 = const 0_u32;
59-
StorageDead(_9);
6059
StorageDead(_8);
60+
StorageDead(_9);
6161
- goto -> bb4;
6262
+ goto -> bb3;
6363
}

tests/mir-opt/early_otherwise_branch.opt2.EarlyOtherwiseBranch.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,13 @@
5858
-
5959
- bb4: {
6060
+ bb2: {
61-
StorageLive(_9);
62-
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
6361
StorageLive(_10);
6462
_10 = (((_3.1: std::option::Option<u32>) as Some).0: u32);
63+
StorageLive(_9);
64+
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
6565
_0 = const 0_u32;
66-
StorageDead(_10);
6766
StorageDead(_9);
67+
StorageDead(_10);
6868
- goto -> bb6;
6969
+ goto -> bb4;
7070
}

tests/mir-opt/early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@
5151
- }
5252
-
5353
- bb3: {
54-
StorageLive(_8);
55-
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
5654
StorageLive(_9);
5755
_9 = (((_3.1: std::option::Option<bool>) as Some).0: bool);
56+
StorageLive(_8);
57+
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
5858
_0 = const 0_u32;
59-
StorageDead(_9);
6059
StorageDead(_8);
60+
StorageDead(_9);
6161
- goto -> bb4;
6262
+ goto -> bb3;
6363
}

tests/mir-opt/early_otherwise_branch_3_element_tuple.opt1.EarlyOtherwiseBranch.diff

+6-6
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,16 @@
6969

7070
- bb4: {
7171
+ bb3: {
72-
StorageLive(_11);
73-
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32);
74-
StorageLive(_12);
75-
_12 = (((_4.1: std::option::Option<u32>) as Some).0: u32);
7672
StorageLive(_13);
7773
_13 = (((_4.2: std::option::Option<u32>) as Some).0: u32);
74+
StorageLive(_12);
75+
_12 = (((_4.1: std::option::Option<u32>) as Some).0: u32);
76+
StorageLive(_11);
77+
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32);
7878
_0 = const 0_u32;
79-
StorageDead(_13);
80-
StorageDead(_12);
8179
StorageDead(_11);
80+
StorageDead(_12);
81+
StorageDead(_13);
8282
- goto -> bb5;
8383
+ goto -> bb4;
8484
}

tests/mir-opt/early_otherwise_branch_68867.try_sum.EarlyOtherwiseBranch.diff

+24-24
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,12 @@
116116
}
117117

118118
bb6: {
119-
StorageLive(_12);
120-
_39 = deref_copy (_4.0: &ViewportPercentageLength);
121-
_12 = (((*_39) as Vw).0: f32);
122119
StorageLive(_13);
123-
_40 = deref_copy (_4.1: &ViewportPercentageLength);
124-
_13 = (((*_40) as Vw).0: f32);
120+
_39 = deref_copy (_4.1: &ViewportPercentageLength);
121+
_13 = (((*_39) as Vw).0: f32);
122+
StorageLive(_12);
123+
_40 = deref_copy (_4.0: &ViewportPercentageLength);
124+
_12 = (((*_40) as Vw).0: f32);
125125
StorageLive(_14);
126126
StorageLive(_15);
127127
_15 = _12;
@@ -132,18 +132,18 @@
132132
StorageDead(_15);
133133
_3 = ViewportPercentageLength::Vw(move _14);
134134
StorageDead(_14);
135-
StorageDead(_13);
136135
StorageDead(_12);
136+
StorageDead(_13);
137137
goto -> bb10;
138138
}
139139

140140
bb7: {
141-
StorageLive(_17);
142-
_41 = deref_copy (_4.0: &ViewportPercentageLength);
143-
_17 = (((*_41) as Vh).0: f32);
144141
StorageLive(_18);
145-
_42 = deref_copy (_4.1: &ViewportPercentageLength);
146-
_18 = (((*_42) as Vh).0: f32);
142+
_41 = deref_copy (_4.1: &ViewportPercentageLength);
143+
_18 = (((*_41) as Vh).0: f32);
144+
StorageLive(_17);
145+
_42 = deref_copy (_4.0: &ViewportPercentageLength);
146+
_17 = (((*_42) as Vh).0: f32);
147147
StorageLive(_19);
148148
StorageLive(_20);
149149
_20 = _17;
@@ -154,18 +154,18 @@
154154
StorageDead(_20);
155155
_3 = ViewportPercentageLength::Vh(move _19);
156156
StorageDead(_19);
157-
StorageDead(_18);
158157
StorageDead(_17);
158+
StorageDead(_18);
159159
goto -> bb10;
160160
}
161161

162162
bb8: {
163-
StorageLive(_22);
164-
_43 = deref_copy (_4.0: &ViewportPercentageLength);
165-
_22 = (((*_43) as Vmin).0: f32);
166163
StorageLive(_23);
167-
_44 = deref_copy (_4.1: &ViewportPercentageLength);
168-
_23 = (((*_44) as Vmin).0: f32);
164+
_43 = deref_copy (_4.1: &ViewportPercentageLength);
165+
_23 = (((*_43) as Vmin).0: f32);
166+
StorageLive(_22);
167+
_44 = deref_copy (_4.0: &ViewportPercentageLength);
168+
_22 = (((*_44) as Vmin).0: f32);
169169
StorageLive(_24);
170170
StorageLive(_25);
171171
_25 = _22;
@@ -176,18 +176,18 @@
176176
StorageDead(_25);
177177
_3 = ViewportPercentageLength::Vmin(move _24);
178178
StorageDead(_24);
179-
StorageDead(_23);
180179
StorageDead(_22);
180+
StorageDead(_23);
181181
goto -> bb10;
182182
}
183183

184184
bb9: {
185-
StorageLive(_27);
186-
_45 = deref_copy (_4.0: &ViewportPercentageLength);
187-
_27 = (((*_45) as Vmax).0: f32);
188185
StorageLive(_28);
189-
_46 = deref_copy (_4.1: &ViewportPercentageLength);
190-
_28 = (((*_46) as Vmax).0: f32);
186+
_45 = deref_copy (_4.1: &ViewportPercentageLength);
187+
_28 = (((*_45) as Vmax).0: f32);
188+
StorageLive(_27);
189+
_46 = deref_copy (_4.0: &ViewportPercentageLength);
190+
_27 = (((*_46) as Vmax).0: f32);
191191
StorageLive(_29);
192192
StorageLive(_30);
193193
_30 = _27;
@@ -198,8 +198,8 @@
198198
StorageDead(_30);
199199
_3 = ViewportPercentageLength::Vmax(move _29);
200200
StorageDead(_29);
201-
StorageDead(_28);
202201
StorageDead(_27);
202+
StorageDead(_28);
203203
goto -> bb10;
204204
}
205205

tests/mir-opt/early_otherwise_branch_noopt.noopt1.EarlyOtherwiseBranch.diff

+3-3
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,13 @@
5959
}
6060

6161
bb5: {
62-
StorageLive(_9);
63-
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
6462
StorageLive(_10);
6563
_10 = (((_3.1: std::option::Option<u32>) as Some).0: u32);
64+
StorageLive(_9);
65+
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
6666
_0 = const 0_u32;
67-
StorageDead(_10);
6867
StorageDead(_9);
68+
StorageDead(_10);
6969
goto -> bb8;
7070
}
7171

tests/ui/pattern/bindings-after-at/bind-by-copy.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// run-pass
12
#![allow(unused)]
23

34
// Test copy
@@ -41,7 +42,6 @@ pub fn main() {
4142

4243
match (E::E { a: 10, e: C { c: 20 } }) {
4344
x @ E::E{ a, e: C { c } } => {
44-
//~^ ERROR use of moved value
4545
assert!(matches!(x, E::E { a: 10, e: C { c: 20 } }));
4646
assert!(a == 10);
4747
assert!(c == 20);
@@ -50,7 +50,6 @@ pub fn main() {
5050
}
5151
match (E::E { a: 10, e: C { c: 20 } }) {
5252
mut x @ E::E{ a, e: C { mut c } } => {
53-
//~^ ERROR use of moved value
5453
x = E::NotE;
5554
c += 30;
5655
assert_eq!(c, 50);

tests/ui/pattern/bindings-after-at/borrowck-move-and-move.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ fn main() {
1515
let a @ (b, c) = (u(), u()); //~ ERROR use of partially moved value
1616

1717
match Ok(U) {
18-
a @ Ok(b) | a @ Err(b) => {} //~ ERROR use of moved value
19-
//~^ ERROR use of moved value
18+
a @ Ok(b) | a @ Err(b) => {} //~ ERROR use of partially moved value
19+
//~^ ERROR use of partially moved value
2020
}
2121

2222
fn fun(a @ b: U) {} //~ ERROR use of moved value

tests/ui/pattern/bindings-after-at/borrowck-move-and-move.stderr

+14-16
Original file line numberDiff line numberDiff line change
@@ -40,35 +40,33 @@ help: borrow this binding in the pattern to avoid moving the value
4040
LL | let ref a @ (b, ref c) = (u(), u());
4141
| +++ +++
4242

43-
error[E0382]: use of moved value
44-
--> $DIR/borrowck-move-and-move.rs:18:16
43+
error[E0382]: use of partially moved value
44+
--> $DIR/borrowck-move-and-move.rs:18:9
4545
|
46-
LL | match Ok(U) {
47-
| ----- move occurs because value has type `Result<U, U>`, which does not implement the `Copy` trait
4846
LL | a @ Ok(b) | a @ Err(b) => {}
49-
| - ^ value used here after move
47+
| ^ - value partially moved here
5048
| |
51-
| value moved here
49+
| value used here after partial move
5250
|
51+
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
5352
help: borrow this binding in the pattern to avoid moving the value
5453
|
55-
LL | ref a @ Ok(b) | a @ Err(b) => {}
56-
| +++
54+
LL | ref a @ Ok(ref b) | a @ Err(b) => {}
55+
| +++ +++
5756

58-
error[E0382]: use of moved value
59-
--> $DIR/borrowck-move-and-move.rs:18:29
57+
error[E0382]: use of partially moved value
58+
--> $DIR/borrowck-move-and-move.rs:18:21
6059
|
61-
LL | match Ok(U) {
62-
| ----- move occurs because value has type `Result<U, U>`, which does not implement the `Copy` trait
6360
LL | a @ Ok(b) | a @ Err(b) => {}
64-
| - ^ value used here after move
61+
| ^ - value partially moved here
6562
| |
66-
| value moved here
63+
| value used here after partial move
6764
|
65+
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
6866
help: borrow this binding in the pattern to avoid moving the value
6967
|
70-
LL | a @ Ok(b) | ref a @ Err(b) => {}
71-
| +++
68+
LL | a @ Ok(b) | ref a @ Err(ref b) => {}
69+
| +++ +++
7270

7371
error[E0382]: use of partially moved value
7472
--> $DIR/borrowck-move-and-move.rs:25:9

tests/ui/pattern/bindings-after-at/borrowck-pat-by-move-and-ref-inverse.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -48,33 +48,30 @@ fn main() {
4848
//~^ ERROR borrow of moved value
4949
//~| ERROR borrow of moved value
5050
//~| ERROR borrow of moved value
51-
//~| ERROR use of moved value
51+
//~| ERROR use of partially moved value
5252
None => {}
5353
}
5454
match Some([U, U]) {
5555
mut a @ Some([ref b, ref mut c]) => {}
5656
//~^ ERROR borrow of moved value
57-
//~| ERROR borrow of moved value
5857
None => {}
5958
}
6059
match Some(u()) {
6160
a @ Some(ref b) => {}
6261
//~^ ERROR borrow of moved value
63-
//~| ERROR borrow of moved value
6462
None => {}
6563
}
6664
match Some((u(), u())) {
6765
a @ Some((mut b @ ref mut c, d @ ref e)) => {}
6866
//~^ ERROR borrow of moved value
6967
//~| ERROR borrow of moved value
7068
//~| ERROR borrow of moved value
71-
//~| ERROR use of moved value
69+
//~| ERROR use of partially moved value
7270
None => {}
7371
}
7472
match Some([u(), u()]) {
7573
mut a @ Some([ref b, ref mut c]) => {}
7674
//~^ ERROR borrow of moved value
77-
//~| ERROR borrow of moved value
7875
None => {}
7976
}
8077
}

0 commit comments

Comments
 (0)