Skip to content

Commit 9ccbbda

Browse files
authored
Rollup merge of #122290 - RalfJung:mir-printing, r=compiler-errors
MIR printing: print the path of uneval'd const Currently it just prints `const _` which makes it impossible to say which constant is being referred to. Also refer to promoteds in a consistent way; previously MIR printing would do ``` promoted[0] in C1: &Option<Cell<i32>> = { // ... } ``` Now that should be ``` const C1::promoted[0]: &Option<Cell<i32>> = { // ... } ``` We don't seem to have a test for that so I tried it by hand, it seems to work: ``` const main::promoted[12]: &[&str; 3] = { let mut _0: &[&str; 3]; let mut _1: [&str; 3]; let mut _2: &str; let mut _3: &str; let mut _4: &str; let mut _5: &str; bb0: { _3 = const "b"; _2 = &(*_3); _5 = const "c"; _4 = &(*_5); _1 = [const "a", move _2, move _4]; _0 = &_1; return; } } ```
2 parents b02f2a0 + 7d99e80 commit 9ccbbda

File tree

79 files changed

+114
-101
lines changed

Some content is hidden

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

79 files changed

+114
-101
lines changed

compiler/rustc_const_eval/src/const_eval/eval_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
6767
trace!(
6868
"eval_body_using_ecx: pushing stack frame for global: {}{}",
6969
with_no_trimmed_paths!(ecx.tcx.def_path_str(cid.instance.def_id())),
70-
cid.promoted.map_or_else(String::new, |p| format!("::promoted[{p:?}]"))
70+
cid.promoted.map_or_else(String::new, |p| format!("::{p:?}"))
7171
);
7272

7373
ecx.push_stack_frame(

compiler/rustc_middle/src/mir/consts.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use rustc_target::abi::{HasDataLayout, Size};
77

88
use crate::mir::interpret::{alloc_range, AllocId, ConstAllocation, ErrorHandled, Scalar};
99
use crate::mir::{pretty_print_const_value, Promoted};
10+
use crate::ty::print::with_no_trimmed_paths;
1011
use crate::ty::GenericArgsRef;
1112
use crate::ty::ScalarInt;
1213
use crate::ty::{self, print::pretty_print_const, Ty, TyCtxt};
@@ -489,9 +490,18 @@ impl<'tcx> Display for Const<'tcx> {
489490
Const::Ty(c) => pretty_print_const(c, fmt, true),
490491
Const::Val(val, ty) => pretty_print_const_value(val, ty, fmt),
491492
// FIXME(valtrees): Correctly print mir constants.
492-
Const::Unevaluated(..) => {
493-
fmt.write_str("_")?;
494-
Ok(())
493+
Const::Unevaluated(c, _ty) => {
494+
ty::tls::with(move |tcx| {
495+
let c = tcx.lift(c).unwrap();
496+
// Matches `GlobalId` printing.
497+
let instance =
498+
with_no_trimmed_paths!(tcx.def_path_str_with_args(c.def, c.args));
499+
write!(fmt, "{instance}")?;
500+
if let Some(promoted) = c.promoted {
501+
write!(fmt, "::{promoted:?}")?;
502+
}
503+
Ok(())
504+
})
495505
}
496506
}
497507
}

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
496496
_ => tcx.is_closure_like(def_id),
497497
};
498498
match (kind, body.source.promoted) {
499-
(_, Some(i)) => write!(w, "{i:?} in ")?,
499+
(_, Some(_)) => write!(w, "const ")?, // promoteds are the closest to consts
500500
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
501501
(DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?,
502502
(DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?,
@@ -509,6 +509,9 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
509509
// see notes on #41697 elsewhere
510510
write!(w, "{}", tcx.def_path_str(def_id))?
511511
}
512+
if let Some(p) = body.source.promoted {
513+
write!(w, "::{p:?}")?;
514+
}
512515

513516
if body.source.promoted.is_none() && is_function {
514517
write!(w, "(")?;

tests/mir-opt/building/custom/consts.consts.built.after.mir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ fn consts() -> () {
1010

1111
bb0: {
1212
_1 = const 5_u8;
13-
_2 = const _;
13+
_2 = const consts::<C>::{constant#0};
1414
_3 = const C;
15-
_4 = const _;
15+
_4 = const D;
1616
_5 = consts::<10>;
1717
return;
1818
}

tests/mir-opt/const_promotion_extern_static.BAR-promoted[0].SimplifyCfg-elaborate-drops.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// MIR for `BAR::promoted[0]` after SimplifyCfg-elaborate-drops
22

3-
promoted[0] in BAR: &[&i32; 1] = {
3+
const BAR::promoted[0]: &[&i32; 1] = {
44
let mut _0: &[&i32; 1];
55
let mut _1: [&i32; 1];
66
let mut _2: &i32;

tests/mir-opt/const_promotion_extern_static.BAR.PromoteTemps.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- _4 = &(*_5);
2121
- _3 = [move _4];
2222
- _2 = &_3;
23-
+ _6 = const _;
23+
+ _6 = const BAR::promoted[0];
2424
+ _2 = &(*_6);
2525
_1 = move _2 as &[&i32] (PointerCoercion(Unsize));
2626
- StorageDead(_4);

tests/mir-opt/const_promotion_extern_static.FOO-promoted[0].SimplifyCfg-elaborate-drops.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// MIR for `FOO::promoted[0]` after SimplifyCfg-elaborate-drops
22

3-
promoted[0] in FOO: &[&i32; 1] = {
3+
const FOO::promoted[0]: &[&i32; 1] = {
44
let mut _0: &[&i32; 1];
55
let mut _1: [&i32; 1];
66
let mut _2: &i32;

tests/mir-opt/const_promotion_extern_static.FOO.PromoteTemps.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
- _4 = &(*_5);
2323
- _3 = [move _4];
2424
- _2 = &_3;
25-
+ _6 = const _;
25+
+ _6 = const FOO::promoted[0];
2626
+ _2 = &(*_6);
2727
_1 = move _2 as &[&i32] (PointerCoercion(Unsize));
2828
- StorageDead(_4);

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.32bit.panic-abort.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
StorageLive(_1);
2626
StorageLive(_2);
2727
StorageLive(_3);
28-
_9 = const _;
28+
_9 = const main::promoted[0];
2929
_3 = &(*_9);
3030
_2 = &raw const (*_3);
3131
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));

tests/mir-opt/const_prop/bad_op_unsafe_oob_for_slices.main.GVN.32bit.panic-unwind.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
StorageLive(_1);
2626
StorageLive(_2);
2727
StorageLive(_3);
28-
_9 = const _;
28+
_9 = const main::promoted[0];
2929
_3 = &(*_9);
3030
_2 = &raw const (*_3);
3131
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));

0 commit comments

Comments
 (0)