Skip to content

Commit 5856307

Browse files
committed
Put new bindings first in refutable cases too
1 parent 0a54fc9 commit 5856307

12 files changed

+187
-167
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/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
}

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

+21-54
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ LL | ref mut a @ Some([ref b, ref mut c]) => {}
215215
| +++
216216

217217
error: borrow of moved value
218-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:61:9
218+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:60:9
219219
|
220220
LL | a @ Some(ref b) => {}
221221
| ^ ----- value borrowed here after move
@@ -229,7 +229,7 @@ LL | ref a @ Some(ref b) => {}
229229
| +++
230230

231231
error: borrow of moved value
232-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:9
232+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:65:9
233233
|
234234
LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
235235
| ^ --------- ----- value borrowed here after move
@@ -244,7 +244,7 @@ LL | ref a @ Some((mut b @ ref mut c, d @ ref e)) => {}
244244
| +++
245245

246246
error: borrow of moved value
247-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:19
247+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:65:19
248248
|
249249
LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
250250
| ^^^^^ --------- value borrowed here after move
@@ -258,7 +258,7 @@ LL | a @ Some((ref mut b @ ref mut c, d @ ref e)) => {}
258258
| +++
259259

260260
error: borrow of moved value
261-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:38
261+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:65:38
262262
|
263263
LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
264264
| ^ ----- value borrowed here after move
@@ -272,7 +272,7 @@ LL | a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
272272
| +++
273273

274274
error: borrow of moved value
275-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:75:9
275+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:73:9
276276
|
277277
LL | mut a @ Some([ref b, ref mut c]) => {}
278278
| ^^^^^ ----- --------- value borrowed here after move
@@ -314,66 +314,33 @@ help: borrow this binding in the pattern to avoid moving the value
314314
LL | let ref a @ (mut b @ ref mut c, ref d @ ref e) = (u(), u());
315315
| +++ +++
316316

317-
error[E0382]: use of moved value
318-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:47:38
317+
error[E0382]: use of partially moved value
318+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:47:9
319319
|
320-
LL | match Some((U, U)) {
321-
| ------------ move occurs because value has type `Option<(U, U)>`, which does not implement the `Copy` trait
322320
LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
323-
| - value moved here ^ value used here after move
324-
|
325-
help: borrow this binding in the pattern to avoid moving the value
326-
|
327-
LL | ref a @ Some((mut b @ ref mut c, d @ ref e)) => {}
328-
| +++
329-
330-
error[E0382]: borrow of moved value
331-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:55:30
332-
|
333-
LL | match Some([U, U]) {
334-
| ------------ move occurs because value has type `Option<[U; 2]>`, which does not implement the `Copy` trait
335-
LL | mut a @ Some([ref b, ref mut c]) => {}
336-
| ----- ^^^^^^^^^ value borrowed here after move
337-
| |
338-
| value moved here
339-
340-
error[E0382]: borrow of moved value
341-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:61:18
342-
|
343-
LL | match Some(u()) {
344-
| --------- move occurs because value has type `Option<U>`, which does not implement the `Copy` trait
345-
LL | a @ Some(ref b) => {}
346-
| - ^^^^^ value borrowed here after move
321+
| ^ - value partially moved here
347322
| |
348-
| value moved here
323+
| value used here after partial move
349324
|
325+
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
350326
help: borrow this binding in the pattern to avoid moving the value
351327
|
352-
LL | ref a @ Some(ref b) => {}
353-
| +++
328+
LL | ref a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
329+
| +++ +++
354330

355-
error[E0382]: use of moved value
356-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:67:38
331+
error[E0382]: use of partially moved value
332+
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:65:9
357333
|
358-
LL | match Some((u(), u())) {
359-
| ---------------- move occurs because value has type `Option<(U, U)>`, which does not implement the `Copy` trait
360334
LL | a @ Some((mut b @ ref mut c, d @ ref e)) => {}
361-
| - value moved here ^ value used here after move
335+
| ^ - value partially moved here
336+
| |
337+
| value used here after partial move
362338
|
339+
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
363340
help: borrow this binding in the pattern to avoid moving the value
364341
|
365-
LL | ref a @ Some((mut b @ ref mut c, d @ ref e)) => {}
366-
| +++
367-
368-
error[E0382]: borrow of moved value
369-
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:75:30
370-
|
371-
LL | match Some([u(), u()]) {
372-
| ---------------- move occurs because value has type `Option<[U; 2]>`, which does not implement the `Copy` trait
373-
LL | mut a @ Some([ref b, ref mut c]) => {}
374-
| ----- ^^^^^^^^^ value borrowed here after move
375-
| |
376-
| value moved here
342+
LL | ref a @ Some((mut b @ ref mut c, ref d @ ref e)) => {}
343+
| +++ +++
377344

378345
error: borrow of moved value
379346
--> $DIR/borrowck-pat-by-move-and-ref-inverse.rs:11:11
@@ -457,6 +424,6 @@ help: borrow this binding in the pattern to avoid moving the value
457424
LL | fn f3(ref a @ [ref mut b, ref c]: [U; 2]) {}
458425
| +++
459426

460-
error: aborting due to 33 previous errors
427+
error: aborting due to 30 previous errors
461428

462429
For more information about this error, try `rustc --explain E0382`.

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

+3
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ fn main() {
5858
match Some([U, U]) {
5959
ref mut a @ Some([b, mut c]) => {}
6060
//~^ ERROR cannot move out of value because it is borrowed
61+
//~| ERROR borrow of partially moved value
6162
None => {}
6263
}
6364
match Some(u()) {
6465
ref a @ Some(b) => {}
6566
//~^ ERROR cannot move out of value because it is borrowed
67+
//~| ERROR borrow of partially moved value
6668
None => {}
6769
}
6870
match Some((u(), u())) {
@@ -77,6 +79,7 @@ fn main() {
7779
match Some([u(), u()]) {
7880
ref mut a @ Some([b, mut c]) => {}
7981
//~^ ERROR cannot move out of value because it is borrowed
82+
//~| ERROR borrow of partially moved value
8083
None => {}
8184
}
8285
}

0 commit comments

Comments
 (0)