Skip to content

Commit 6bdce7b

Browse files
committed
new fix method and update tests
1 parent 9088807 commit 6bdce7b

16 files changed

+143
-135
lines changed

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

+23-15
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
4444
candidate: &mut Candidate<'pat, 'tcx>,
4545
) -> bool {
4646
// repeatedly simplify match pairs until fixed point is reached
47+
debug!("simplify_candidate(candidate={:?})", candidate);
48+
let mut new_bindings = Vec::new();
4749
loop {
4850
let match_pairs = mem::take(&mut candidate.match_pairs);
4951

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

5759
let mut changed = false;
5860
for match_pair in match_pairs {
59-
match self.simplify_match_pair(match_pair, candidate) {
61+
match self.simplify_match_pair(match_pair, candidate, &mut new_bindings) {
6062
Ok(()) => {
6163
changed = true;
6264
}
@@ -65,13 +67,31 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6567
}
6668
}
6769
}
70+
// issue #69971: the binding order should be right to left if there are more
71+
// bindings after `@` to please the borrow checker
72+
// Ex
73+
// struct NonCopyStruct {
74+
// copy_field: u32,
75+
// }
76+
//
77+
// fn foo1(x: NonCopyStruct) {
78+
// let y @ NonCopyStruct { copy_field: z } = x;
79+
// // the above should turn into
80+
// let z = x.copy_field;
81+
// let y = x;
82+
// }
83+
new_bindings.extend_from_slice(&candidate.bindings);
84+
mem::swap(&mut candidate.bindings, &mut new_bindings);
85+
new_bindings.clear();
86+
6887
if !changed {
6988
// Move or-patterns to the end, because they can result in us
7089
// creating additional candidates, so we want to test them as
7190
// late as possible.
7291
candidate
7392
.match_pairs
7493
.sort_by_key(|pair| matches!(*pair.pattern.kind, PatKind::Or { .. }));
94+
debug!("simplify_candidate: simplifed {:?}", candidate);
7595
return false; // if we were not able to simplify any, done.
7696
}
7797
}
@@ -104,6 +124,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
104124
&mut self,
105125
match_pair: MatchPair<'pat, 'tcx>,
106126
candidate: &mut Candidate<'pat, 'tcx>,
127+
bindings: &mut Vec<Binding<'tcx>>,
107128
) -> Result<(), MatchPair<'pat, 'tcx>> {
108129
let tcx = self.hir.tcx();
109130
match *match_pair.pattern.kind {
@@ -131,20 +152,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
131152
}
132153

133154
PatKind::Binding { name, mutability, mode, var, ty, ref subpattern, is_primary: _ } => {
134-
// issue #69971: the binding order should be right to left if there are more
135-
// bindings after `@` to please the borrow checker
136-
// Ex
137-
// struct NonCopyStruct {
138-
// copy_field: u32,
139-
// }
140-
//
141-
// fn foo1(x: NonCopyStruct) {
142-
// let y @ NonCopyStruct { copy_field: z } = x;
143-
// // the above should turn into
144-
// let z = x.copy_field;
145-
// let y = x;
146-
// }
147-
candidate.bindings.insert(0, Binding {
155+
bindings.push(Binding {
148156
name,
149157
mutability,
150158
span: match_pair.pattern.span,

src/test/mir-opt/exponential_or.match_tuple.SimplifyCfg-initial.after.mir

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ fn match_tuple(_1: (u32, bool, Option<i32>, u32)) -> u32 {
6161
}
6262

6363
bb9: {
64-
StorageLive(_8); // scope 0 at $DIR/exponential-or.rs:8:57: 8:78
65-
_8 = (_1.3: u32); // scope 0 at $DIR/exponential-or.rs:8:57: 8:78
6664
StorageLive(_7); // scope 0 at $DIR/exponential-or.rs:8:10: 8:21
6765
_7 = (_1.0: u32); // scope 0 at $DIR/exponential-or.rs:8:10: 8:21
66+
StorageLive(_8); // scope 0 at $DIR/exponential-or.rs:8:57: 8:78
67+
_8 = (_1.3: u32); // scope 0 at $DIR/exponential-or.rs:8:57: 8:78
6868
StorageLive(_9); // scope 1 at $DIR/exponential-or.rs:8:83: 8:84
6969
_9 = _7; // scope 1 at $DIR/exponential-or.rs:8:83: 8:84
7070
StorageLive(_10); // scope 1 at $DIR/exponential-or.rs:8:87: 8:88
7171
_10 = _8; // scope 1 at $DIR/exponential-or.rs:8:87: 8:88
7272
_0 = BitXor(move _9, move _10); // scope 1 at $DIR/exponential-or.rs:8:83: 8:88
7373
StorageDead(_10); // scope 1 at $DIR/exponential-or.rs:8:87: 8:88
7474
StorageDead(_9); // scope 1 at $DIR/exponential-or.rs:8:87: 8:88
75-
StorageDead(_7); // scope 0 at $DIR/exponential-or.rs:8:87: 8:88
7675
StorageDead(_8); // scope 0 at $DIR/exponential-or.rs:8:87: 8:88
76+
StorageDead(_7); // scope 0 at $DIR/exponential-or.rs:8:87: 8:88
7777
goto -> bb10; // scope 0 at $DIR/exponential-or.rs:7:5: 10:6
7878
}
7979

src/test/mir-opt/issue_73223.main.PreCodegen.64bit.diff

+4-4
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,10 @@
8989
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL
9090
// + literal: Const { ty: &i32, val: Unevaluated(WithOptConstParam { did: DefId(0:3 ~ issue_73223[317d]::main), const_param_did: None }, [], Some(promoted[1])) }
9191
(_5.1: &i32) = move _6; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
92-
StorageLive(_8); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
93-
_8 = (_5.1: &i32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
9492
StorageLive(_7); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
9593
_7 = (_5.0: &i32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
94+
StorageLive(_8); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
95+
_8 = (_5.1: &i32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
9696
StorageLive(_9); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
9797
StorageLive(_10); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
9898
StorageLive(_11); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
@@ -106,8 +106,8 @@
106106

107107
bb1: {
108108
StorageDead(_9); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
109-
StorageDead(_7); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
110109
StorageDead(_8); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
110+
StorageDead(_7); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
111111
_0 = const (); // scope 0 at $DIR/issue-73223.rs:1:11: 9:2
112112
StorageDead(_1); // scope 0 at $DIR/issue-73223.rs:9:1: 9:2
113113
return; // scope 0 at $DIR/issue-73223.rs:9:2: 9:2
@@ -133,8 +133,8 @@
133133
_19 = &_20; // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
134134
(_17.1: &&i32) = move _19; // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
135135
StorageDead(_19); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
136-
_28 = (_17.1: &&i32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
137136
_25 = (_17.0: &&i32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
137+
_28 = (_17.1: &&i32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
138138
_24 = <&i32 as Debug>::fmt as for<'r, 's, 't0> fn(&'r &i32, &'s mut std::fmt::Formatter<'t0>) -> std::result::Result<(), std::fmt::Error> (Pointer(ReifyFnPointer)); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
139139
// mir::Constant
140140
// + span: $SRC_DIR/core/src/macros/mod.rs:LL:COL

src/test/mir-opt/issue_73223.main.SimplifyArmIdentity.64bit.diff

+6-6
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,10 @@
138138
(_9.1: &i32) = move _11; // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
139139
StorageDead(_11); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
140140
StorageDead(_10); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
141-
StorageLive(_14); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
142-
_14 = (_9.1: &i32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
143141
StorageLive(_13); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
144142
_13 = (_9.0: &i32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
143+
StorageLive(_14); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
144+
_14 = (_9.1: &i32); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
145145
StorageLive(_15); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
146146
StorageLive(_16); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
147147
StorageLive(_17); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
@@ -159,8 +159,8 @@
159159
bb3: {
160160
_8 = const (); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
161161
StorageDead(_15); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
162-
StorageDead(_13); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
163162
StorageDead(_14); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
163+
StorageDead(_13); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
164164
StorageDead(_9); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
165165
StorageDead(_8); // scope 3 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
166166
_0 = const (); // scope 0 at $DIR/issue-73223.rs:1:11: 9:2
@@ -205,10 +205,10 @@
205205
(_31.1: &&i32) = move _34; // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
206206
StorageDead(_34); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
207207
StorageDead(_32); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
208-
StorageLive(_37); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
209-
_37 = (_31.1: &&i32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
210208
StorageLive(_36); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
211209
_36 = (_31.0: &&i32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
210+
StorageLive(_37); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
211+
_37 = (_31.1: &&i32); // scope 4 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
212212
StorageLive(_38); // scope 5 at $SRC_DIR/std/src/macros.rs:LL:COL
213213
StorageLive(_39); // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
214214
_39 = _36; // scope 5 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
@@ -284,8 +284,8 @@
284284
_30 = [move _38, move _41]; // scope 5 at $SRC_DIR/std/src/macros.rs:LL:COL
285285
StorageDead(_41); // scope 5 at $SRC_DIR/std/src/macros.rs:LL:COL
286286
StorageDead(_38); // scope 5 at $SRC_DIR/std/src/macros.rs:LL:COL
287-
StorageDead(_36); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
288287
StorageDead(_37); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
288+
StorageDead(_36); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
289289
_29 = &_30; // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
290290
_28 = _29; // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL
291291
_27 = move _28 as &[std::fmt::ArgumentV1] (Pointer(Unsize)); // scope 4 at $SRC_DIR/std/src/macros.rs:LL:COL

src/test/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff

+6-6
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@
7878
}
7979

8080
bb2: {
81-
StorageLive(_15); // scope 1 at $DIR/issue_76432.rs:9:26: 9:32
82-
_15 = &(*_2)[2 of 3]; // scope 1 at $DIR/issue_76432.rs:9:26: 9:32
83-
StorageLive(_14); // scope 1 at $DIR/issue_76432.rs:9:18: 9:24
84-
_14 = &(*_2)[1 of 3]; // scope 1 at $DIR/issue_76432.rs:9:18: 9:24
8581
StorageLive(_13); // scope 1 at $DIR/issue_76432.rs:9:10: 9:16
8682
_13 = &(*_2)[0 of 3]; // scope 1 at $DIR/issue_76432.rs:9:10: 9:16
83+
StorageLive(_14); // scope 1 at $DIR/issue_76432.rs:9:18: 9:24
84+
_14 = &(*_2)[1 of 3]; // scope 1 at $DIR/issue_76432.rs:9:18: 9:24
85+
StorageLive(_15); // scope 1 at $DIR/issue_76432.rs:9:26: 9:32
86+
_15 = &(*_2)[2 of 3]; // scope 1 at $DIR/issue_76432.rs:9:26: 9:32
8787
StorageLive(_16); // scope 2 at $DIR/issue_76432.rs:9:38: 9:52
8888
StorageLive(_17); // scope 2 at $DIR/issue_76432.rs:9:38: 9:52
8989
_17 = &raw const (*_13); // scope 2 at $DIR/issue_76432.rs:9:38: 9:40
@@ -103,9 +103,9 @@
103103
StorageDead(_18); // scope 2 at $DIR/issue_76432.rs:9:84: 9:85
104104
StorageDead(_17); // scope 2 at $DIR/issue_76432.rs:9:84: 9:85
105105
StorageDead(_16); // scope 2 at $DIR/issue_76432.rs:9:84: 9:85
106-
StorageDead(_13); // scope 1 at $DIR/issue_76432.rs:9:84: 9:85
107-
StorageDead(_14); // scope 1 at $DIR/issue_76432.rs:9:84: 9:85
108106
StorageDead(_15); // scope 1 at $DIR/issue_76432.rs:9:84: 9:85
107+
StorageDead(_14); // scope 1 at $DIR/issue_76432.rs:9:84: 9:85
108+
StorageDead(_13); // scope 1 at $DIR/issue_76432.rs:9:84: 9:85
109109
StorageDead(_9); // scope 1 at $DIR/issue_76432.rs:11:6: 11:7
110110
_0 = const (); // scope 0 at $DIR/issue_76432.rs:6:44: 12:2
111111
StorageDead(_5); // scope 0 at $DIR/issue_76432.rs:12:1: 12:2

0 commit comments

Comments
 (0)