]> git.proxmox.com Git - rustc.git/blob - src/test/ui/print_type_sizes/generics.rs
New upstream version 1.15.0+dfsg1
[rustc.git] / src / test / ui / print_type_sizes / generics.rs
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 }