Skip to content

Commit a2bd433

Browse files
committed
Fix debugger stepping for consts assigned to local vars
1 parent bbd3a5a commit a2bd433

21 files changed

+117
-116
lines changed

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ declare_passes! {
194194
Final
195195
};
196196
mod simplify_comparison_integral : SimplifyComparisonIntegral;
197-
mod single_use_consts : SingleUseConsts;
197+
mod single_use_consts : SingleUseConsts { TempOnly, Full };
198198
mod sroa : ScalarReplacementOfAggregates;
199199
mod strip_debuginfo : StripDebugInfo;
200200
mod unreachable_enum_branching : UnreachableEnumBranching;
@@ -710,7 +710,8 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
710710
&simplify::SimplifyLocals::AfterGVN,
711711
&match_branches::MatchBranchSimplification,
712712
&dataflow_const_prop::DataflowConstProp,
713-
&single_use_consts::SingleUseConsts,
713+
&single_use_consts::SingleUseConsts::TempOnly,
714+
&single_use_consts::SingleUseConsts::Full,
714715
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
715716
&jump_threading::JumpThreading,
716717
&early_otherwise_branch::EarlyOtherwiseBranch,

compiler/rustc_mir_transform/src/single_use_consts.rs

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,24 @@ use rustc_middle::ty::TyCtxt;
1919
///
2020
/// It also removes *never*-used constants, since it had all the information
2121
/// needed to do that too, including updating the debug info.
22-
pub(super) struct SingleUseConsts;
22+
pub(super) enum SingleUseConsts {
23+
TempOnly,
24+
Full,
25+
}
2326

2427
impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
28+
fn name(&self) -> &'static str {
29+
match self {
30+
SingleUseConsts::TempOnly => "SingleUseConsts-temp-only",
31+
SingleUseConsts::Full => "SingleUseConsts-full",
32+
}
33+
}
34+
2535
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
26-
sess.mir_opt_level() > 0
36+
match self {
37+
SingleUseConsts::TempOnly => sess.mir_opt_level() == 1,
38+
SingleUseConsts::Full => sess.mir_opt_level() > 1,
39+
}
2740
}
2841

2942
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
@@ -46,6 +59,11 @@ impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
4659
continue;
4760
};
4861

62+
let local_has_debug_info = finder.locals_in_debug_info.contains(local);
63+
if local_has_debug_info && matches!(self, SingleUseConsts::TempOnly) {
64+
continue;
65+
}
66+
4967
// We're only changing an operand, not the terminator kinds or successors
5068
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
5169
let init_statement_kind = std::mem::replace(
@@ -61,7 +79,7 @@ impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
6179

6280
let mut replacer = LocalReplacer { tcx, local, operand: Some(operand) };
6381

64-
if finder.locals_in_debug_info.contains(local) {
82+
if local_has_debug_info {
6583
for var_debug_info in &mut body.var_debug_info {
6684
replacer.visit_var_debug_info(var_debug_info);
6785
}

tests/mir-opt/const_debuginfo.main.SingleUseConsts.diff renamed to tests/mir-opt/const_debuginfo.main.SingleUseConsts-full.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `main` before SingleUseConsts
2-
+ // MIR for `main` after SingleUseConsts
1+
- // MIR for `main` before SingleUseConsts-full
2+
+ // MIR for `main` after SingleUseConsts-full
33

44
fn main() -> () {
55
let mut _0: ();

tests/mir-opt/const_debuginfo.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ test-mir-pass: SingleUseConsts
1+
//@ test-mir-pass: SingleUseConsts-full
22
//@ compile-flags: -C overflow-checks=no -Zmir-enable-passes=+GVN -Zdump-mir-exclude-alloc-bytes
33

44
#![allow(unused)]
@@ -8,7 +8,7 @@ struct Point {
88
y: u32,
99
}
1010

11-
// EMIT_MIR const_debuginfo.main.SingleUseConsts.diff
11+
// EMIT_MIR const_debuginfo.main.SingleUseConsts-full.diff
1212
fn main() {
1313
// CHECK-LABEL: fn main(
1414
// CHECK: debug x => const 1_u8;

tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-abort.diff renamed to tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts-temp-only.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `assign_const_to_return` before SingleUseConsts
2-
+ // MIR for `assign_const_to_return` after SingleUseConsts
1+
- // MIR for `assign_const_to_return` before SingleUseConsts-temp-only
2+
+ // MIR for `assign_const_to_return` after SingleUseConsts-temp-only
33

44
fn assign_const_to_return() -> bool {
55
let mut _0: bool;

tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts.panic-unwind.diff renamed to tests/mir-opt/single_use_consts.assign_const_to_return.SingleUseConsts-temp-only.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `assign_const_to_return` before SingleUseConsts
2-
+ // MIR for `assign_const_to_return` after SingleUseConsts
1+
- // MIR for `assign_const_to_return` before SingleUseConsts-temp-only
2+
+ // MIR for `assign_const_to_return` after SingleUseConsts-temp-only
33

44
fn assign_const_to_return() -> bool {
55
let mut _0: bool;

tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-unwind.diff renamed to tests/mir-opt/single_use_consts.if_const.SingleUseConsts-temp-only.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `if_const` before SingleUseConsts
2-
+ // MIR for `if_const` after SingleUseConsts
1+
- // MIR for `if_const` before SingleUseConsts-temp-only
2+
+ // MIR for `if_const` after SingleUseConsts-temp-only
33

44
fn if_const() -> i32 {
55
let mut _0: i32;

tests/mir-opt/single_use_consts.if_const.SingleUseConsts.panic-abort.diff renamed to tests/mir-opt/single_use_consts.if_const.SingleUseConsts-temp-only.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `if_const` before SingleUseConsts
2-
+ // MIR for `if_const` after SingleUseConsts
1+
- // MIR for `if_const` before SingleUseConsts-temp-only
2+
+ // MIR for `if_const` after SingleUseConsts-temp-only
33

44
fn if_const() -> i32 {
55
let mut _0: i32;

tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-abort.diff renamed to tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts-temp-only.panic-abort.diff

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
- // MIR for `if_const_debug` before SingleUseConsts
2-
+ // MIR for `if_const_debug` after SingleUseConsts
1+
- // MIR for `if_const_debug` before SingleUseConsts-temp-only
2+
+ // MIR for `if_const_debug` after SingleUseConsts-temp-only
33

44
fn if_const_debug() -> i32 {
55
let mut _0: i32;
66
let _1: bool;
77
let _2: ();
88
let mut _3: bool;
99
scope 1 {
10-
- debug my_bool => _1;
11-
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
10+
debug my_bool => _1;
1211
}
1312

1413
bb0: {
1514
StorageLive(_1);
16-
- _1 = const <T as MyTrait>::ASSOC_BOOL;
17-
+ nop;
15+
_1 = const <T as MyTrait>::ASSOC_BOOL;
1816
StorageLive(_2);
1917
_2 = do_whatever() -> [return: bb1, unwind unreachable];
2018
}
2119

2220
bb1: {
2321
StorageDead(_2);
2422
StorageLive(_3);
25-
- _3 = copy _1;
26-
+ _3 = const <T as MyTrait>::ASSOC_BOOL;
23+
_3 = copy _1;
2724
switchInt(move _3) -> [0: bb3, otherwise: bb2];
2825
}
2926

tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts.panic-unwind.diff renamed to tests/mir-opt/single_use_consts.if_const_debug.SingleUseConsts-temp-only.panic-unwind.diff

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,26 @@
1-
- // MIR for `if_const_debug` before SingleUseConsts
2-
+ // MIR for `if_const_debug` after SingleUseConsts
1+
- // MIR for `if_const_debug` before SingleUseConsts-temp-only
2+
+ // MIR for `if_const_debug` after SingleUseConsts-temp-only
33

44
fn if_const_debug() -> i32 {
55
let mut _0: i32;
66
let _1: bool;
77
let _2: ();
88
let mut _3: bool;
99
scope 1 {
10-
- debug my_bool => _1;
11-
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
10+
debug my_bool => _1;
1211
}
1312

1413
bb0: {
1514
StorageLive(_1);
16-
- _1 = const <T as MyTrait>::ASSOC_BOOL;
17-
+ nop;
15+
_1 = const <T as MyTrait>::ASSOC_BOOL;
1816
StorageLive(_2);
1917
_2 = do_whatever() -> [return: bb1, unwind continue];
2018
}
2119

2220
bb1: {
2321
StorageDead(_2);
2422
StorageLive(_3);
25-
- _3 = copy _1;
26-
+ _3 = const <T as MyTrait>::ASSOC_BOOL;
23+
_3 = copy _1;
2724
switchInt(move _3) -> [0: bb3, otherwise: bb2];
2825
}
2926

tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-abort.diff renamed to tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts-temp-only.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `keep_parameter` before SingleUseConsts
2-
+ // MIR for `keep_parameter` after SingleUseConsts
1+
- // MIR for `keep_parameter` before SingleUseConsts-temp-only
2+
+ // MIR for `keep_parameter` after SingleUseConsts-temp-only
33

44
fn keep_parameter(_1: i32) -> () {
55
debug other => _1;

tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts.panic-unwind.diff renamed to tests/mir-opt/single_use_consts.keep_parameter.SingleUseConsts-temp-only.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `keep_parameter` before SingleUseConsts
2-
+ // MIR for `keep_parameter` after SingleUseConsts
1+
- // MIR for `keep_parameter` before SingleUseConsts-temp-only
2+
+ // MIR for `keep_parameter` after SingleUseConsts-temp-only
33

44
fn keep_parameter(_1: i32) -> () {
55
debug other => _1;

tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-abort.diff renamed to tests/mir-opt/single_use_consts.match_const.SingleUseConsts-temp-only.panic-abort.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_const` before SingleUseConsts
2-
+ // MIR for `match_const` after SingleUseConsts
1+
- // MIR for `match_const` before SingleUseConsts-temp-only
2+
+ // MIR for `match_const` after SingleUseConsts-temp-only
33

44
fn match_const() -> &str {
55
let mut _0: &str;

tests/mir-opt/single_use_consts.match_const.SingleUseConsts.panic-unwind.diff renamed to tests/mir-opt/single_use_consts.match_const.SingleUseConsts-temp-only.panic-unwind.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
- // MIR for `match_const` before SingleUseConsts
2-
+ // MIR for `match_const` after SingleUseConsts
1+
- // MIR for `match_const` before SingleUseConsts-temp-only
2+
+ // MIR for `match_const` after SingleUseConsts-temp-only
33

44
fn match_const() -> &str {
55
let mut _0: &str;

tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-abort.diff renamed to tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts-temp-only.panic-abort.diff

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
- // MIR for `match_const_debug` before SingleUseConsts
2-
+ // MIR for `match_const_debug` after SingleUseConsts
1+
- // MIR for `match_const_debug` before SingleUseConsts-temp-only
2+
+ // MIR for `match_const_debug` after SingleUseConsts-temp-only
33

44
fn match_const_debug() -> &str {
55
let mut _0: &str;
66
let _1: i32;
77
let _2: ();
88
scope 1 {
9-
- debug my_int => _1;
10-
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
9+
debug my_int => _1;
1110
}
1211

1312
bb0: {
1413
StorageLive(_1);
15-
- _1 = const <T as MyTrait>::ASSOC_INT;
16-
+ nop;
14+
_1 = const <T as MyTrait>::ASSOC_INT;
1715
StorageLive(_2);
1816
_2 = do_whatever() -> [return: bb1, unwind unreachable];
1917
}
2018

2119
bb1: {
2220
StorageDead(_2);
23-
- switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
24-
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb4, 42: bb3, otherwise: bb2];
21+
switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
2522
}
2623

2724
bb2: {

tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts.panic-unwind.diff renamed to tests/mir-opt/single_use_consts.match_const_debug.SingleUseConsts-temp-only.panic-unwind.diff

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
1-
- // MIR for `match_const_debug` before SingleUseConsts
2-
+ // MIR for `match_const_debug` after SingleUseConsts
1+
- // MIR for `match_const_debug` before SingleUseConsts-temp-only
2+
+ // MIR for `match_const_debug` after SingleUseConsts-temp-only
33

44
fn match_const_debug() -> &str {
55
let mut _0: &str;
66
let _1: i32;
77
let _2: ();
88
scope 1 {
9-
- debug my_int => _1;
10-
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
9+
debug my_int => _1;
1110
}
1211

1312
bb0: {
1413
StorageLive(_1);
15-
- _1 = const <T as MyTrait>::ASSOC_INT;
16-
+ nop;
14+
_1 = const <T as MyTrait>::ASSOC_INT;
1715
StorageLive(_2);
1816
_2 = do_whatever() -> [return: bb1, unwind continue];
1917
}
2018

2119
bb1: {
2220
StorageDead(_2);
23-
- switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
24-
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb4, 42: bb3, otherwise: bb2];
21+
switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
2522
}
2623

2724
bb2: {
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- // MIR for `never_used_debug` before SingleUseConsts-temp-only
2+
+ // MIR for `never_used_debug` after SingleUseConsts-temp-only
3+
4+
fn never_used_debug() -> () {
5+
let mut _0: ();
6+
let _1: i32;
7+
scope 1 {
8+
debug my_int => _1;
9+
}
10+
11+
bb0: {
12+
StorageLive(_1);
13+
_1 = const <T as MyTrait>::ASSOC_INT;
14+
_0 = const ();
15+
StorageDead(_1);
16+
return;
17+
}
18+
}
19+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
- // MIR for `never_used_debug` before SingleUseConsts-temp-only
2+
+ // MIR for `never_used_debug` after SingleUseConsts-temp-only
3+
4+
fn never_used_debug() -> () {
5+
let mut _0: ();
6+
let _1: i32;
7+
scope 1 {
8+
debug my_int => _1;
9+
}
10+
11+
bb0: {
12+
StorageLive(_1);
13+
_1 = const <T as MyTrait>::ASSOC_INT;
14+
_0 = const ();
15+
StorageDead(_1);
16+
return;
17+
}
18+
}
19+

tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-abort.diff

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/mir-opt/single_use_consts.never_used_debug.SingleUseConsts.panic-unwind.diff

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)