Skip to content

Commit 1c858ba

Browse files
committed
Auto merge of rust-lang#88678 - matthewjasper:if-boolean-scoping, r=oli-obk
Change scope of temporaries in match guards Each pattern in a match arm has its own copy of the match guard in MIR, with its own temporary, so it has to be dropped before the the guards are joined to the single copy of the arm. This PR changes `then_else_break` to allow it to put the temporary in the innermost scope possible. This change isn't done for `if` expressions because that affects a large number of mir-opt tests and could more significantly affect performance. closes rust-lang#88649 r? `@oli-obk`
2 parents d19d864 + ad7f109 commit 1c858ba

10 files changed

+25
-24
lines changed

compiler/rustc_mir_build/src/build/expr/into.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6868
let then_blk = unpack!(this.then_else_break(
6969
block,
7070
&this.thir[cond],
71-
condition_scope,
71+
Some(condition_scope),
7272
condition_scope,
7373
then_expr.span,
7474
));

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
3939
&mut self,
4040
mut block: BasicBlock,
4141
expr: &Expr<'tcx>,
42-
temp_scope: region::Scope,
42+
temp_scope_override: Option<region::Scope>,
4343
break_scope: region::Scope,
4444
variable_scope_span: Span,
4545
) -> BlockAnd<()> {
@@ -53,7 +53,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
5353
this.then_else_break(
5454
block,
5555
&this.thir[value],
56-
temp_scope,
56+
temp_scope_override,
5757
break_scope,
5858
variable_scope_span,
5959
)
@@ -63,6 +63,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6363
this.lower_let_expr(block, &this.thir[expr], pat, break_scope, variable_scope_span)
6464
}
6565
_ => {
66+
let temp_scope = temp_scope_override.unwrap_or_else(|| this.local_scope());
6667
let mutability = Mutability::Mut;
6768
let place =
6869
unpack!(block = this.as_temp(block, Some(temp_scope), expr, mutability));
@@ -1948,7 +1949,6 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19481949
}
19491950

19501951
let arm_span = arm_span.unwrap();
1951-
let arm_scope = self.local_scope();
19521952
let match_scope = match_scope.unwrap();
19531953
let mut guard_span = rustc_span::DUMMY_SP;
19541954

@@ -1957,7 +1957,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
19571957
Guard::If(e) => {
19581958
let e = &this.thir[e];
19591959
guard_span = e.span;
1960-
this.then_else_break(block, e, arm_scope, match_scope, arm_span)
1960+
this.then_else_break(block, e, None, match_scope, arm_span)
19611961
}
19621962
Guard::IfLet(ref pat, scrutinee) => {
19631963
let s = &this.thir[scrutinee];

src/test/mir-opt/match_arm_scopes.complicated_match.SimplifyCfg-initial.after-ElaborateDrops.after.diff

+6-5
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
+ bb6: {
9494
_0 = const 3_i32; // scope 0 at $DIR/match-arm-scopes.rs:15:59: 15:60
9595
StorageDead(_10); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
96-
StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
96+
StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
9797
- goto -> bb23; // scope 0 at no-location
9898
+ goto -> bb20; // scope 0 at no-location
9999
}
@@ -108,6 +108,7 @@
108108
- bb11: {
109109
+ bb8: {
110110
StorageDead(_10); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
111+
StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
111112
- FakeRead(ForMatchGuard, _3); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
112113
- FakeRead(ForMatchGuard, _4); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
113114
- FakeRead(ForGuardBinding, _6); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
@@ -123,7 +124,7 @@
123124
- bb12: {
124125
+ bb9: {
125126
StorageDead(_10); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
126-
StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
127+
StorageDead(_9); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
127128
StorageDead(_8); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
128129
StorageDead(_6); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
129130
- falseEdge -> [real: bb2, imaginary: bb3]; // scope 0 at $DIR/match-arm-scopes.rs:15:42: 15:73
@@ -149,7 +150,7 @@
149150
+ bb11: {
150151
_0 = const 3_i32; // scope 0 at $DIR/match-arm-scopes.rs:15:59: 15:60
151152
StorageDead(_13); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
152-
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
153+
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
153154
- goto -> bb23; // scope 0 at no-location
154155
+ goto -> bb20; // scope 0 at no-location
155156
}
@@ -164,6 +165,7 @@
164165
- bb16: {
165166
+ bb13: {
166167
StorageDead(_13); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
168+
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
167169
- FakeRead(ForMatchGuard, _3); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
168170
- FakeRead(ForMatchGuard, _4); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
169171
- FakeRead(ForGuardBinding, _6); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
@@ -179,7 +181,7 @@
179181
- bb17: {
180182
+ bb14: {
181183
StorageDead(_13); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
182-
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
184+
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:72: 15:73
183185
StorageDead(_8); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
184186
StorageDead(_6); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
185187
- falseEdge -> [real: bb4, imaginary: bb5]; // scope 0 at $DIR/match-arm-scopes.rs:15:42: 15:73
@@ -190,7 +192,6 @@
190192
+ bb15: {
191193
StorageDead(_7); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
192194
StorageDead(_5); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
193-
StorageDead(_12); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
194195
StorageDead(_8); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
195196
StorageDead(_6); // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78
196197
- goto -> bb22; // scope 0 at $DIR/match-arm-scopes.rs:15:77: 15:78

src/test/mir-opt/match_false_edges.full_tested_match.PromoteTemps.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ fn full_tested_match() -> () {
7272
}
7373

7474
bb7: {
75+
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:16:26: 16:27
7576
FakeRead(ForMatchGuard, _4); // scope 0 at $DIR/match_false_edges.rs:16:26: 16:27
7677
FakeRead(ForGuardBinding, _6); // scope 0 at $DIR/match_false_edges.rs:16:26: 16:27
7778
StorageLive(_5); // scope 0 at $DIR/match_false_edges.rs:16:14: 16:15
@@ -81,13 +82,12 @@ fn full_tested_match() -> () {
8182
_1 = (const 1_i32, move _8); // scope 2 at $DIR/match_false_edges.rs:16:31: 16:37
8283
StorageDead(_8); // scope 2 at $DIR/match_false_edges.rs:16:36: 16:37
8384
StorageDead(_5); // scope 0 at $DIR/match_false_edges.rs:16:36: 16:37
84-
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:16:36: 16:37
8585
StorageDead(_6); // scope 0 at $DIR/match_false_edges.rs:16:36: 16:37
8686
goto -> bb10; // scope 0 at $DIR/match_false_edges.rs:16:36: 16:37
8787
}
8888

8989
bb8: {
90-
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:16:36: 16:37
90+
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:16:26: 16:27
9191
StorageDead(_6); // scope 0 at $DIR/match_false_edges.rs:16:36: 16:37
9292
goto -> bb3; // scope 0 at $DIR/match_false_edges.rs:16:20: 16:27
9393
}

src/test/mir-opt/match_false_edges.full_tested_match2.PromoteTemps.before.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ fn full_tested_match2() -> () {
7070
}
7171

7272
bb7: {
73+
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:27:26: 27:27
7374
FakeRead(ForMatchGuard, _4); // scope 0 at $DIR/match_false_edges.rs:27:26: 27:27
7475
FakeRead(ForGuardBinding, _6); // scope 0 at $DIR/match_false_edges.rs:27:26: 27:27
7576
StorageLive(_5); // scope 0 at $DIR/match_false_edges.rs:27:14: 27:15
@@ -79,13 +80,12 @@ fn full_tested_match2() -> () {
7980
_1 = (const 1_i32, move _8); // scope 2 at $DIR/match_false_edges.rs:27:31: 27:37
8081
StorageDead(_8); // scope 2 at $DIR/match_false_edges.rs:27:36: 27:37
8182
StorageDead(_5); // scope 0 at $DIR/match_false_edges.rs:27:36: 27:37
82-
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:27:36: 27:37
8383
StorageDead(_6); // scope 0 at $DIR/match_false_edges.rs:27:36: 27:37
8484
goto -> bb10; // scope 0 at $DIR/match_false_edges.rs:27:36: 27:37
8585
}
8686

8787
bb8: {
88-
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:27:36: 27:37
88+
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:27:26: 27:27
8989
StorageDead(_6); // scope 0 at $DIR/match_false_edges.rs:27:36: 27:37
9090
falseEdge -> [real: bb3, imaginary: bb1]; // scope 0 at $DIR/match_false_edges.rs:27:20: 27:27
9191
}

src/test/mir-opt/match_false_edges.main.PromoteTemps.before.mir

+4-4
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ fn main() -> () {
7878
}
7979

8080
bb7: {
81+
StorageDead(_8); // scope 0 at $DIR/match_false_edges.rs:36:27: 36:28
8182
FakeRead(ForMatchGuard, _5); // scope 0 at $DIR/match_false_edges.rs:36:27: 36:28
8283
FakeRead(ForGuardBinding, _7); // scope 0 at $DIR/match_false_edges.rs:36:27: 36:28
8384
StorageLive(_6); // scope 0 at $DIR/match_false_edges.rs:36:14: 36:16
8485
_6 = ((_2 as Some).0: i32); // scope 0 at $DIR/match_false_edges.rs:36:14: 36:16
8586
_1 = const 1_i32; // scope 2 at $DIR/match_false_edges.rs:36:32: 36:33
8687
StorageDead(_6); // scope 0 at $DIR/match_false_edges.rs:36:32: 36:33
87-
StorageDead(_8); // scope 0 at $DIR/match_false_edges.rs:36:32: 36:33
8888
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:36:32: 36:33
8989
goto -> bb14; // scope 0 at $DIR/match_false_edges.rs:36:32: 36:33
9090
}
9191

9292
bb8: {
93-
StorageDead(_8); // scope 0 at $DIR/match_false_edges.rs:36:32: 36:33
93+
StorageDead(_8); // scope 0 at $DIR/match_false_edges.rs:36:27: 36:28
9494
StorageDead(_7); // scope 0 at $DIR/match_false_edges.rs:36:32: 36:33
9595
falseEdge -> [real: bb1, imaginary: bb1]; // scope 0 at $DIR/match_false_edges.rs:36:21: 36:28
9696
}
@@ -122,20 +122,20 @@ fn main() -> () {
122122

123123
bb12: {
124124
StorageDead(_13); // scope 0 at $DIR/match_false_edges.rs:38:28: 38:29
125+
StorageDead(_12); // scope 0 at $DIR/match_false_edges.rs:38:28: 38:29
125126
FakeRead(ForMatchGuard, _5); // scope 0 at $DIR/match_false_edges.rs:38:28: 38:29
126127
FakeRead(ForGuardBinding, _11); // scope 0 at $DIR/match_false_edges.rs:38:28: 38:29
127128
StorageLive(_10); // scope 0 at $DIR/match_false_edges.rs:38:14: 38:15
128129
_10 = ((_2 as Some).0: i32); // scope 0 at $DIR/match_false_edges.rs:38:14: 38:15
129130
_1 = const 3_i32; // scope 4 at $DIR/match_false_edges.rs:38:33: 38:34
130131
StorageDead(_10); // scope 0 at $DIR/match_false_edges.rs:38:33: 38:34
131-
StorageDead(_12); // scope 0 at $DIR/match_false_edges.rs:38:33: 38:34
132132
StorageDead(_11); // scope 0 at $DIR/match_false_edges.rs:38:33: 38:34
133133
goto -> bb14; // scope 0 at $DIR/match_false_edges.rs:38:33: 38:34
134134
}
135135

136136
bb13: {
137137
StorageDead(_13); // scope 0 at $DIR/match_false_edges.rs:38:28: 38:29
138-
StorageDead(_12); // scope 0 at $DIR/match_false_edges.rs:38:33: 38:34
138+
StorageDead(_12); // scope 0 at $DIR/match_false_edges.rs:38:28: 38:29
139139
StorageDead(_11); // scope 0 at $DIR/match_false_edges.rs:38:33: 38:34
140140
falseEdge -> [real: bb3, imaginary: bb3]; // scope 0 at $DIR/match_false_edges.rs:38:20: 38:29
141141
}

src/test/mir-opt/match_test.main.SimplifyCfg-initial.after.mir

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ fn main() -> () {
7575
}
7676

7777
bb10: {
78+
StorageDead(_9); // scope 2 at $DIR/match_test.rs:13:18: 13:19
7879
FakeRead(ForMatchGuard, _8); // scope 2 at $DIR/match_test.rs:13:18: 13:19
7980
_3 = const 0_i32; // scope 2 at $DIR/match_test.rs:13:23: 13:24
80-
StorageDead(_9); // scope 2 at $DIR/match_test.rs:13:23: 13:24
8181
goto -> bb14; // scope 2 at $DIR/match_test.rs:13:23: 13:24
8282
}
8383

8484
bb11: {
85-
StorageDead(_9); // scope 2 at $DIR/match_test.rs:13:23: 13:24
85+
StorageDead(_9); // scope 2 at $DIR/match_test.rs:13:18: 13:19
8686
falseEdge -> [real: bb3, imaginary: bb6]; // scope 2 at $DIR/match_test.rs:13:18: 13:19
8787
}
8888

src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.32bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
+ _7 = move _2; // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:76
6767
StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:45:9: 45:10
6868
- _1 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:47:13: 47:17
69-
- goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:48:9: 48:10
69+
- goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:47:13: 47:17
7070
- }
7171
-
7272
- bb8: {

src/test/mir-opt/matches_reduce_branches.match_nested_if.MatchBranchSimplification.64bit.diff

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
+ _7 = move _2; // scope 0 at $DIR/matches_reduce_branches.rs:41:18: 41:76
6767
StorageDead(_2); // scope 0 at $DIR/matches_reduce_branches.rs:45:9: 45:10
6868
- _1 = const true; // scope 0 at $DIR/matches_reduce_branches.rs:47:13: 47:17
69-
- goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:48:9: 48:10
69+
- goto -> bb9; // scope 0 at $DIR/matches_reduce_branches.rs:47:13: 47:17
7070
- }
7171
-
7272
- bb8: {

src/test/mir-opt/remove_fake_borrows.match_guard.CleanupNonCodegenStatements.diff

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
}
4848

4949
bb5: {
50+
StorageDead(_8); // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
5051
- FakeRead(ForMatchGuard, _4); // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
5152
- FakeRead(ForMatchGuard, _5); // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
5253
- FakeRead(ForMatchGuard, _6); // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
@@ -56,12 +57,11 @@
5657
+ nop; // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
5758
+ nop; // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
5859
_0 = const 0_i32; // scope 0 at $DIR/remove_fake_borrows.rs:8:25: 8:26
59-
StorageDead(_8); // scope 0 at $DIR/remove_fake_borrows.rs:8:25: 8:26
6060
goto -> bb7; // scope 0 at $DIR/remove_fake_borrows.rs:8:25: 8:26
6161
}
6262

6363
bb6: {
64-
StorageDead(_8); // scope 0 at $DIR/remove_fake_borrows.rs:8:25: 8:26
64+
StorageDead(_8); // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
6565
goto -> bb1; // scope 0 at $DIR/remove_fake_borrows.rs:8:20: 8:21
6666
}
6767

0 commit comments

Comments
 (0)