]> git.lizzy.rs Git - rust.git/blob - src/test/ui/print_type_sizes/generics.rs
360c9958686543d2f8ef4b1bad97da74cbb3ae75
[rust.git] / src / test / ui / print_type_sizes / generics.rs
1 // compile-flags: -Z print-type-sizes
2 // compile-pass
3
4 // This file illustrates how generics are handled: types have to be
5 // monomorphized, in the MIR of the original function in which they
6 // occur, to have their size reported.
7
8 #![feature(start)]
9
10 // In an ad-hoc attempt to avoid the injection of unwinding code
11 // (which clutters the output of `-Z print-type-sizes` with types from
12 // `unwind::libunwind`):
13 //
14 //   * I am not using Default to build values because that seems to
15 //     cause the injection of unwinding code. (Instead I just make `fn new`
16 //     methods.)
17 //
18 //   * Pair derive Copy to ensure that we don't inject
19 //     unwinding code into generic uses of Pair when T itself is also
20 //     Copy.
21 //
22 //     (I suspect this reflect some naivety within the rust compiler
23 //      itself; it should be checking for drop glue, i.e., a destructor
24 //      somewhere in the monomorphized types. It should not matter whether
25 //      the type is Copy.)
26 #[derive(Copy, Clone)]
27 pub struct Pair<T> {
28     _car: T,
29     _cdr: T,
30 }
31
32 impl<T> Pair<T> {
33     fn new(a: T, d: T) -> Self {
34         Pair {
35             _car: a,
36             _cdr: d,
37         }
38     }
39 }
40
41 #[derive(Copy, Clone)]
42 pub struct SevenBytes([u8; 7]);
43 pub struct FiftyBytes([u8; 50]);
44
45 pub struct ZeroSized;
46
47 impl SevenBytes {
48     fn new() -> Self { SevenBytes([0; 7]) }
49 }
50
51 impl FiftyBytes {
52     fn new() -> Self { FiftyBytes([0; 50]) }
53 }
54
55 pub fn f1<T:Copy>(x: T) {
56     let _v: Pair<T> = Pair::new(x, x);
57     let _v2: Pair<FiftyBytes> =
58         Pair::new(FiftyBytes::new(), FiftyBytes::new());
59 }
60
61 #[start]
62 fn start(_: isize, _: *const *const u8) -> isize {
63     let _b: Pair<u8> = Pair::new(0, 0);
64     let _s: Pair<SevenBytes> = Pair::new(SevenBytes::new(), SevenBytes::new());
65     let ref _z: ZeroSized = ZeroSized;
66     f1::<SevenBytes>(SevenBytes::new());
67     0
68 }