Skip to content

Commit

Permalink
MIR printing: print the path of uneval'd const; refer to promoteds in…
Browse files Browse the repository at this point in the history
… a consistent way
  • Loading branch information
RalfJung committed Mar 10, 2024
1 parent 3521a2f commit 7d99e80
Show file tree
Hide file tree
Showing 79 changed files with 114 additions and 101 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_const_eval/src/const_eval/eval_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn eval_body_using_ecx<'mir, 'tcx>(
trace!(
"eval_body_using_ecx: pushing stack frame for global: {}{}",
with_no_trimmed_paths!(ecx.tcx.def_path_str(cid.instance.def_id())),
cid.promoted.map_or_else(String::new, |p| format!("::promoted[{p:?}]"))
cid.promoted.map_or_else(String::new, |p| format!("::{p:?}"))
);

ecx.push_stack_frame(
Expand Down
16 changes: 13 additions & 3 deletions compiler/rustc_middle/src/mir/consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use rustc_target::abi::{HasDataLayout, Size};

use crate::mir::interpret::{alloc_range, AllocId, ConstAllocation, ErrorHandled, Scalar};
use crate::mir::{pretty_print_const_value, Promoted};
use crate::ty::print::with_no_trimmed_paths;
use crate::ty::GenericArgsRef;
use crate::ty::ScalarInt;
use crate::ty::{self, print::pretty_print_const, Ty, TyCtxt};
Expand Down Expand Up @@ -489,9 +490,18 @@ impl<'tcx> Display for Const<'tcx> {
Const::Ty(c) => pretty_print_const(c, fmt, true),
Const::Val(val, ty) => pretty_print_const_value(val, ty, fmt),
// FIXME(valtrees): Correctly print mir constants.
Const::Unevaluated(..) => {
fmt.write_str("_")?;
Ok(())
Const::Unevaluated(c, _ty) => {
ty::tls::with(move |tcx| {
let c = tcx.lift(c).unwrap();
// Matches `GlobalId` printing.
let instance =
with_no_trimmed_paths!(tcx.def_path_str_with_args(c.def, c.args));
write!(fmt, "{instance}")?;
if let Some(promoted) = c.promoted {
write!(fmt, "::{promoted:?}")?;
}
Ok(())
})
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion compiler/rustc_middle/src/mir/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
_ => tcx.is_closure_like(def_id),
};
match (kind, body.source.promoted) {
(_, Some(i)) => write!(w, "{i:?} in ")?,
(_, Some(_)) => write!(w, "const ")?, // promoteds are the closest to consts
(DefKind::Const | DefKind::AssocConst, _) => write!(w, "const ")?,
(DefKind::Static(hir::Mutability::Not), _) => write!(w, "static ")?,
(DefKind::Static(hir::Mutability::Mut), _) => write!(w, "static mut ")?,
Expand All @@ -509,6 +509,9 @@ fn write_mir_sig(tcx: TyCtxt<'_>, body: &Body<'_>, w: &mut dyn io::Write) -> io:
// see notes on #41697 elsewhere
write!(w, "{}", tcx.def_path_str(def_id))?
}
if let Some(p) = body.source.promoted {
write!(w, "::{p:?}")?;
}

if body.source.promoted.is_none() && is_function {
write!(w, "(")?;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/building/custom/consts.consts.built.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ fn consts() -> () {

bb0: {
_1 = const 5_u8;
_2 = const _;
_2 = const consts::<C>::{constant#0};
_3 = const C;
_4 = const _;
_4 = const D;
_5 = consts::<10>;
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `BAR::promoted[0]` after SimplifyCfg-elaborate-drops

promoted[0] in BAR: &[&i32; 1] = {
const BAR::promoted[0]: &[&i32; 1] = {
let mut _0: &[&i32; 1];
let mut _1: [&i32; 1];
let mut _2: &i32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
- _4 = &(*_5);
- _3 = [move _4];
- _2 = &_3;
+ _6 = const _;
+ _6 = const BAR::promoted[0];
+ _2 = &(*_6);
_1 = move _2 as &[&i32] (PointerCoercion(Unsize));
- StorageDead(_4);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// MIR for `FOO::promoted[0]` after SimplifyCfg-elaborate-drops

promoted[0] in FOO: &[&i32; 1] = {
const FOO::promoted[0]: &[&i32; 1] = {
let mut _0: &[&i32; 1];
let mut _1: [&i32; 1];
let mut _2: &i32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
- _4 = &(*_5);
- _3 = [move _4];
- _2 = &_3;
+ _6 = const _;
+ _6 = const FOO::promoted[0];
+ _2 = &(*_6);
_1 = move _2 as &[&i32] (PointerCoercion(Unsize));
- StorageDead(_4);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_9 = const _;
_9 = const main::promoted[0];
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_9 = const _;
_9 = const main::promoted[0];
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_9 = const _;
_9 = const main::promoted[0];
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
StorageLive(_1);
StorageLive(_2);
StorageLive(_3);
_9 = const _;
_9 = const main::promoted[0];
_3 = &(*_9);
_2 = &raw const (*_3);
_1 = move _2 as *const [i32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bb0: {
StorageLive(_1);
_1 = const _;
_1 = const <bool as NeedsDrop>::NEEDS;
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

bb0: {
StorageLive(_1);
_1 = const _;
_1 = const <bool as NeedsDrop>::NEEDS;
- switchInt(move _1) -> [0: bb2, otherwise: bb1];
+ switchInt(const false) -> [0: bb2, otherwise: bb1];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl<T> SizeOfConst<T> {
fn size_of<T>() -> usize {
// CHECK-LABEL: fn size_of(
// CHECK: _1 = const 0_usize;
// CHECK-NEXT: _1 = const _;
// CHECK-NEXT: _1 = const SizeOfConst::<T>::SIZE;
// CHECK-NEXT: _0 = _1;
let mut a = 0;
a = SizeOfConst::<T>::SIZE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
bb0: {
StorageLive(_1);
_1 = const 0_usize;
_1 = const _;
_1 = const SizeOfConst::<T>::SIZE;
_0 = _1;
StorageDead(_1);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
+ nop;
StorageLive(_2);
StorageLive(_3);
_3 = const _;
_3 = const main::FOO;
_2 = &raw const (*_3);
_1 = move _2 as usize (PointerExposeAddress);
StorageDead(_2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
+ nop;
StorageLive(_2);
StorageLive(_3);
_3 = const _;
_3 = const main::FOO;
_2 = &raw const (*_3);
_1 = move _2 as usize (PointerExposeAddress);
StorageDead(_2);
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/pointer_expose_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ fn read(_: usize) { }
// EMIT_MIR pointer_expose_address.main.GVN.diff
fn main() {
// CHECK-LABEL: fn main(
// CHECK: [[ptr:_.*]] = const _;
// CHECK: [[ptr:_.*]] = const main::FOO;
// CHECK: [[ref:_.*]] = &raw const (*[[ptr]]);
// CHECK: [[x:_.*]] = move [[ref]] as usize (PointerExposeAddress);
// CHECK: = read([[x]])
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/ref_deref.main.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
bb0: {
StorageLive(_1);
StorageLive(_2);
_4 = const _;
_4 = const main::promoted[0];
_2 = &(*_4);
- _1 = (*_2);
+ _1 = const 4_i32;
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/ref_deref_project.main.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
bb0: {
StorageLive(_1);
StorageLive(_2);
_4 = const _;
_4 = const main::promoted[0];
_2 = &((*_4).1: i32);
- _1 = (*_2);
+ _1 = const 5_i32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_9 = const main::promoted[0];
- _4 = _9;
- _3 = _4;
- _2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_9 = const main::promoted[0];
- _4 = _9;
- _3 = _4;
- _2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_9 = const main::promoted[0];
- _4 = _9;
- _3 = _4;
- _2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_9 = const _;
_9 = const main::promoted[0];
- _4 = _9;
- _3 = _4;
- _2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

bb0: {
- _0 = const _ as char (Transmute);
- _0 = const core::num::<impl i32>::MAX as char (Transmute);
+ _0 = const {transmute(0x7fffffff): char};
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
}

bb0: {
- _0 = const _ as char (Transmute);
- _0 = const core::num::<impl i32>::MAX as char (Transmute);
+ _0 = const {transmute(0x7fffffff): char};
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
StorageDead(_5);
StorageDead(_4);
StorageLive(_7);
_7 = const _;
_7 = const core::num::<impl i32>::MAX;
StorageLive(_8);
StorageLive(_9);
- _9 = _7;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
StorageDead(_5);
StorageDead(_4);
StorageLive(_7);
_7 = const _;
_7 = const core::num::<impl i32>::MAX;
StorageLive(_8);
StorageLive(_9);
- _9 = _7;
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/dataflow-const-prop/checked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
// CHECK: [[c]] = const 3_i32;
let c = a + b;

// CHECK: [[d]] = const _;
// CHECK: [[d]] = const core::num::<impl i32>::MAX;
let d = i32::MAX;

// CHECK: assert(!const true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

bb0: {
StorageLive(_1);
_1 = const _;
_1 = const constant::C;
StorageLive(_2);
- _3 = discriminant(_1);
- switchInt(move _3) -> [0: bb3, 1: bb2, otherwise: bb1];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

bb0: {
StorageLive(_1);
_1 = const _;
_1 = const constant::C;
StorageLive(_2);
- _3 = discriminant(_1);
- switchInt(move _3) -> [0: bb3, 1: bb2, otherwise: bb1];
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/dataflow-const-prop/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ fn constant() {
// CHECK: debug x => [[x:_.*]];
const C: E = E::V1(0);

// CHECK: [[e]] = const _;
// CHECK: [[e]] = const constant::C;
let e = C;
// CHECK: switchInt(const 0_isize) -> [0: [[target_bb:bb.*]], 1: bb2, otherwise: bb1];
// CHECK: [[target_bb]]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_14 = const _;
_14 = const main::promoted[0];
_4 = _14;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand All @@ -52,7 +52,7 @@
StorageDead(_2);
StorageLive(_9);
StorageLive(_10);
_10 = const _;
_10 = const main::SLICE;
StorageLive(_11);
_11 = const 1_usize;
- _12 = Len((*_10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_14 = const _;
_14 = const main::promoted[0];
_4 = _14;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand All @@ -52,7 +52,7 @@
StorageDead(_2);
StorageLive(_9);
StorageLive(_10);
_10 = const _;
_10 = const main::SLICE;
StorageLive(_11);
_11 = const 1_usize;
- _12 = Len((*_10));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
StorageLive(_2);
StorageLive(_3);
StorageLive(_4);
_14 = const _;
_14 = const main::promoted[0];
_4 = _14;
_3 = _4;
_2 = move _3 as &[u32] (PointerCoercion(Unsize));
Expand All @@ -52,7 +52,7 @@
StorageDead(_2);
StorageLive(_9);
StorageLive(_10);
_10 = const _;
_10 = const main::SLICE;
StorageLive(_11);
_11 = const 1_usize;
- _12 = Len((*_10));
Expand Down
Loading

0 comments on commit 7d99e80

Please sign in to comment.