Skip to content

Commit 33424a4

Browse files
Rollup merge of rust-lang#96581 - RalfJung:debug-size-align, r=oli-obk
make Size and Align debug-printing a bit more compact In particular in `{:#?}`-mode, these take up a lot of space, so I think this is the better alternative (even though it is a bit longer in `{:?}` mode, I think it is still more readable). We could make it even smaller by deviating further from what the actual code looks like, e.g. via something like `Size(4 bytes)`. Not sure what people would think about that? Cc `@oli-obk`
2 parents 914f947 + d455752 commit 33424a4

26 files changed

+244
-555
lines changed

compiler/rustc_middle/src/mir/pretty.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,12 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
448448
self.push(&format!("+ user_ty: {:?}", user_ty));
449449
}
450450

451+
let fmt_val = |val: &ConstValue<'tcx>| match val {
452+
ConstValue::Scalar(s) => format!("Scalar({:?})", s),
453+
ConstValue::Slice { .. } => format!("Slice(..)"),
454+
ConstValue::ByRef { .. } => format!("ByRef(..)"),
455+
};
456+
451457
let val = match literal {
452458
ConstantKind::Ty(ct) => match ct.val() {
453459
ty::ConstKind::Param(p) => format!("Param({})", p),
@@ -457,7 +463,7 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
457463
uv.substs,
458464
uv.promoted,
459465
),
460-
ty::ConstKind::Value(val) => format!("Value({:?})", val),
466+
ty::ConstKind::Value(val) => format!("Value({})", fmt_val(&val)),
461467
ty::ConstKind::Error(_) => "Error".to_string(),
462468
// These variants shouldn't exist in the MIR.
463469
ty::ConstKind::Placeholder(_)
@@ -467,7 +473,7 @@ impl<'tcx> Visitor<'tcx> for ExtraComments<'tcx> {
467473
// To keep the diffs small, we render this like we render `ty::Const::Value`.
468474
//
469475
// This changes once `ty::Const::Value` is represented using valtrees.
470-
ConstantKind::Val(val, _) => format!("Value({:?})", val),
476+
ConstantKind::Val(val, _) => format!("Value({})", fmt_val(&val)),
471477
};
472478

473479
self.push(&format!("+ literal: Const {{ ty: {}, val: {} }}", literal.ty(), val));

compiler/rustc_target/src/abi/mod.rs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,19 @@ impl ToJson for Endian {
276276
}
277277

278278
/// Size of a type in bytes.
279-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
279+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
280280
#[derive(HashStable_Generic)]
281281
pub struct Size {
282282
raw: u64,
283283
}
284284

285+
// This is debug-printed a lot in larger structs, don't waste too much space there
286+
impl fmt::Debug for Size {
287+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
288+
write!(f, "Size({} bytes)", self.bytes())
289+
}
290+
}
291+
285292
impl Size {
286293
pub const ZERO: Size = Size { raw: 0 };
287294

@@ -485,12 +492,19 @@ impl Step for Size {
485492
}
486493

487494
/// Alignment of a type in bytes (always a power of two).
488-
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Encodable, Decodable)]
495+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Encodable, Decodable)]
489496
#[derive(HashStable_Generic)]
490497
pub struct Align {
491498
pow2: u8,
492499
}
493500

501+
// This is debug-printed a lot in larger structs, don't waste too much space there
502+
impl fmt::Debug for Align {
503+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
504+
write!(f, "Align({} bytes)", self.bytes())
505+
}
506+
}
507+
494508
impl Align {
495509
pub const ONE: Align = Align { pow2: 0 };
496510

src/test/mir-opt/const_debuginfo.main.ConstDebugInfo.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
_9 = const "hello, world!"; // scope 4 at $DIR/const_debuginfo.rs:14:13: 14:28
7878
// mir::Constant
7979
// + span: $DIR/const_debuginfo.rs:14:13: 14:28
80-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [104, 101, 108, 108, 111, 44, 32, 119, 111, 114, 108, 100, 33], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8191], len: Size { raw: 13 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 13 }) }
80+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
8181
StorageLive(_10); // scope 5 at $DIR/const_debuginfo.rs:16:9: 16:10
8282
Deinit(_10); // scope 5 at $DIR/const_debuginfo.rs:16:13: 16:34
8383
(_10.0: bool) = const true; // scope 5 at $DIR/const_debuginfo.rs:16:13: 16:34

src/test/mir-opt/const_prop/control_flow_simplification.hello.ConstProp.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
// + literal: Const { ty: fn(&str) -> ! {begin_panic::<&str>}, val: Value(Scalar(<ZST>)) }
2323
// mir::Constant
2424
// + span: $SRC_DIR/std/src/panic.rs:LL:COL
25-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 }) }
25+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
2626
}
2727

2828
bb2: {

src/test/mir-opt/inline/inline_diverging.g.Inline.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
+ // + literal: Const { ty: fn(&str) -> ! {begin_panic::<&str>}, val: Value(Scalar(<ZST>)) }
4444
+ // mir::Constant
4545
+ // + span: $SRC_DIR/std/src/panic.rs:LL:COL
46-
+ // + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 }) }
46+
+ // + literal: Const { ty: &str, val: Value(Slice(..)) }
4747
}
4848
}
4949

src/test/mir-opt/inline/inline_into_box_place.main.Inline.32bit.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
- bb2: {
4747
+ // + span: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
4848
+ // + user_ty: UserType(0)
49-
+ // + literal: Const { ty: alloc::raw_vec::RawVec<u32>, val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [255], len: Size { raw: 8 } }, align: Align { pow2: 2 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
49+
+ // + literal: Const { ty: alloc::raw_vec::RawVec<u32>, val: Value(ByRef(..)) }
5050
+ Deinit((*_7)); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
5151
+ ((*_7).0: alloc::raw_vec::RawVec<u32>) = move _8; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
5252
+ ((*_7).1: usize) = const 0_usize; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

src/test/mir-opt/inline/inline_into_box_place.main.Inline.64bit.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646
- bb2: {
4747
+ // + span: $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
4848
+ // + user_ty: UserType(0)
49-
+ // + literal: Const { ty: alloc::raw_vec::RawVec<u32>, val: Value(ByRef { alloc: Allocation { bytes: [4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [65535], len: Size { raw: 16 } }, align: Align { pow2: 3 }, mutability: Not, extra: () }, offset: Size { raw: 0 } }) }
49+
+ // + literal: Const { ty: alloc::raw_vec::RawVec<u32>, val: Value(ByRef(..)) }
5050
+ Deinit((*_7)); // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
5151
+ ((*_7).0: alloc::raw_vec::RawVec<u32>) = move _8; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL
5252
+ ((*_7).1: usize) = const 0_usize; // scope 3 at $SRC_DIR/alloc/src/vec/mod.rs:LL:COL

src/test/mir-opt/issue_76432.test.SimplifyComparisonIntegral.diff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@
7373
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
7474
// mir::Constant
7575
// + span: $SRC_DIR/core/src/panic.rs:LL:COL
76-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [105, 110, 116, 101, 114, 110, 97, 108, 32, 101, 114, 114, 111, 114, 58, 32, 101, 110, 116, 101, 114, 101, 100, 32, 117, 110, 114, 101, 97, 99, 104, 97, 98, 108, 101, 32, 99, 111, 100, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [1099511627775], len: Size { raw: 40 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 40 }) }
76+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
7777
}
7878

7979
bb2: {

src/test/mir-opt/issues/issue_59352.num_to_digit.PreCodegen.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn num_to_digit(_1: char) -> u32 {
9292
// + literal: Const { ty: fn(&'static str) -> ! {core::panicking::panic}, val: Value(Scalar(<ZST>)) }
9393
// mir::Constant
9494
// + span: $SRC_DIR/core/src/option.rs:LL:COL
95-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [99, 97, 108, 108, 101, 100, 32, 96, 79, 112, 116, 105, 111, 110, 58, 58, 117, 110, 119, 114, 97, 112, 40, 41, 96, 32, 111, 110, 32, 97, 32, 96, 78, 111, 110, 101, 96, 32, 118, 97, 108, 117, 101], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [8796093022207], len: Size { raw: 43 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 43 }) }
95+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
9696
}
9797

9898
bb7: {

src/test/mir-opt/no_drop_for_inactive_variant.unwrap.SimplifyCfg-elaborate-drops.after.mir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ fn unwrap(_1: Option<T>) -> T {
2626
// + literal: Const { ty: fn(&str) -> ! {begin_panic::<&str>}, val: Value(Scalar(<ZST>)) }
2727
// mir::Constant
2828
// + span: $SRC_DIR/std/src/panic.rs:LL:COL
29-
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 }) }
29+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
3030
}
3131

3232
bb2: {

0 commit comments

Comments
 (0)