Skip to content

Commit ab95039

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

5 files changed

+19
-48
lines changed

compiler/rustc_mir_transform/src/single_use_consts.rs

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
4646
continue;
4747
};
4848

49+
if finder.locals_in_debug_info.contains(local) {
50+
continue;
51+
}
52+
4953
// We're only changing an operand, not the terminator kinds or successors
5054
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
5155
let init_statement_kind = std::mem::replace(
@@ -61,12 +65,6 @@ impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
6165

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

64-
if finder.locals_in_debug_info.contains(local) {
65-
for var_debug_info in &mut body.var_debug_info {
66-
replacer.visit_var_debug_info(var_debug_info);
67-
}
68-
}
69-
7068
let Some(use_loc) = locations.use_loc else { continue };
7169

7270
let use_block = &mut basic_blocks[use_loc.block];
@@ -185,21 +183,4 @@ impl<'tcx> MutVisitor<'tcx> for LocalReplacer<'tcx> {
185183
});
186184
}
187185
}
188-
189-
fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) {
190-
if let VarDebugInfoContents::Place(place) = &var_debug_info.value
191-
&& let Some(local) = place.as_local()
192-
&& local == self.local
193-
{
194-
let const_op = *self
195-
.operand
196-
.as_ref()
197-
.unwrap_or_else(|| {
198-
bug!("the operand was already stolen");
199-
})
200-
.constant()
201-
.unwrap();
202-
var_debug_info.value = VarDebugInfoContents::Const(const_op);
203-
}
204-
}
205186
}

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

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,20 @@
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.match_const_debug.SingleUseConsts.panic-unwind.diff

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,19 @@
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: {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
let mut _0: ();
66
let _1: i32;
77
scope 1 {
8-
- debug my_int => _1;
9-
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
8+
debug my_int => _1;
109
}
1110

1211
bb0: {
1312
StorageLive(_1);
14-
- _1 = const <T as MyTrait>::ASSOC_INT;
15-
+ nop;
13+
_1 = const <T as MyTrait>::ASSOC_INT;
1614
_0 = const ();
1715
StorageDead(_1);
1816
return;

tests/mir-opt/single_use_consts.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ fn match_const<T: MyTrait>() -> &'static str {
2828
// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts.diff
2929
fn if_const_debug<T: MyTrait>() -> i32 {
3030
// CHECK-LABEL: fn if_const_debug(
31-
// CHECK: my_bool => const <T as MyTrait>::ASSOC_BOOL;
32-
// FIXME: `if` forces a temporary (unlike `match`), so the const isn't direct
33-
// CHECK: _3 = const <T as MyTrait>::ASSOC_BOOL;
31+
// Note: we must not reorder assignments for vars with debuginfo
32+
// CHECK: my_bool => _1;
33+
// CHECK: _3 = copy _1;
3434
// CHECK: switchInt(move _3)
3535
let my_bool = T::ASSOC_BOOL;
3636
do_whatever();
@@ -40,8 +40,8 @@ fn if_const_debug<T: MyTrait>() -> i32 {
4040
// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts.diff
4141
fn match_const_debug<T: MyTrait>() -> &'static str {
4242
// CHECK-LABEL: fn match_const_debug(
43-
// CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
44-
// CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
43+
// CHECK: my_int => _1;
44+
// CHECK: switchInt(copy _1)
4545
let my_int = T::ASSOC_INT;
4646
do_whatever();
4747
match my_int {
@@ -55,10 +55,8 @@ fn match_const_debug<T: MyTrait>() -> &'static str {
5555
#[allow(unused_variables)]
5656
fn never_used_debug<T: MyTrait>() {
5757
// CHECK-LABEL: fn never_used_debug(
58-
// CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
59-
// CHECK-NOT: ASSOC_INT
60-
// CHECK: nop
61-
// CHECK-NOT: ASSOC_INT
58+
// CHECK: my_int => _1;
59+
// CHECK: _1 = const <T as MyTrait>::ASSOC_INT
6260
let my_int = T::ASSOC_INT;
6361
}
6462

0 commit comments

Comments
 (0)