Skip to content

Commit

Permalink
Put new bindings first in refutable cases too
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Jan 25, 2024
1 parent e902878 commit 09d4613
Show file tree
Hide file tree
Showing 18 changed files with 229 additions and 209 deletions.
8 changes: 4 additions & 4 deletions compiler/rustc_mir_build/src/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// let z = (x as Variant).copy_field;
// // and raises an error
// }
let mut original_bindings = mem::take(&mut candidate.bindings);
let original_bindings = mem::take(&mut candidate.bindings);
let mut new_bindings = Vec::new();
// Repeatedly simplify match pairs until fixed point is reached
loop {
Expand Down Expand Up @@ -115,9 +115,9 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

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

let did_expand_or =
if let [MatchPair { pattern: Pat { kind: PatKind::Or { pats }, .. }, place }] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
- }
-
- bb3: {
StorageLive(_8);
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
StorageLive(_9);
_9 = (((_3.1: std::option::Option<u32>) as Some).0: u32);
StorageLive(_8);
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
_0 = const 0_u32;
StorageDead(_9);
StorageDead(_8);
StorageDead(_9);
- goto -> bb4;
+ goto -> bb3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
-
- bb4: {
+ bb2: {
StorageLive(_9);
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
StorageLive(_10);
_10 = (((_3.1: std::option::Option<u32>) as Some).0: u32);
StorageLive(_9);
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
_0 = const 0_u32;
StorageDead(_10);
StorageDead(_9);
StorageDead(_10);
- goto -> bb6;
+ goto -> bb4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
- }
-
- bb3: {
StorageLive(_8);
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
StorageLive(_9);
_9 = (((_3.1: std::option::Option<bool>) as Some).0: bool);
StorageLive(_8);
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
_0 = const 0_u32;
StorageDead(_9);
StorageDead(_8);
StorageDead(_9);
- goto -> bb4;
+ goto -> bb3;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@

- bb4: {
+ bb3: {
StorageLive(_11);
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32);
StorageLive(_12);
_12 = (((_4.1: std::option::Option<u32>) as Some).0: u32);
StorageLive(_13);
_13 = (((_4.2: std::option::Option<u32>) as Some).0: u32);
StorageLive(_12);
_12 = (((_4.1: std::option::Option<u32>) as Some).0: u32);
StorageLive(_11);
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32);
_0 = const 0_u32;
StorageDead(_13);
StorageDead(_12);
StorageDead(_11);
StorageDead(_12);
StorageDead(_13);
- goto -> bb5;
+ goto -> bb4;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,12 @@
}

bb6: {
StorageLive(_12);
_39 = deref_copy (_4.0: &ViewportPercentageLength);
_12 = (((*_39) as Vw).0: f32);
StorageLive(_13);
_40 = deref_copy (_4.1: &ViewportPercentageLength);
_13 = (((*_40) as Vw).0: f32);
_39 = deref_copy (_4.1: &ViewportPercentageLength);
_13 = (((*_39) as Vw).0: f32);
StorageLive(_12);
_40 = deref_copy (_4.0: &ViewportPercentageLength);
_12 = (((*_40) as Vw).0: f32);
StorageLive(_14);
StorageLive(_15);
_15 = _12;
Expand All @@ -132,18 +132,18 @@
StorageDead(_15);
_3 = ViewportPercentageLength::Vw(move _14);
StorageDead(_14);
StorageDead(_13);
StorageDead(_12);
StorageDead(_13);
goto -> bb10;
}

bb7: {
StorageLive(_17);
_41 = deref_copy (_4.0: &ViewportPercentageLength);
_17 = (((*_41) as Vh).0: f32);
StorageLive(_18);
_42 = deref_copy (_4.1: &ViewportPercentageLength);
_18 = (((*_42) as Vh).0: f32);
_41 = deref_copy (_4.1: &ViewportPercentageLength);
_18 = (((*_41) as Vh).0: f32);
StorageLive(_17);
_42 = deref_copy (_4.0: &ViewportPercentageLength);
_17 = (((*_42) as Vh).0: f32);
StorageLive(_19);
StorageLive(_20);
_20 = _17;
Expand All @@ -154,18 +154,18 @@
StorageDead(_20);
_3 = ViewportPercentageLength::Vh(move _19);
StorageDead(_19);
StorageDead(_18);
StorageDead(_17);
StorageDead(_18);
goto -> bb10;
}

bb8: {
StorageLive(_22);
_43 = deref_copy (_4.0: &ViewportPercentageLength);
_22 = (((*_43) as Vmin).0: f32);
StorageLive(_23);
_44 = deref_copy (_4.1: &ViewportPercentageLength);
_23 = (((*_44) as Vmin).0: f32);
_43 = deref_copy (_4.1: &ViewportPercentageLength);
_23 = (((*_43) as Vmin).0: f32);
StorageLive(_22);
_44 = deref_copy (_4.0: &ViewportPercentageLength);
_22 = (((*_44) as Vmin).0: f32);
StorageLive(_24);
StorageLive(_25);
_25 = _22;
Expand All @@ -176,18 +176,18 @@
StorageDead(_25);
_3 = ViewportPercentageLength::Vmin(move _24);
StorageDead(_24);
StorageDead(_23);
StorageDead(_22);
StorageDead(_23);
goto -> bb10;
}

bb9: {
StorageLive(_27);
_45 = deref_copy (_4.0: &ViewportPercentageLength);
_27 = (((*_45) as Vmax).0: f32);
StorageLive(_28);
_46 = deref_copy (_4.1: &ViewportPercentageLength);
_28 = (((*_46) as Vmax).0: f32);
_45 = deref_copy (_4.1: &ViewportPercentageLength);
_28 = (((*_45) as Vmax).0: f32);
StorageLive(_27);
_46 = deref_copy (_4.0: &ViewportPercentageLength);
_27 = (((*_46) as Vmax).0: f32);
StorageLive(_29);
StorageLive(_30);
_30 = _27;
Expand All @@ -198,8 +198,8 @@
StorageDead(_30);
_3 = ViewportPercentageLength::Vmax(move _29);
StorageDead(_29);
StorageDead(_28);
StorageDead(_27);
StorageDead(_28);
goto -> bb10;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
}

bb5: {
StorageLive(_9);
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
StorageLive(_10);
_10 = (((_3.1: std::option::Option<u32>) as Some).0: u32);
StorageLive(_9);
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32);
_0 = const 0_u32;
StorageDead(_10);
StorageDead(_9);
StorageDead(_10);
goto -> bb8;
}

Expand Down
3 changes: 1 addition & 2 deletions tests/ui/pattern/bindings-after-at/bind-by-copy.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// run-pass
#![allow(unused)]

// Test copy
Expand Down Expand Up @@ -41,7 +42,6 @@ pub fn main() {

match (E::E { a: 10, e: C { c: 20 } }) {
x @ E::E{ a, e: C { c } } => {
//~^ ERROR use of moved value
assert!(matches!(x, E::E { a: 10, e: C { c: 20 } }));
assert!(a == 10);
assert!(c == 20);
Expand All @@ -50,7 +50,6 @@ pub fn main() {
}
match (E::E { a: 10, e: C { c: 20 } }) {
mut x @ E::E{ a, e: C { mut c } } => {
//~^ ERROR use of moved value
x = E::NotE;
c += 30;
assert_eq!(c, 50);
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/pattern/bindings-after-at/borrowck-move-and-move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn main() {
let a @ (b, c) = (u(), u()); //~ ERROR use of partially moved value

match Ok(U) {
a @ Ok(b) | a @ Err(b) => {} //~ ERROR use of moved value
//~^ ERROR use of moved value
a @ Ok(b) | a @ Err(b) => {} //~ ERROR use of partially moved value
//~^ ERROR use of partially moved value
}

fn fun(a @ b: U) {} //~ ERROR use of moved value
Expand Down
30 changes: 14 additions & 16 deletions tests/ui/pattern/bindings-after-at/borrowck-move-and-move.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,35 +40,33 @@ help: borrow this binding in the pattern to avoid moving the value
LL | let ref a @ (b, ref c) = (u(), u());
| +++ +++

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

error[E0382]: use of moved value
--> $DIR/borrowck-move-and-move.rs:18:29
error[E0382]: use of partially moved value
--> $DIR/borrowck-move-and-move.rs:18:21
|
LL | match Ok(U) {
| ----- move occurs because value has type `Result<U, U>`, which does not implement the `Copy` trait
LL | a @ Ok(b) | a @ Err(b) => {}
| - ^ value used here after move
| ^ - value partially moved here
| |
| value moved here
| value used here after partial move
|
= note: partial move occurs because value has type `U`, which does not implement the `Copy` trait
help: borrow this binding in the pattern to avoid moving the value
|
LL | a @ Ok(b) | ref a @ Err(b) => {}
| +++
LL | a @ Ok(b) | ref a @ Err(ref b) => {}
| +++ +++

error[E0382]: use of partially moved value
--> $DIR/borrowck-move-and-move.rs:25:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,33 +48,30 @@ fn main() {
//~^ ERROR borrow of moved value
//~| ERROR borrow of moved value
//~| ERROR borrow of moved value
//~| ERROR use of moved value
//~| ERROR use of partially moved value
None => {}
}
match Some([U, U]) {
mut a @ Some([ref b, ref mut c]) => {}
//~^ ERROR borrow of moved value
//~| ERROR borrow of moved value
None => {}
}
match Some(u()) {
a @ Some(ref b) => {}
//~^ ERROR borrow of moved value
//~| ERROR borrow of moved value
None => {}
}
match Some((u(), u())) {
a @ Some((mut b @ ref mut c, d @ ref e)) => {}
//~^ ERROR borrow of moved value
//~| ERROR borrow of moved value
//~| ERROR borrow of moved value
//~| ERROR use of moved value
//~| ERROR use of partially moved value
None => {}
}
match Some([u(), u()]) {
mut a @ Some([ref b, ref mut c]) => {}
//~^ ERROR borrow of moved value
//~| ERROR borrow of moved value
None => {}
}
}
Loading

0 comments on commit 09d4613

Please sign in to comment.