Skip to content

Commit c6db544

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

File tree

3 files changed

+42
-7
lines changed

3 files changed

+42
-7
lines changed

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

+26-2
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

+12-1
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.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -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 {
@@ -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 {
@@ -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 {

0 commit comments

Comments
 (0)