Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reverse binding order in matches to allow the subbinding of copyable fields in bindings after @ #78638

Merged
merged 7 commits into from
Nov 5, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions compiler/rustc_mir_build/src/build/matches/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
candidate: &mut Candidate<'pat, 'tcx>,
) -> bool {
// repeatedly simplify match pairs until fixed point is reached
debug!("simplify_candidate(candidate={:?})", candidate);
vn-ki marked this conversation as resolved.
Show resolved Hide resolved
let mut new_bindings = Vec::new();
loop {
let match_pairs = mem::take(&mut candidate.match_pairs);

Expand All @@ -56,7 +58,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {

let mut changed = false;
for match_pair in match_pairs {
match self.simplify_match_pair(match_pair, candidate) {
match self.simplify_match_pair(match_pair, candidate, &mut new_bindings) {
Ok(()) => {
changed = true;
}
Expand All @@ -65,13 +67,31 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}
}
// issue #69971: the binding order should be right to left if there are more
vn-ki marked this conversation as resolved.
Show resolved Hide resolved
// bindings after `@` to please the borrow checker
// Ex
// struct NonCopyStruct {
// copy_field: u32,
// }
//
// fn foo1(x: NonCopyStruct) {
// let y @ NonCopyStruct { copy_field: z } = x;
// // the above should turn into
// let z = x.copy_field;
// let y = x;
// }
new_bindings.extend_from_slice(&candidate.bindings);
mem::swap(&mut candidate.bindings, &mut new_bindings);
new_bindings.clear();

if !changed {
// Move or-patterns to the end, because they can result in us
// creating additional candidates, so we want to test them as
// late as possible.
candidate
.match_pairs
.sort_by_key(|pair| matches!(*pair.pattern.kind, PatKind::Or { .. }));
debug!("simplify_candidate: simplifed {:?}", candidate);
vn-ki marked this conversation as resolved.
Show resolved Hide resolved
return false; // if we were not able to simplify any, done.
}
}
Expand Down Expand Up @@ -104,6 +124,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
&mut self,
match_pair: MatchPair<'pat, 'tcx>,
candidate: &mut Candidate<'pat, 'tcx>,
bindings: &mut Vec<Binding<'tcx>>,
) -> Result<(), MatchPair<'pat, 'tcx>> {
let tcx = self.hir.tcx();
match *match_pair.pattern.kind {
Expand Down Expand Up @@ -131,7 +152,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}

PatKind::Binding { name, mutability, mode, var, ty, ref subpattern, is_primary: _ } => {
candidate.bindings.push(Binding {
bindings.push(Binding {
name,
mutability,
span: match_pair.pattern.span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@
- }
-
- bb3: {
StorageLive(_8); // scope 0 at $DIR/early_otherwise_branch.rs:5:15: 5:16
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:5:15: 5:16
oli-obk marked this conversation as resolved.
Show resolved Hide resolved
StorageLive(_9); // scope 0 at $DIR/early_otherwise_branch.rs:5:24: 5:25
_9 = (((_3.1: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:5:24: 5:25
StorageLive(_8); // scope 0 at $DIR/early_otherwise_branch.rs:5:15: 5:16
_8 = (((_3.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:5:15: 5:16
_0 = const 0_u32; // scope 1 at $DIR/early_otherwise_branch.rs:5:31: 5:32
StorageDead(_9); // scope 0 at $DIR/early_otherwise_branch.rs:5:31: 5:32
StorageDead(_8); // scope 0 at $DIR/early_otherwise_branch.rs:5:31: 5:32
StorageDead(_9); // scope 0 at $DIR/early_otherwise_branch.rs:5:31: 5:32
- goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
+ goto -> bb3; // scope 0 at $DIR/early_otherwise_branch.rs:4:5: 7:6
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
-
- bb4: {
+ bb2: {
StorageLive(_9); // scope 0 at $DIR/early_otherwise_branch.rs:13:15: 13:16
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:13:15: 13:16
StorageLive(_10); // scope 0 at $DIR/early_otherwise_branch.rs:13:24: 13:25
_10 = (((_3.1: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:13:24: 13:25
StorageLive(_9); // scope 0 at $DIR/early_otherwise_branch.rs:13:15: 13:16
_9 = (((_3.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch.rs:13:15: 13:16
_0 = const 0_u32; // scope 1 at $DIR/early_otherwise_branch.rs:13:31: 13:32
StorageDead(_10); // scope 0 at $DIR/early_otherwise_branch.rs:13:31: 13:32
StorageDead(_9); // scope 0 at $DIR/early_otherwise_branch.rs:13:31: 13:32
StorageDead(_10); // scope 0 at $DIR/early_otherwise_branch.rs:13:31: 13:32
- goto -> bb6; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch.rs:12:5: 16:6
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,16 @@

- bb4: {
+ bb3: {
StorageLive(_11); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:15: 6:16
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:15: 6:16
StorageLive(_12); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:24: 6:25
_12 = (((_4.1: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:24: 6:25
StorageLive(_13); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:33: 6:34
_13 = (((_4.2: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:33: 6:34
StorageLive(_12); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:24: 6:25
_12 = (((_4.1: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:24: 6:25
StorageLive(_11); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:15: 6:16
_11 = (((_4.0: std::option::Option<u32>) as Some).0: u32); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:15: 6:16
_0 = const 0_u32; // scope 1 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
StorageDead(_13); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
StorageDead(_11); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
StorageDead(_12); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
StorageDead(_13); // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:6:40: 6:41
- goto -> bb5; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
+ goto -> bb4; // scope 0 at $DIR/early_otherwise_branch_3_element_tuple.rs:5:5: 8:6
}
Expand Down
Loading