Skip to content

Commit a5e27a1

Browse files
committed
Only create scopes for if let
1 parent e39c92e commit a5e27a1

File tree

55 files changed

+756
-850
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+756
-850
lines changed

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

+20-4
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6363
(if_then_scope, then_source_info),
6464
LintLevel::Inherited,
6565
|this| {
66-
let variable_scope =
67-
this.new_source_scope(then_expr.span, LintLevel::Inherited, None);
68-
this.source_scope = variable_scope;
66+
let source_info = if this.is_let(cond) {
67+
let variable_scope = this.new_source_scope(
68+
then_expr.span,
69+
LintLevel::Inherited,
70+
None,
71+
);
72+
this.source_scope = variable_scope;
73+
SourceInfo { span: then_expr.span, scope: variable_scope }
74+
} else {
75+
this.source_info(then_expr.span)
76+
};
6977
let (then_block, else_block) =
7078
this.in_if_then_scope(condition_scope, |this| {
7179
let then_blk = unpack!(this.then_else_break(
7280
block,
7381
&this.thir[cond],
7482
Some(condition_scope),
7583
condition_scope,
76-
SourceInfo { span: then_expr.span, scope: variable_scope }
84+
source_info
7785
));
7886

7987
this.expr_into_dest(destination, then_blk, then_expr)
@@ -579,4 +587,12 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
579587

580588
block_and
581589
}
590+
591+
fn is_let(&self, expr: ExprId) -> bool {
592+
match self.thir[expr].kind {
593+
ExprKind::Let { .. } => true,
594+
ExprKind::Scope { value, .. } => self.is_let(value),
595+
_ => false,
596+
}
597+
}
582598
}

src/test/mir-opt/bool_compare.opt1.InstCombine.diff

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:2:21: 2:24
77
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:3:8: 3:17
88
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:3:8: 3:9
9-
scope 1 {
10-
}
119

1210
bb0: {
13-
StorageLive(_2); // scope 1 at $DIR/bool_compare.rs:3:8: 3:17
14-
StorageLive(_3); // scope 1 at $DIR/bool_compare.rs:3:8: 3:9
15-
_3 = _1; // scope 1 at $DIR/bool_compare.rs:3:8: 3:9
16-
- _2 = Ne(move _3, const true); // scope 1 at $DIR/bool_compare.rs:3:8: 3:17
17-
+ _2 = Not(move _3); // scope 1 at $DIR/bool_compare.rs:3:8: 3:17
18-
StorageDead(_3); // scope 1 at $DIR/bool_compare.rs:3:16: 3:17
19-
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 1 at $DIR/bool_compare.rs:3:8: 3:17
11+
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:3:8: 3:17
12+
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:3:8: 3:9
13+
_3 = _1; // scope 0 at $DIR/bool_compare.rs:3:8: 3:9
14+
- _2 = Ne(move _3, const true); // scope 0 at $DIR/bool_compare.rs:3:8: 3:17
15+
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:3:8: 3:17
16+
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:3:16: 3:17
17+
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:3:8: 3:17
2018
}
2119

2220
bb1: {
23-
_0 = const 0_u32; // scope 1 at $DIR/bool_compare.rs:3:20: 3:21
21+
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:3:20: 3:21
2422
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:3:5: 3:34
2523
}
2624

src/test/mir-opt/bool_compare.opt2.InstCombine.diff

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:7:21: 7:24
77
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:8:8: 8:17
88
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:8:16: 8:17
9-
scope 1 {
10-
}
119

1210
bb0: {
13-
StorageLive(_2); // scope 1 at $DIR/bool_compare.rs:8:8: 8:17
14-
StorageLive(_3); // scope 1 at $DIR/bool_compare.rs:8:16: 8:17
15-
_3 = _1; // scope 1 at $DIR/bool_compare.rs:8:16: 8:17
16-
- _2 = Ne(const true, move _3); // scope 1 at $DIR/bool_compare.rs:8:8: 8:17
17-
+ _2 = Not(move _3); // scope 1 at $DIR/bool_compare.rs:8:8: 8:17
18-
StorageDead(_3); // scope 1 at $DIR/bool_compare.rs:8:16: 8:17
19-
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 1 at $DIR/bool_compare.rs:8:8: 8:17
11+
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:8:8: 8:17
12+
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:8:16: 8:17
13+
_3 = _1; // scope 0 at $DIR/bool_compare.rs:8:16: 8:17
14+
- _2 = Ne(const true, move _3); // scope 0 at $DIR/bool_compare.rs:8:8: 8:17
15+
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:8:8: 8:17
16+
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:8:16: 8:17
17+
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:8:8: 8:17
2018
}
2119

2220
bb1: {
23-
_0 = const 0_u32; // scope 1 at $DIR/bool_compare.rs:8:20: 8:21
21+
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:8:20: 8:21
2422
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:8:5: 8:34
2523
}
2624

src/test/mir-opt/bool_compare.opt3.InstCombine.diff

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:12:21: 12:24
77
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:13:8: 13:18
88
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:13:8: 13:9
9-
scope 1 {
10-
}
119

1210
bb0: {
13-
StorageLive(_2); // scope 1 at $DIR/bool_compare.rs:13:8: 13:18
14-
StorageLive(_3); // scope 1 at $DIR/bool_compare.rs:13:8: 13:9
15-
_3 = _1; // scope 1 at $DIR/bool_compare.rs:13:8: 13:9
16-
- _2 = Eq(move _3, const false); // scope 1 at $DIR/bool_compare.rs:13:8: 13:18
17-
+ _2 = Not(move _3); // scope 1 at $DIR/bool_compare.rs:13:8: 13:18
18-
StorageDead(_3); // scope 1 at $DIR/bool_compare.rs:13:17: 13:18
19-
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 1 at $DIR/bool_compare.rs:13:8: 13:18
11+
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:13:8: 13:18
12+
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:13:8: 13:9
13+
_3 = _1; // scope 0 at $DIR/bool_compare.rs:13:8: 13:9
14+
- _2 = Eq(move _3, const false); // scope 0 at $DIR/bool_compare.rs:13:8: 13:18
15+
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:13:8: 13:18
16+
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:13:17: 13:18
17+
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:13:8: 13:18
2018
}
2119

2220
bb1: {
23-
_0 = const 0_u32; // scope 1 at $DIR/bool_compare.rs:13:21: 13:22
21+
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:13:21: 13:22
2422
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:13:5: 13:35
2523
}
2624

src/test/mir-opt/bool_compare.opt4.InstCombine.diff

+8-10
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,19 @@
66
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:17:21: 17:24
77
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:18:8: 18:18
88
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:18:17: 18:18
9-
scope 1 {
10-
}
119

1210
bb0: {
13-
StorageLive(_2); // scope 1 at $DIR/bool_compare.rs:18:8: 18:18
14-
StorageLive(_3); // scope 1 at $DIR/bool_compare.rs:18:17: 18:18
15-
_3 = _1; // scope 1 at $DIR/bool_compare.rs:18:17: 18:18
16-
- _2 = Eq(const false, move _3); // scope 1 at $DIR/bool_compare.rs:18:8: 18:18
17-
+ _2 = Not(move _3); // scope 1 at $DIR/bool_compare.rs:18:8: 18:18
18-
StorageDead(_3); // scope 1 at $DIR/bool_compare.rs:18:17: 18:18
19-
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 1 at $DIR/bool_compare.rs:18:8: 18:18
11+
StorageLive(_2); // scope 0 at $DIR/bool_compare.rs:18:8: 18:18
12+
StorageLive(_3); // scope 0 at $DIR/bool_compare.rs:18:17: 18:18
13+
_3 = _1; // scope 0 at $DIR/bool_compare.rs:18:17: 18:18
14+
- _2 = Eq(const false, move _3); // scope 0 at $DIR/bool_compare.rs:18:8: 18:18
15+
+ _2 = Not(move _3); // scope 0 at $DIR/bool_compare.rs:18:8: 18:18
16+
StorageDead(_3); // scope 0 at $DIR/bool_compare.rs:18:17: 18:18
17+
switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/bool_compare.rs:18:8: 18:18
2018
}
2119

2220
bb1: {
23-
_0 = const 0_u32; // scope 1 at $DIR/bool_compare.rs:18:21: 18:22
21+
_0 = const 0_u32; // scope 0 at $DIR/bool_compare.rs:18:21: 18:22
2422
goto -> bb3; // scope 0 at $DIR/bool_compare.rs:18:5: 18:35
2523
}
2624

src/test/mir-opt/const_goto.issue_77355_opt.ConstGoto.diff

+11-13
Original file line numberDiff line numberDiff line change
@@ -7,35 +7,33 @@
77
- let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
88
- let mut _3: isize; // in scope 0 at $DIR/const_goto.rs:12:22: 12:28
99
+ let mut _2: isize; // in scope 0 at $DIR/const_goto.rs:12:22: 12:28
10-
scope 1 {
11-
}
1210

1311
bb0: {
14-
- StorageLive(_2); // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
15-
- _3 = discriminant(_1); // scope 1 at $DIR/const_goto.rs:12:17: 12:20
16-
- switchInt(move _3) -> [1_isize: bb2, 2_isize: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
17-
+ _2 = discriminant(_1); // scope 1 at $DIR/const_goto.rs:12:17: 12:20
18-
+ switchInt(move _2) -> [1_isize: bb2, 2_isize: bb2, otherwise: bb1]; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
12+
- StorageLive(_2); // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
13+
- _3 = discriminant(_1); // scope 0 at $DIR/const_goto.rs:12:17: 12:20
14+
- switchInt(move _3) -> [1_isize: bb2, 2_isize: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
15+
+ _2 = discriminant(_1); // scope 0 at $DIR/const_goto.rs:12:17: 12:20
16+
+ switchInt(move _2) -> [1_isize: bb2, 2_isize: bb2, otherwise: bb1]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
1917
}
2018

2119
bb1: {
22-
- _2 = const false; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
23-
- goto -> bb3; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
20+
- _2 = const false; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
21+
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
2422
+ _0 = const 42_u64; // scope 0 at $DIR/const_goto.rs:12:53: 12:55
2523
+ goto -> bb3; // scope 0 at $DIR/const_goto.rs:12:5: 12:57
2624
}
2725

2826
bb2: {
29-
- _2 = const true; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
30-
- goto -> bb3; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
27+
- _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
28+
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
3129
- }
3230
-
3331
- bb3: {
34-
- switchInt(move _2) -> [false: bb5, otherwise: bb4]; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
32+
- switchInt(move _2) -> [false: bb5, otherwise: bb4]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
3533
- }
3634
-
3735
- bb4: {
38-
_0 = const 23_u64; // scope 1 at $DIR/const_goto.rs:12:41: 12:43
36+
_0 = const 23_u64; // scope 0 at $DIR/const_goto.rs:12:41: 12:43
3937
- goto -> bb6; // scope 0 at $DIR/const_goto.rs:12:5: 12:57
4038
+ goto -> bb3; // scope 0 at $DIR/const_goto.rs:12:5: 12:57
4139
}

src/test/mir-opt/const_goto_storage.match_nested_if.ConstGoto.diff

+23-29
Original file line numberDiff line numberDiff line change
@@ -13,60 +13,54 @@
1313
scope 1 {
1414
debug val => _1; // in scope 1 at $DIR/const_goto_storage.rs:3:9: 3:12
1515
}
16-
scope 2 {
17-
scope 3 {
18-
scope 4 {
19-
}
20-
}
21-
}
2216

2317
bb0: {
2418
StorageLive(_1); // scope 0 at $DIR/const_goto_storage.rs:3:9: 3:12
2519
- StorageLive(_2); // scope 0 at $DIR/const_goto_storage.rs:3:21: 3:23
2620
- nop; // scope 0 at $DIR/const_goto_storage.rs:3:21: 3:23
2721
- StorageLive(_3); // scope 0 at $DIR/const_goto_storage.rs:4:15: 8:10
28-
- StorageLive(_4); // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
29-
- StorageLive(_5); // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
30-
- StorageLive(_6); // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
31-
- _6 = const true; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
32-
- switchInt(move _6) -> [false: bb2, otherwise: bb1]; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
33-
+ StorageLive(_2); // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
34-
+ _2 = const true; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
35-
+ switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
22+
- StorageLive(_4); // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
23+
- StorageLive(_5); // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
24+
- StorageLive(_6); // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
25+
- _6 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
26+
- switchInt(move _6) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
27+
+ StorageLive(_2); // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
28+
+ _2 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
29+
+ switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
3630
}
3731

3832
bb1: {
39-
- _5 = const true; // scope 4 at $DIR/const_goto_storage.rs:4:31: 4:35
40-
- goto -> bb3; // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
33+
- _5 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:31: 4:35
34+
- goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
4135
- }
4236
-
4337
- bb2: {
44-
- _5 = const false; // scope 3 at $DIR/const_goto_storage.rs:4:45: 4:50
45-
- goto -> bb3; // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
38+
- _5 = const false; // scope 0 at $DIR/const_goto_storage.rs:4:45: 4:50
39+
- goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
4640
- }
4741
-
4842
- bb3: {
49-
- StorageDead(_6); // scope 3 at $DIR/const_goto_storage.rs:4:51: 4:52
50-
- switchInt(move _5) -> [false: bb5, otherwise: bb4]; // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
43+
- StorageDead(_6); // scope 0 at $DIR/const_goto_storage.rs:4:51: 4:52
44+
- switchInt(move _5) -> [false: bb5, otherwise: bb4]; // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
5145
- }
5246
-
5347
- bb4: {
54-
- _4 = const true; // scope 3 at $DIR/const_goto_storage.rs:4:55: 4:59
55-
- goto -> bb6; // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
48+
- _4 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:55: 4:59
49+
- goto -> bb6; // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
5650
- }
5751
-
5852
- bb5: {
59-
- _4 = const false; // scope 2 at $DIR/const_goto_storage.rs:4:69: 4:74
60-
- goto -> bb6; // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
53+
- _4 = const false; // scope 0 at $DIR/const_goto_storage.rs:4:69: 4:74
54+
- goto -> bb6; // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
6155
- }
6256
-
6357
- bb6: {
64-
- StorageDead(_5); // scope 2 at $DIR/const_goto_storage.rs:4:75: 4:76
65-
- switchInt(move _4) -> [false: bb8, otherwise: bb7]; // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
58+
- StorageDead(_5); // scope 0 at $DIR/const_goto_storage.rs:4:75: 4:76
59+
- switchInt(move _4) -> [false: bb8, otherwise: bb7]; // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
6660
- }
6761
-
6862
- bb7: {
69-
- _3 = const true; // scope 2 at $DIR/const_goto_storage.rs:5:13: 5:17
63+
- _3 = const true; // scope 0 at $DIR/const_goto_storage.rs:5:13: 5:17
7064
- goto -> bb9; // scope 0 at $DIR/const_goto_storage.rs:4:15: 8:10
7165
- }
7266
-
@@ -82,7 +76,7 @@
8276
- bb10: {
8377
- StorageDead(_4); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
8478
- StorageDead(_3); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
85-
+ StorageDead(_2); // scope 3 at $DIR/const_goto_storage.rs:4:51: 4:52
79+
+ StorageDead(_2); // scope 0 at $DIR/const_goto_storage.rs:4:51: 4:52
8680
_1 = const true; // scope 0 at $DIR/const_goto_storage.rs:10:17: 10:21
8781
- goto -> bb12; // scope 0 at $DIR/const_goto_storage.rs:10:17: 10:21
8882
+ goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:10:17: 10:21
@@ -92,7 +86,7 @@
9286
- StorageDead(_4); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
9387
- StorageDead(_3); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
9488
+ bb2: {
95-
+ StorageDead(_2); // scope 3 at $DIR/const_goto_storage.rs:4:51: 4:52
89+
+ StorageDead(_2); // scope 0 at $DIR/const_goto_storage.rs:4:51: 4:52
9690
_1 = const false; // scope 0 at $DIR/const_goto_storage.rs:12:14: 12:19
9791
- goto -> bb12; // scope 0 at $DIR/const_goto_storage.rs:12:14: 12:19
9892
+ goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:12:14: 12:19

0 commit comments

Comments
 (0)