Skip to content

Commit 71cab64

Browse files
BoxyUwURalfJung
authored andcommitted
special case TyAndLayout debug impl
1 parent 06a76ab commit 71cab64

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

compiler/rustc_target/src/abi/call/mod.rs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::abi::{self, Abi, Align, FieldsShape, Size};
22
use crate::abi::{HasDataLayout, TyAbiInterface, TyAndLayout};
33
use crate::spec::{self, HasTargetSpec};
44
use rustc_span::Symbol;
5+
use std::fmt;
56
use std::str::FromStr;
67

78
mod aarch64;
@@ -515,12 +516,20 @@ impl<'a, Ty> TyAndLayout<'a, Ty> {
515516

516517
/// Information about how to pass an argument to,
517518
/// or return a value from, a function, under some ABI.
518-
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
519+
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic)]
519520
pub struct ArgAbi<'a, Ty> {
520521
pub layout: TyAndLayout<'a, Ty>,
521522
pub mode: PassMode,
522523
}
523524

525+
// Needs to be a custom impl because of the bounds on the `TyAndLayout` debug impl.
526+
impl<'a, Ty: fmt::Display> fmt::Debug for ArgAbi<'a, Ty> {
527+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
528+
let ArgAbi { layout, mode } = self;
529+
f.debug_struct("ArgAbi").field("layout", layout).field("mode", mode).finish()
530+
}
531+
}
532+
524533
impl<'a, Ty> ArgAbi<'a, Ty> {
525534
/// This defines the "default ABI" for that type, that is then later adjusted in `fn_abi_adjust_for_abi`.
526535
pub fn new(
@@ -694,7 +703,7 @@ impl RiscvInterruptKind {
694703
///
695704
/// I will do my best to describe this structure, but these
696705
/// comments are reverse-engineered and may be inaccurate. -NDM
697-
#[derive(Clone, PartialEq, Eq, Hash, Debug, HashStable_Generic)]
706+
#[derive(Clone, PartialEq, Eq, Hash, HashStable_Generic)]
698707
pub struct FnAbi<'a, Ty> {
699708
/// The LLVM types of each argument.
700709
pub args: Box<[ArgAbi<'a, Ty>]>,
@@ -715,6 +724,21 @@ pub struct FnAbi<'a, Ty> {
715724
pub can_unwind: bool,
716725
}
717726

727+
// Needs to be a custom impl because of the bounds on the `TyAndLayout` debug impl.
728+
impl<'a, Ty: fmt::Display> fmt::Debug for FnAbi<'a, Ty> {
729+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
730+
let FnAbi { args, ret, c_variadic, fixed_count, conv, can_unwind } = self;
731+
f.debug_struct("FnAbi")
732+
.field("args", args)
733+
.field("ret", ret)
734+
.field("c_variadic", c_variadic)
735+
.field("fixed_count", fixed_count)
736+
.field("conv", conv)
737+
.field("can_unwind", can_unwind)
738+
.finish()
739+
}
740+
}
741+
718742
/// Error produced by attempting to adjust a `FnAbi`, for a "foreign" ABI.
719743
#[derive(Copy, Clone, Debug, HashStable_Generic)]
720744
pub enum AdjustForForeignAbiError {

compiler/rustc_target/src/abi/mod.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub use Primitive::*;
33

44
use crate::json::{Json, ToJson};
55

6+
use std::fmt;
67
use std::ops::Deref;
78

89
use rustc_macros::HashStable_Generic;
@@ -24,12 +25,22 @@ impl ToJson for Endian {
2425
/// to that obtained from `layout_of(ty)`, as we need to produce
2526
/// layouts for which Rust types do not exist, such as enum variants
2627
/// or synthetic fields of enums (i.e., discriminants) and fat pointers.
27-
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, HashStable_Generic)]
28+
#[derive(Copy, Clone, PartialEq, Eq, Hash, HashStable_Generic)]
2829
pub struct TyAndLayout<'a, Ty> {
2930
pub ty: Ty,
3031
pub layout: Layout<'a>,
3132
}
3233

34+
impl<'a, Ty: fmt::Display> fmt::Debug for TyAndLayout<'a, Ty> {
35+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
36+
// Print the type in a readable way, not its debug representation.
37+
f.debug_struct("TyAndLayout")
38+
.field("ty", &format_args!("{}", self.ty))
39+
.field("layout", &self.layout)
40+
.finish()
41+
}
42+
}
43+
3344
impl<'a, Ty> Deref for TyAndLayout<'a, Ty> {
3445
type Target = &'a LayoutS;
3546
fn deref(&self) -> &&'a LayoutS {

tests/ui/abi/debug.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
// normalize-stderr-test "(valid_range): 0\.\.=(4294967295|18446744073709551615)" -> "$1: $$FULL"
55
// This pattern is prepared for when we account for alignment in the niche.
66
// normalize-stderr-test "(valid_range): [1-9]\.\.=(429496729[0-9]|1844674407370955161[0-9])" -> "$1: $$NON_NULL"
7-
// normalize-stderr-test "Leaf\(0x0*20\)" -> "Leaf(0x0...20)"
87
// Some attributes are only computed for release builds:
98
// compile-flags: -O
109
#![feature(rustc_attrs)]

tests/ui/abi/debug.stderr

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ error: fn_abi_of(test) = FnAbi {
8787
conv: Rust,
8888
can_unwind: $SOME_BOOL,
8989
}
90-
--> $DIR/debug.rs:16:1
90+
--> $DIR/debug.rs:15:1
9191
|
9292
LL | fn test(_x: u8) -> bool { true }
9393
| ^^^^^^^^^^^^^^^^^^^^^^^
@@ -181,7 +181,7 @@ error: fn_abi_of(TestFnPtr) = FnAbi {
181181
conv: Rust,
182182
can_unwind: $SOME_BOOL,
183183
}
184-
--> $DIR/debug.rs:19:1
184+
--> $DIR/debug.rs:18:1
185185
|
186186
LL | type TestFnPtr = fn(bool) -> u8;
187187
| ^^^^^^^^^^^^^^
@@ -190,7 +190,7 @@ error: fn_abi_of(test_generic) = FnAbi {
190190
args: [
191191
ArgAbi {
192192
layout: TyAndLayout {
193-
ty: *const T/#0,
193+
ty: *const T,
194194
layout: Layout {
195195
size: $SOME_SIZE,
196196
align: AbiAndPrefAlign {
@@ -257,13 +257,13 @@ error: fn_abi_of(test_generic) = FnAbi {
257257
conv: Rust,
258258
can_unwind: $SOME_BOOL,
259259
}
260-
--> $DIR/debug.rs:22:1
260+
--> $DIR/debug.rs:21:1
261261
|
262262
LL | fn test_generic<T>(_x: *const T) { }
263263
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
264264

265265
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
266-
--> $DIR/debug.rs:25:1
266+
--> $DIR/debug.rs:24:1
267267
|
268268
LL | const C: () = ();
269269
| ^^^^^^^^^^^
@@ -409,7 +409,7 @@ error: ABIs are not compatible
409409
conv: Rust,
410410
can_unwind: $SOME_BOOL,
411411
}
412-
--> $DIR/debug.rs:41:1
412+
--> $DIR/debug.rs:40:1
413413
|
414414
LL | type TestAbiNe = (fn(u8), fn(u32));
415415
| ^^^^^^^^^^^^^^
@@ -419,7 +419,7 @@ error: ABIs are not compatible
419419
args: [
420420
ArgAbi {
421421
layout: TyAndLayout {
422-
ty: [u8; Const { ty: usize, kind: Leaf(0x0...20) }],
422+
ty: [u8; 32],
423423
layout: Layout {
424424
size: Size(32 bytes),
425425
align: AbiAndPrefAlign {
@@ -490,7 +490,7 @@ error: ABIs are not compatible
490490
args: [
491491
ArgAbi {
492492
layout: TyAndLayout {
493-
ty: [u32; Const { ty: usize, kind: Leaf(0x0...20) }],
493+
ty: [u32; 32],
494494
layout: Layout {
495495
size: Size(128 bytes),
496496
align: AbiAndPrefAlign {
@@ -557,7 +557,7 @@ error: ABIs are not compatible
557557
conv: Rust,
558558
can_unwind: $SOME_BOOL,
559559
}
560-
--> $DIR/debug.rs:44:1
560+
--> $DIR/debug.rs:43:1
561561
|
562562
LL | type TestAbiNeLarger = (fn([u8; 32]), fn([u32; 32]));
563563
| ^^^^^^^^^^^^^^^^^^^^
@@ -700,7 +700,7 @@ error: ABIs are not compatible
700700
conv: Rust,
701701
can_unwind: $SOME_BOOL,
702702
}
703-
--> $DIR/debug.rs:47:1
703+
--> $DIR/debug.rs:46:1
704704
|
705705
LL | type TestAbiNeFloat = (fn(f32), fn(u32));
706706
| ^^^^^^^^^^^^^^^^^^^
@@ -846,13 +846,13 @@ error: ABIs are not compatible
846846
conv: Rust,
847847
can_unwind: $SOME_BOOL,
848848
}
849-
--> $DIR/debug.rs:51:1
849+
--> $DIR/debug.rs:50:1
850850
|
851851
LL | type TestAbiNeSign = (fn(i32), fn(u32));
852852
| ^^^^^^^^^^^^^^^^^^
853853

854854
error[E0277]: the size for values of type `str` cannot be known at compilation time
855-
--> $DIR/debug.rs:54:46
855+
--> $DIR/debug.rs:53:46
856856
|
857857
LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
858858
| ^^^^^^^^^^ doesn't have a size known at compile-time
@@ -861,7 +861,7 @@ LL | type TestAbiEqNonsense = (fn((str, str)), fn((str, str)));
861861
= note: only the last element of a tuple may have a dynamically sized type
862862

863863
error: `#[rustc_abi]` can only be applied to function items, type aliases, and associated functions
864-
--> $DIR/debug.rs:29:5
864+
--> $DIR/debug.rs:28:5
865865
|
866866
LL | const C: () = ();
867867
| ^^^^^^^^^^^
@@ -870,7 +870,7 @@ error: fn_abi_of(assoc_test) = FnAbi {
870870
args: [
871871
ArgAbi {
872872
layout: TyAndLayout {
873-
ty: &ReErased Adt(S, []),
873+
ty: &S,
874874
layout: Layout {
875875
size: $SOME_SIZE,
876876
align: AbiAndPrefAlign {
@@ -949,7 +949,7 @@ error: fn_abi_of(assoc_test) = FnAbi {
949949
conv: Rust,
950950
can_unwind: $SOME_BOOL,
951951
}
952-
--> $DIR/debug.rs:34:5
952+
--> $DIR/debug.rs:33:5
953953
|
954954
LL | fn assoc_test(&self) { }
955955
| ^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)