Skip to content

Commit 75825fe

Browse files
committed
Tests of -Z print-type-sizes functionality.
Note that the tests have been updated to initialize the local variables; originally it was enough just to declare them. Back when I started this, the `layout_cache` contained entries even just for types that had been declared but not initialized. Apparently things have changed in the interim so that if I want one of those layouts to be computed, I need to actually initialize the value. (Incidentally, this shows a weakness in the strategy of just walking the `layout_cache`; the original strategy of using a MIR visitor would probably have exhibited more robustness in terms of consistent output, but it had other weaknesses so I chose not to reimplement it. At least, not yet.) ---- Also, I have updated tests to avoid target-specific alignments.
1 parent 70e5ca2 commit 75825fe

15 files changed

+442
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// All of the types that occur in this function are uninteresting, in
14+
// that one cannot control the sizes of these types with the same sort
15+
// of enum-variant manipulation tricks.
16+
17+
pub fn main() {
18+
let _byte: u8 = 0;
19+
let _word: usize = 0;
20+
let _tuple: (u8, usize)= (0, 0);
21+
let _array: [u8; 128] = [0; 128];
22+
let _fn: fn (u8) -> u8 = id;
23+
let _diverging: fn (u8) -> ! = bye;
24+
25+
fn id(x: u8) -> u8 { x };
26+
fn bye(_: u8) -> ! { loop { } }
27+
}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// This file illustrates how generics are handled: types have to be
14+
// monomorphized, in the MIR of the original function in which they
15+
// occur, to have their size reported.
16+
17+
// In an ad-hoc attempt to avoid the injection of unwinding code
18+
// (which clutters the output of `-Z print-type-sizes` with types from
19+
// `unwind::libunwind`):
20+
//
21+
// * I am not using Default to build values because that seems to
22+
// cause the injection of unwinding code. (Instead I just make `fn new`
23+
// methods.)
24+
//
25+
// * Pair derive Copy to ensure that we don't inject
26+
// unwinding code into generic uses of Pair when T itself is also
27+
// Copy.
28+
//
29+
// (I suspect this reflect some naivety within the rust compiler
30+
// itself; it should be checking for drop glue, i.e. a destructor
31+
// somewhere in the monomorphized types. It should not matter whether
32+
// the type is Copy.)
33+
#[derive(Copy, Clone)]
34+
pub struct Pair<T> {
35+
_car: T,
36+
_cdr: T,
37+
}
38+
39+
impl<T> Pair<T> {
40+
fn new(a: T, d: T) -> Self {
41+
Pair {
42+
_car: a,
43+
_cdr: d,
44+
}
45+
}
46+
}
47+
48+
#[derive(Copy, Clone)]
49+
pub struct SevenBytes([u8; 7]);
50+
pub struct FiftyBytes([u8; 50]);
51+
52+
pub struct ZeroSized;
53+
54+
impl SevenBytes {
55+
fn new() -> Self { SevenBytes([0; 7]) }
56+
}
57+
58+
impl FiftyBytes {
59+
fn new() -> Self { FiftyBytes([0; 50]) }
60+
}
61+
62+
pub fn f1<T:Copy>(x: T) {
63+
let _v: Pair<T> = Pair::new(x, x);
64+
let _v2: Pair<FiftyBytes> =
65+
Pair::new(FiftyBytes::new(), FiftyBytes::new());
66+
}
67+
68+
pub fn main() {
69+
let _b: Pair<u8> = Pair::new(0, 0);
70+
let _s: Pair<SevenBytes> = Pair::new(SevenBytes::new(), SevenBytes::new());
71+
let _z: ZeroSized = ZeroSized;
72+
f1::<SevenBytes>(SevenBytes::new());
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
print-type-size type: `Pair<FiftyBytes>`: 100 bytes, alignment: 1 bytes
2+
print-type-size field `._car`: 50 bytes
3+
print-type-size field `._cdr`: 50 bytes
4+
print-type-size type: `FiftyBytes`: 50 bytes, alignment: 1 bytes
5+
print-type-size field `.0`: 50 bytes
6+
print-type-size type: `Pair<SevenBytes>`: 14 bytes, alignment: 1 bytes
7+
print-type-size field `._car`: 7 bytes
8+
print-type-size field `._cdr`: 7 bytes
9+
print-type-size type: `SevenBytes`: 7 bytes, alignment: 1 bytes
10+
print-type-size field `.0`: 7 bytes
11+
print-type-size type: `Pair<u8>`: 2 bytes, alignment: 1 bytes
12+
print-type-size field `._car`: 1 bytes
13+
print-type-size field `._cdr`: 1 bytes
14+
print-type-size type: `ZeroSized`: 0 bytes, alignment: 1 bytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// This file illustrates that when multiple structural types occur in
14+
// a function, every one of them is included in the output.
15+
16+
pub struct SevenBytes([u8; 7]);
17+
pub struct FiftyBytes([u8; 50]);
18+
19+
pub enum Enum {
20+
Small(SevenBytes),
21+
Large(FiftyBytes),
22+
}
23+
24+
pub fn main() {
25+
let _e: Enum;
26+
let _f: FiftyBytes;
27+
let _s: SevenBytes;
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
print-type-size type: `Enum`: 51 bytes, alignment: 1 bytes
2+
print-type-size discriminant: 1 bytes
3+
print-type-size variant `Small`: 7 bytes
4+
print-type-size field `.0`: 7 bytes
5+
print-type-size variant `Large`: 50 bytes
6+
print-type-size field `.0`: 50 bytes
7+
print-type-size type: `FiftyBytes`: 50 bytes, alignment: 1 bytes
8+
print-type-size field `.0`: 50 bytes
9+
print-type-size type: `SevenBytes`: 7 bytes, alignment: 1 bytes
10+
print-type-size field `.0`: 7 bytes
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// This file illustrates that when the same type occurs repeatedly
14+
// (even if multiple functions), it is only printed once in the
15+
// print-type-sizes output.
16+
17+
pub struct SevenBytes([u8; 7]);
18+
19+
pub fn f1() {
20+
let _s: SevenBytes = SevenBytes([0; 7]);
21+
}
22+
23+
pub fn main() {
24+
let _s: SevenBytes = SevenBytes([0; 7]);
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
print-type-size type: `SevenBytes`: 7 bytes, alignment: 1 bytes
2+
print-type-size field `.0`: 7 bytes
+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// This file illustrates how enums with a non-null field are handled,
14+
// modelled after cases like `Option<&u32>` and such.
15+
//
16+
// It uses NonZero directly, rather than `&_` or `Unique<_>`, because
17+
// the test is not set up to deal with target-dependent pointer width.
18+
//
19+
// It avoids using u64/i64 because on some targets that is only 4-byte
20+
// aligned (while on most it is 8-byte aligned) and so the resulting
21+
// padding and overall computed sizes can be quite different.
22+
23+
#![feature(nonzero)]
24+
#![allow(dead_code)]
25+
26+
extern crate core;
27+
use core::nonzero::{NonZero, Zeroable};
28+
29+
pub enum MyOption<T> { None, Some(T) }
30+
31+
impl<T> Default for MyOption<T> {
32+
fn default() -> Self { MyOption::None }
33+
}
34+
35+
pub enum EmbeddedDiscr {
36+
None,
37+
Record { pre: u8, val: NonZero<u32>, post: u16 },
38+
}
39+
40+
impl Default for EmbeddedDiscr {
41+
fn default() -> Self { EmbeddedDiscr::None }
42+
}
43+
44+
#[derive(Default)]
45+
pub struct IndirectNonZero<T: Zeroable> {
46+
pre: u8,
47+
nested: NestedNonZero<T>,
48+
post: u16,
49+
}
50+
51+
pub struct NestedNonZero<T: Zeroable> {
52+
pre: u8,
53+
val: NonZero<T>,
54+
post: u16,
55+
}
56+
57+
impl<T: Zeroable+Default> Default for NestedNonZero<T> {
58+
fn default() -> Self {
59+
unsafe {
60+
NestedNonZero { pre: 0, val: NonZero::new(Default::default()), post: 0 }
61+
}
62+
}
63+
}
64+
65+
pub fn main() {
66+
let _x: MyOption<NonZero<u32>> = Default::default();
67+
let _y: EmbeddedDiscr = Default::default();
68+
let _z: MyOption<IndirectNonZero<u32>> = Default::default();
69+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
print-type-size type: `IndirectNonZero<u32>`: 20 bytes, alignment: 4 bytes
2+
print-type-size field `.pre`: 1 bytes
3+
print-type-size padding: 3 bytes
4+
print-type-size field `.nested`: 12 bytes, alignment: 4 bytes
5+
print-type-size field `.post`: 2 bytes
6+
print-type-size end padding: 2 bytes
7+
print-type-size type: `MyOption<IndirectNonZero<u32>>`: 20 bytes, alignment: 4 bytes
8+
print-type-size variant `Some`: 20 bytes
9+
print-type-size field `.0`: 20 bytes
10+
print-type-size type: `EmbeddedDiscr`: 12 bytes, alignment: 4 bytes
11+
print-type-size variant `Record`: 10 bytes
12+
print-type-size field `.pre`: 1 bytes
13+
print-type-size padding: 3 bytes
14+
print-type-size field `.val`: 4 bytes, alignment: 4 bytes
15+
print-type-size field `.post`: 2 bytes
16+
print-type-size end padding: 2 bytes
17+
print-type-size type: `NestedNonZero<u32>`: 12 bytes, alignment: 4 bytes
18+
print-type-size field `.pre`: 1 bytes
19+
print-type-size padding: 3 bytes
20+
print-type-size field `.val`: 4 bytes, alignment: 4 bytes
21+
print-type-size field `.post`: 2 bytes
22+
print-type-size end padding: 2 bytes
23+
print-type-size type: `MyOption<core::nonzero::NonZero<u32>>`: 4 bytes, alignment: 4 bytes
24+
print-type-size variant `Some`: 4 bytes
25+
print-type-size field `.0`: 4 bytes
26+
print-type-size type: `core::nonzero::NonZero<u32>`: 4 bytes, alignment: 4 bytes
27+
print-type-size field `.0`: 4 bytes
+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// This file illustrates how packing is handled; it should cause
14+
// the elimination of padding that would normally be introduced
15+
// to satisfy alignment desirata.
16+
//
17+
// It avoids using u64/i64 because on some targets that is only 4-byte
18+
// aligned (while on most it is 8-byte aligned) and so the resulting
19+
// padding and overall computed sizes can be quite different.
20+
21+
#![feature(untagged_unions)]
22+
23+
#![allow(dead_code)]
24+
25+
#[derive(Default)]
26+
#[repr(packed)]
27+
struct Packed {
28+
a: u8,
29+
b: u8,
30+
g: i32,
31+
c: u8,
32+
h: i16,
33+
d: u8,
34+
}
35+
36+
#[derive(Default)]
37+
struct Padded {
38+
a: u8,
39+
b: u8,
40+
g: i32,
41+
c: u8,
42+
h: i16,
43+
d: u8,
44+
}
45+
46+
pub fn main() {
47+
let _c: Packed = Default::default();
48+
let _d: Padded = Default::default();
49+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
print-type-size type: `Padded`: 16 bytes, alignment: 4 bytes
2+
print-type-size field `.a`: 1 bytes
3+
print-type-size field `.b`: 1 bytes
4+
print-type-size padding: 2 bytes
5+
print-type-size field `.g`: 4 bytes, alignment: 4 bytes
6+
print-type-size field `.c`: 1 bytes
7+
print-type-size padding: 1 bytes
8+
print-type-size field `.h`: 2 bytes, alignment: 2 bytes
9+
print-type-size field `.d`: 1 bytes
10+
print-type-size end padding: 3 bytes
11+
print-type-size type: `Packed`: 10 bytes, alignment: 1 bytes
12+
print-type-size field `.a`: 1 bytes
13+
print-type-size field `.b`: 1 bytes
14+
print-type-size field `.g`: 4 bytes
15+
print-type-size field `.c`: 1 bytes
16+
print-type-size field `.h`: 2 bytes
17+
print-type-size field `.d`: 1 bytes
+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: -Z print-type-sizes
12+
13+
// This file illustrates how padding is handled: alignment
14+
// requirements can lead to the introduction of padding, either before
15+
// fields or at the end of the structure as a whole.
16+
//
17+
// It avoids using u64/i64 because on some targets that is only 4-byte
18+
// aligned (while on most it is 8-byte aligned) and so the resulting
19+
// padding and overall computed sizes can be quite different.
20+
21+
#![allow(dead_code)]
22+
23+
struct S {
24+
a: bool,
25+
b: bool,
26+
g: i32,
27+
}
28+
29+
enum E1 {
30+
A(i32, i8),
31+
B(S),
32+
}
33+
34+
enum E2 {
35+
A(i8, i32),
36+
B(S),
37+
}
38+
39+
fn main() { }

0 commit comments

Comments
 (0)