Skip to content

Commit

Permalink
Only create scopes for if let
Browse files Browse the repository at this point in the history
  • Loading branch information
xldenis committed Jun 15, 2022
1 parent e39c92e commit a5e27a1
Show file tree
Hide file tree
Showing 55 changed files with 756 additions and 850 deletions.
24 changes: 20 additions & 4 deletions compiler/rustc_mir_build/src/build/expr/into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,25 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
(if_then_scope, then_source_info),
LintLevel::Inherited,
|this| {
let variable_scope =
this.new_source_scope(then_expr.span, LintLevel::Inherited, None);
this.source_scope = variable_scope;
let source_info = if this.is_let(cond) {
let variable_scope = this.new_source_scope(
then_expr.span,
LintLevel::Inherited,
None,
);
this.source_scope = variable_scope;
SourceInfo { span: then_expr.span, scope: variable_scope }
} else {
this.source_info(then_expr.span)
};
let (then_block, else_block) =
this.in_if_then_scope(condition_scope, |this| {
let then_blk = unpack!(this.then_else_break(
block,
&this.thir[cond],
Some(condition_scope),
condition_scope,
SourceInfo { span: then_expr.span, scope: variable_scope }
source_info
));

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

block_and
}

fn is_let(&self, expr: ExprId) -> bool {
match self.thir[expr].kind {
ExprKind::Let { .. } => true,
ExprKind::Scope { value, .. } => self.is_let(value),
_ => false,
}
}
}
18 changes: 8 additions & 10 deletions src/test/mir-opt/bool_compare.opt1.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:2:21: 2:24
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:3:8: 3:17
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:3:8: 3:9
scope 1 {
}

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

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

Expand Down
18 changes: 8 additions & 10 deletions src/test/mir-opt/bool_compare.opt2.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:7:21: 7:24
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:8:8: 8:17
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:8:16: 8:17
scope 1 {
}

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

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

Expand Down
18 changes: 8 additions & 10 deletions src/test/mir-opt/bool_compare.opt3.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:12:21: 12:24
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:13:8: 13:18
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:13:8: 13:9
scope 1 {
}

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

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

Expand Down
18 changes: 8 additions & 10 deletions src/test/mir-opt/bool_compare.opt4.InstCombine.diff
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,19 @@
let mut _0: u32; // return place in scope 0 at $DIR/bool_compare.rs:17:21: 17:24
let mut _2: bool; // in scope 0 at $DIR/bool_compare.rs:18:8: 18:18
let mut _3: bool; // in scope 0 at $DIR/bool_compare.rs:18:17: 18:18
scope 1 {
}

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

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

Expand Down
24 changes: 11 additions & 13 deletions src/test/mir-opt/const_goto.issue_77355_opt.ConstGoto.diff
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,33 @@
- let mut _2: bool; // in scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- let mut _3: isize; // in scope 0 at $DIR/const_goto.rs:12:22: 12:28
+ let mut _2: isize; // in scope 0 at $DIR/const_goto.rs:12:22: 12:28
scope 1 {
}

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

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

bb2: {
- _2 = const true; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- goto -> bb3; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- _2 = const true; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- goto -> bb3; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- }
-
- bb3: {
- switchInt(move _2) -> [false: bb5, otherwise: bb4]; // scope 1 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- switchInt(move _2) -> [false: bb5, otherwise: bb4]; // scope 0 at $SRC_DIR/core/src/macros/mod.rs:LL:COL
- }
-
- bb4: {
_0 = const 23_u64; // scope 1 at $DIR/const_goto.rs:12:41: 12:43
_0 = const 23_u64; // scope 0 at $DIR/const_goto.rs:12:41: 12:43
- goto -> bb6; // scope 0 at $DIR/const_goto.rs:12:5: 12:57
+ goto -> bb3; // scope 0 at $DIR/const_goto.rs:12:5: 12:57
}
Expand Down
52 changes: 23 additions & 29 deletions src/test/mir-opt/const_goto_storage.match_nested_if.ConstGoto.diff
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,54 @@
scope 1 {
debug val => _1; // in scope 1 at $DIR/const_goto_storage.rs:3:9: 3:12
}
scope 2 {
scope 3 {
scope 4 {
}
}
}

bb0: {
StorageLive(_1); // scope 0 at $DIR/const_goto_storage.rs:3:9: 3:12
- StorageLive(_2); // scope 0 at $DIR/const_goto_storage.rs:3:21: 3:23
- nop; // scope 0 at $DIR/const_goto_storage.rs:3:21: 3:23
- StorageLive(_3); // scope 0 at $DIR/const_goto_storage.rs:4:15: 8:10
- StorageLive(_4); // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
- StorageLive(_5); // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
- StorageLive(_6); // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
- _6 = const true; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
- switchInt(move _6) -> [false: bb2, otherwise: bb1]; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
+ StorageLive(_2); // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
+ _2 = const true; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
+ switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 4 at $DIR/const_goto_storage.rs:4:24: 4:28
- StorageLive(_4); // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
- StorageLive(_5); // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
- StorageLive(_6); // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
- _6 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
- switchInt(move _6) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
+ StorageLive(_2); // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
+ _2 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
+ switchInt(move _2) -> [false: bb2, otherwise: bb1]; // scope 0 at $DIR/const_goto_storage.rs:4:24: 4:28
}

bb1: {
- _5 = const true; // scope 4 at $DIR/const_goto_storage.rs:4:31: 4:35
- goto -> bb3; // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
- _5 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:31: 4:35
- goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
- }
-
- bb2: {
- _5 = const false; // scope 3 at $DIR/const_goto_storage.rs:4:45: 4:50
- goto -> bb3; // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
- _5 = const false; // scope 0 at $DIR/const_goto_storage.rs:4:45: 4:50
- goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
- }
-
- bb3: {
- StorageDead(_6); // scope 3 at $DIR/const_goto_storage.rs:4:51: 4:52
- switchInt(move _5) -> [false: bb5, otherwise: bb4]; // scope 3 at $DIR/const_goto_storage.rs:4:21: 4:52
- StorageDead(_6); // scope 0 at $DIR/const_goto_storage.rs:4:51: 4:52
- switchInt(move _5) -> [false: bb5, otherwise: bb4]; // scope 0 at $DIR/const_goto_storage.rs:4:21: 4:52
- }
-
- bb4: {
- _4 = const true; // scope 3 at $DIR/const_goto_storage.rs:4:55: 4:59
- goto -> bb6; // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
- _4 = const true; // scope 0 at $DIR/const_goto_storage.rs:4:55: 4:59
- goto -> bb6; // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
- }
-
- bb5: {
- _4 = const false; // scope 2 at $DIR/const_goto_storage.rs:4:69: 4:74
- goto -> bb6; // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
- _4 = const false; // scope 0 at $DIR/const_goto_storage.rs:4:69: 4:74
- goto -> bb6; // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
- }
-
- bb6: {
- StorageDead(_5); // scope 2 at $DIR/const_goto_storage.rs:4:75: 4:76
- switchInt(move _4) -> [false: bb8, otherwise: bb7]; // scope 2 at $DIR/const_goto_storage.rs:4:18: 4:76
- StorageDead(_5); // scope 0 at $DIR/const_goto_storage.rs:4:75: 4:76
- switchInt(move _4) -> [false: bb8, otherwise: bb7]; // scope 0 at $DIR/const_goto_storage.rs:4:18: 4:76
- }
-
- bb7: {
- _3 = const true; // scope 2 at $DIR/const_goto_storage.rs:5:13: 5:17
- _3 = const true; // scope 0 at $DIR/const_goto_storage.rs:5:13: 5:17
- goto -> bb9; // scope 0 at $DIR/const_goto_storage.rs:4:15: 8:10
- }
-
Expand All @@ -82,7 +76,7 @@
- bb10: {
- StorageDead(_4); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
- StorageDead(_3); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
+ StorageDead(_2); // scope 3 at $DIR/const_goto_storage.rs:4:51: 4:52
+ StorageDead(_2); // scope 0 at $DIR/const_goto_storage.rs:4:51: 4:52
_1 = const true; // scope 0 at $DIR/const_goto_storage.rs:10:17: 10:21
- goto -> bb12; // scope 0 at $DIR/const_goto_storage.rs:10:17: 10:21
+ goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:10:17: 10:21
Expand All @@ -92,7 +86,7 @@
- StorageDead(_4); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
- StorageDead(_3); // scope 0 at $DIR/const_goto_storage.rs:8:9: 8:10
+ bb2: {
+ StorageDead(_2); // scope 3 at $DIR/const_goto_storage.rs:4:51: 4:52
+ StorageDead(_2); // scope 0 at $DIR/const_goto_storage.rs:4:51: 4:52
_1 = const false; // scope 0 at $DIR/const_goto_storage.rs:12:14: 12:19
- goto -> bb12; // scope 0 at $DIR/const_goto_storage.rs:12:14: 12:19
+ goto -> bb3; // scope 0 at $DIR/const_goto_storage.rs:12:14: 12:19
Expand Down
Loading

0 comments on commit a5e27a1

Please sign in to comment.