Skip to content

Commit 96cfb20

Browse files
committed
organize Debug/Display impls a bit more; avoid sign-ignorant decimal display
1 parent 19eb934 commit 96cfb20

File tree

14 files changed

+65
-59
lines changed

14 files changed

+65
-59
lines changed

src/librustc_middle/mir/interpret/mod.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,9 @@ pub enum LitToConstError {
168168
#[derive(Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd)]
169169
pub struct AllocId(pub u64);
170170

171-
impl fmt::Display for AllocId {
171+
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
172+
// all the Miri types.
173+
impl fmt::Debug for AllocId {
172174
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
173175
if f.alternate() {
174176
write!(f, "a{}", self.0)
@@ -178,11 +180,9 @@ impl fmt::Display for AllocId {
178180
}
179181
}
180182

181-
// We also want the `Debug` output to be readable as it is used by `derive(Debug)` for
182-
// all the Miri types.
183-
impl fmt::Debug for AllocId {
184-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
185-
fmt::Display::fmt(self, fmt)
183+
impl fmt::Display for AllocId {
184+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
185+
fmt::Debug::fmt(self, f)
186186
}
187187
}
188188

src/librustc_middle/mir/interpret/pointer.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,11 @@ pub struct Pointer<Tag = (), Id = AllocId> {
119119

120120
static_assert_size!(Pointer, 16);
121121

122-
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Display for Pointer<Tag, Id> {
122+
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
123+
// all the Miri types.
124+
// We have to use `Debug` output for the tag, because `()` does not implement
125+
// `Display` so we cannot specialize that.
126+
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
123127
default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124128
if f.alternate() {
125129
write!(f, "{:#?}+0x{:x}[{:?}]", self.alloc_id, self.offset.bytes(), self.tag)
@@ -129,7 +133,7 @@ impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Display for Pointer<Tag, Id> {
129133
}
130134
}
131135
// Specialization for no tag
132-
impl<Id: fmt::Debug> fmt::Display for Pointer<(), Id> {
136+
impl<Id: fmt::Debug> fmt::Debug for Pointer<(), Id> {
133137
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
134138
if f.alternate() {
135139
write!(f, "{:#?}+0x{:x}", self.alloc_id, self.offset.bytes())
@@ -139,11 +143,9 @@ impl<Id: fmt::Debug> fmt::Display for Pointer<(), Id> {
139143
}
140144
}
141145

142-
// We also want the `Debug` output to be readable as it is used by `derive(Debug)` for
143-
// all the Miri types.
144-
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Pointer<Tag, Id> {
145-
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
146-
fmt::Display::fmt(self, fmt)
146+
impl<Tag: fmt::Debug> fmt::Display for Pointer<Tag> {
147+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
148+
fmt::Debug::fmt(self, f)
147149
}
148150
}
149151

src/librustc_middle/mir/interpret/value.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ pub enum Scalar<Tag = (), Id = AllocId> {
107107
#[cfg(target_arch = "x86_64")]
108108
static_assert_size!(Scalar, 24);
109109

110+
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
111+
// all the Miri types.
110112
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
111113
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112114
match self {
@@ -125,12 +127,9 @@ impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for Scalar<Tag, Id> {
125127
}
126128
}
127129

128-
impl<Tag> fmt::Display for Scalar<Tag> {
130+
impl<Tag: fmt::Debug> fmt::Display for Scalar<Tag> {
129131
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
130-
match self {
131-
Scalar::Ptr(_) => write!(f, "a pointer"),
132-
Scalar::Raw { data, .. } => write!(f, "{}", data),
133-
}
132+
fmt::Debug::fmt(self, f)
134133
}
135134
}
136135

@@ -554,16 +553,18 @@ impl<Tag> From<Pointer<Tag>> for ScalarMaybeUndef<Tag> {
554553
}
555554
}
556555

556+
// We want the `Debug` output to be readable as it is used by `derive(Debug)` for
557+
// all the Miri types.
557558
impl<Tag: fmt::Debug, Id: fmt::Debug> fmt::Debug for ScalarMaybeUndef<Tag, Id> {
558559
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
559560
match self {
560-
ScalarMaybeUndef::Undef => write!(f, "Undef"),
561+
ScalarMaybeUndef::Undef => write!(f, "<uninitialized>"),
561562
ScalarMaybeUndef::Scalar(s) => write!(f, "{:?}", s),
562563
}
563564
}
564565
}
565566

566-
impl<Tag> fmt::Display for ScalarMaybeUndef<Tag> {
567+
impl<Tag: fmt::Debug> fmt::Display for ScalarMaybeUndef<Tag> {
567568
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
568569
match self {
569570
ScalarMaybeUndef::Undef => write!(f, "uninitialized bytes"),

src/librustc_mir/interpret/machine.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ pub trait Machine<'mir, 'tcx>: Sized {
8484
/// Tag tracked alongside every pointer. This is used to implement "Stacked Borrows"
8585
/// <https://www.ralfj.de/blog/2018/08/07/stacked-borrows.html>.
8686
/// The `default()` is used for pointers to consts, statics, vtables and functions.
87+
/// The `Debug` formatting is used for displaying pointers; we cannot use `Display`
88+
/// as `()` does not implement that, but it should be "nice" output.
8789
type PointerTag: ::std::fmt::Debug + Copy + Eq + Hash + 'static;
8890

8991
/// Machines can define extra (non-instance) things that represent values of function pointers.

src/librustc_mir/interpret/operand.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
122122
p(cx, s, ty)?;
123123
return Ok(());
124124
}
125-
write!(f, "{:?}: {}", s.erase_tag(), self.layout.ty)
125+
write!(f, "{}: {}", s.erase_tag(), self.layout.ty)
126126
}
127127
Immediate::ScalarPair(a, b) => {
128128
// FIXME(oli-obk): at least print tuples and slices nicely
129-
write!(f, "({:?}, {:?}): {}", a.erase_tag(), b.erase_tag(), self.layout.ty,)
129+
write!(f, "({}, {}): {}", a.erase_tag(), b.erase_tag(), self.layout.ty,)
130130
}
131131
}
132132
})

src/test/ui/consts/const-eval/const-pointer-values-in-various-types.stderr

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/const-pointer-values-in-various-types.rs:25:5
33
|
44
LL | const I32_REF_USIZE_UNION: usize = unsafe { Nonsense { int_32_ref: &3 }.u };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc2+0x0, but expected initialized plain (non-pointer) bytes
66
|
77
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
88

@@ -36,7 +36,7 @@ error[E0080]: it is undefined behavior to use this value
3636
--> $DIR/const-pointer-values-in-various-types.rs:37:5
3737
|
3838
LL | const I32_REF_U64_UNION: u64 = unsafe { Nonsense { int_32_ref: &3 }.uint_64 };
39-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc22+0x0, but expected initialized plain (non-pointer) bytes
4040
|
4141
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
4242

@@ -76,7 +76,7 @@ error[E0080]: it is undefined behavior to use this value
7676
--> $DIR/const-pointer-values-in-various-types.rs:52:5
7777
|
7878
LL | const I32_REF_I64_UNION: i64 = unsafe { Nonsense { int_32_ref: &3 }.int_64 };
79-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
79+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc47+0x0, but expected initialized plain (non-pointer) bytes
8080
|
8181
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
8282

@@ -100,7 +100,7 @@ error[E0080]: it is undefined behavior to use this value
100100
--> $DIR/const-pointer-values-in-various-types.rs:61:5
101101
|
102102
LL | const I32_REF_F64_UNION: f64 = unsafe { Nonsense { int_32_ref: &3 }.float_64 };
103-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
103+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc62+0x0, but expected initialized plain (non-pointer) bytes
104104
|
105105
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
106106

@@ -148,7 +148,7 @@ error[E0080]: it is undefined behavior to use this value
148148
--> $DIR/const-pointer-values-in-various-types.rs:79:5
149149
|
150150
LL | const STR_U64_UNION: u64 = unsafe { Nonsense { stringy: "3" }.uint_64 };
151-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
151+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc86+0x0, but expected initialized plain (non-pointer) bytes
152152
|
153153
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
154154

@@ -188,7 +188,7 @@ error[E0080]: it is undefined behavior to use this value
188188
--> $DIR/const-pointer-values-in-various-types.rs:94:5
189189
|
190190
LL | const STR_I64_UNION: i64 = unsafe { Nonsense { stringy: "3" }.int_64 };
191-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
191+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc101+0x0, but expected initialized plain (non-pointer) bytes
192192
|
193193
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
194194

@@ -212,7 +212,7 @@ error[E0080]: it is undefined behavior to use this value
212212
--> $DIR/const-pointer-values-in-various-types.rs:103:5
213213
|
214214
LL | const STR_F64_UNION: f64 = unsafe { Nonsense { stringy: "3" }.float_64 };
215-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
215+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc110+0x0, but expected initialized plain (non-pointer) bytes
216216
|
217217
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
218218

src/test/ui/consts/const-eval/double_check2.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LL | / static FOO: (&Foo, &Bar) = unsafe {(
55
LL | | Union { u8: &BAR }.foo,
66
LL | | Union { u8: &BAR }.bar,
77
LL | | )};
8-
| |___^ type validation failed: encountered 5 at .1.<deref>, but expected a valid enum discriminant
8+
| |___^ type validation failed: encountered 0x05 at .1.<deref>, but expected a valid enum discriminant
99
|
1010
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
1111

src/test/ui/consts/const-eval/ref_to_int_match.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/ref_to_int_match.rs:25:1
33
|
44
LL | const BAR: Int = unsafe { Foo { r: &42 }.f };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a pointer, but expected initialized plain (non-pointer) bytes
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered alloc2+0x0, but expected initialized plain (non-pointer) bytes
66
|
77
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
88

src/test/ui/consts/const-eval/transmute-const.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0080]: it is undefined behavior to use this value
22
--> $DIR/transmute-const.rs:5:1
33
|
44
LL | static FOO: bool = unsafe { mem::transmute(3u8) };
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 3, but expected a boolean
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered 0x03, but expected a boolean
66
|
77
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rustc repository if you believe it should not be considered undefined behavior.
88

src/test/ui/consts/const-eval/ub-enum.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// normalize-stderr-64bit "0x0000000000" -> "0x00"
12
#![feature(const_transmute, never_type)]
23
#![allow(const_err)] // make sure we cannot allow away the errors tested here
34

0 commit comments

Comments
 (0)