Skip to content

Commit bd31ba0

Browse files
committed
make Size and Align debug-printing a bit more compact
1 parent 9714e13 commit bd31ba0

11 files changed

+206
-523
lines changed

compiler/rustc_target/src/abi/mod.rs

+16-2
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/ui/layout/debug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// normalize-stderr-test "pref: Align \{\n *pow2: [1-3],\n *\}" -> "pref: $$PREF_ALIGN"
1+
// normalize-stderr-test "pref: Align\([1-8] bytes\)" -> "pref: $$PREF_ALIGN"
22
#![feature(never_type, rustc_attrs, type_alias_impl_trait)]
33
#![crate_type = "lib"]
44

src/test/ui/layout/debug.stderr

+30-90
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
error: layout_of(E) = Layout {
22
fields: Arbitrary {
33
offsets: [
4-
Size {
5-
raw: 0,
6-
},
4+
Size(0 bytes),
75
],
86
memory_index: [
97
0,
@@ -33,27 +31,17 @@ error: layout_of(E) = Layout {
3331
},
3432
largest_niche: None,
3533
align: AbiAndPrefAlign {
36-
abi: Align {
37-
pow2: 0,
38-
},
34+
abi: Align(1 bytes),
3935
pref: $PREF_ALIGN,
4036
},
41-
size: Size {
42-
raw: 4,
43-
},
37+
size: Size(4 bytes),
4438
},
4539
Layout {
4640
fields: Arbitrary {
4741
offsets: [
48-
Size {
49-
raw: 4,
50-
},
51-
Size {
52-
raw: 4,
53-
},
54-
Size {
55-
raw: 8,
56-
},
42+
Size(4 bytes),
43+
Size(4 bytes),
44+
Size(8 bytes),
5745
],
5846
memory_index: [
5947
0,
@@ -67,14 +55,10 @@ error: layout_of(E) = Layout {
6755
abi: Uninhabited,
6856
largest_niche: None,
6957
align: AbiAndPrefAlign {
70-
abi: Align {
71-
pow2: 2,
72-
},
58+
abi: Align(4 bytes),
7359
pref: $PREF_ALIGN,
7460
},
75-
size: Size {
76-
raw: 12,
77-
},
61+
size: Size(12 bytes),
7862
},
7963
],
8064
},
@@ -83,9 +67,7 @@ error: layout_of(E) = Layout {
8367
},
8468
largest_niche: Some(
8569
Niche {
86-
offset: Size {
87-
raw: 0,
88-
},
70+
offset: Size(0 bytes),
8971
value: Int(
9072
I32,
9173
false,
@@ -94,14 +76,10 @@ error: layout_of(E) = Layout {
9476
},
9577
),
9678
align: AbiAndPrefAlign {
97-
abi: Align {
98-
pow2: 2,
99-
},
79+
abi: Align(4 bytes),
10080
pref: $PREF_ALIGN,
10181
},
102-
size: Size {
103-
raw: 12,
104-
},
82+
size: Size(12 bytes),
10583
}
10684
--> $DIR/debug.rs:6:1
10785
|
@@ -111,15 +89,9 @@ LL | enum E { Foo, Bar(!, i32, i32) }
11189
error: layout_of(S) = Layout {
11290
fields: Arbitrary {
11391
offsets: [
114-
Size {
115-
raw: 0,
116-
},
117-
Size {
118-
raw: 0,
119-
},
120-
Size {
121-
raw: 4,
122-
},
92+
Size(0 bytes),
93+
Size(0 bytes),
94+
Size(4 bytes),
12395
],
12496
memory_index: [
12597
1,
@@ -148,14 +120,10 @@ error: layout_of(S) = Layout {
148120
),
149121
largest_niche: None,
150122
align: AbiAndPrefAlign {
151-
abi: Align {
152-
pow2: 2,
153-
},
123+
abi: Align(4 bytes),
154124
pref: $PREF_ALIGN,
155125
},
156-
size: Size {
157-
raw: 8,
158-
},
126+
size: Size(8 bytes),
159127
}
160128
--> $DIR/debug.rs:9:1
161129
|
@@ -174,14 +142,10 @@ error: layout_of(U) = Layout {
174142
},
175143
largest_niche: None,
176144
align: AbiAndPrefAlign {
177-
abi: Align {
178-
pow2: 2,
179-
},
145+
abi: Align(4 bytes),
180146
pref: $PREF_ALIGN,
181147
},
182-
size: Size {
183-
raw: 8,
184-
},
148+
size: Size(8 bytes),
185149
}
186150
--> $DIR/debug.rs:12:1
187151
|
@@ -191,9 +155,7 @@ LL | union U { f1: (i32, i32), f3: i32 }
191155
error: layout_of(std::result::Result<i32, i32>) = Layout {
192156
fields: Arbitrary {
193157
offsets: [
194-
Size {
195-
raw: 0,
196-
},
158+
Size(0 bytes),
197159
],
198160
memory_index: [
199161
0,
@@ -213,9 +175,7 @@ error: layout_of(std::result::Result<i32, i32>) = Layout {
213175
Layout {
214176
fields: Arbitrary {
215177
offsets: [
216-
Size {
217-
raw: 4,
218-
},
178+
Size(4 bytes),
219179
],
220180
memory_index: [
221181
0,
@@ -229,21 +189,15 @@ error: layout_of(std::result::Result<i32, i32>) = Layout {
229189
},
230190
largest_niche: None,
231191
align: AbiAndPrefAlign {
232-
abi: Align {
233-
pow2: 2,
234-
},
192+
abi: Align(4 bytes),
235193
pref: $PREF_ALIGN,
236194
},
237-
size: Size {
238-
raw: 8,
239-
},
195+
size: Size(8 bytes),
240196
},
241197
Layout {
242198
fields: Arbitrary {
243199
offsets: [
244-
Size {
245-
raw: 4,
246-
},
200+
Size(4 bytes),
247201
],
248202
memory_index: [
249203
0,
@@ -257,14 +211,10 @@ error: layout_of(std::result::Result<i32, i32>) = Layout {
257211
},
258212
largest_niche: None,
259213
align: AbiAndPrefAlign {
260-
abi: Align {
261-
pow2: 2,
262-
},
214+
abi: Align(4 bytes),
263215
pref: $PREF_ALIGN,
264216
},
265-
size: Size {
266-
raw: 8,
267-
},
217+
size: Size(8 bytes),
268218
},
269219
],
270220
},
@@ -286,9 +236,7 @@ error: layout_of(std::result::Result<i32, i32>) = Layout {
286236
),
287237
largest_niche: Some(
288238
Niche {
289-
offset: Size {
290-
raw: 0,
291-
},
239+
offset: Size(0 bytes),
292240
value: Int(
293241
I32,
294242
false,
@@ -297,14 +245,10 @@ error: layout_of(std::result::Result<i32, i32>) = Layout {
297245
},
298246
),
299247
align: AbiAndPrefAlign {
300-
abi: Align {
301-
pow2: 2,
302-
},
248+
abi: Align(4 bytes),
303249
pref: $PREF_ALIGN,
304250
},
305-
size: Size {
306-
raw: 8,
307-
},
251+
size: Size(8 bytes),
308252
}
309253
--> $DIR/debug.rs:15:1
310254
|
@@ -327,14 +271,10 @@ error: layout_of(i32) = Layout {
327271
),
328272
largest_niche: None,
329273
align: AbiAndPrefAlign {
330-
abi: Align {
331-
pow2: 2,
332-
},
274+
abi: Align(4 bytes),
333275
pref: $PREF_ALIGN,
334276
},
335-
size: Size {
336-
raw: 4,
337-
},
277+
size: Size(4 bytes),
338278
}
339279
--> $DIR/debug.rs:18:1
340280
|

0 commit comments

Comments
 (0)