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