]> git.proxmox.com Git - rustc.git/blame - src/test/ui/mir/mir_adt_construction.rs
Update unsuspicious file list
[rustc.git] / src / test / ui / mir / mir_adt_construction.rs
CommitLineData
b7449926 1// run-pass
8bb4bdeb
XL
2use std::fmt;
3
9e0c209e 4#[repr(C)]
9cc50fc6
SL
5enum CEnum {
6 Hello = 30,
7 World = 60
8}
9
9cc50fc6 10fn test1(c: CEnum) -> i32 {
8bb4bdeb
XL
11 let c2 = CEnum::Hello;
12 match (c, c2) {
13 (CEnum::Hello, CEnum::Hello) => 42,
14 (CEnum::World, CEnum::Hello) => 0,
15 _ => 1
16 }
9cc50fc6
SL
17}
18
19#[repr(packed)]
9cc50fc6
SL
20struct Pakd {
21 a: u64,
22 b: u32,
23 c: u16,
24 d: u8,
25 e: ()
26}
27
8bb4bdeb
XL
28// It is unsafe to use #[derive(Debug)] on a packed struct because the code generated by the derive
29// macro takes references to the fields instead of accessing them directly.
30impl fmt::Debug for Pakd {
31 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32 // It's important that we load the fields into locals by-value here. This will do safe
33 // unaligned loads into the locals, then pass references to the properly-aligned locals to
34 // the formatting code.
35 let Pakd { a, b, c, d, e } = *self;
36 f.debug_struct("Pakd")
37 .field("a", &a)
38 .field("b", &b)
39 .field("c", &c)
40 .field("d", &d)
41 .field("e", &e)
42 .finish()
43 }
44}
45
46// It is unsafe to use #[derive(PartialEq)] on a packed struct because the code generated by the
47// derive macro takes references to the fields instead of accessing them directly.
48impl PartialEq for Pakd {
49 fn eq(&self, other: &Pakd) -> bool {
50 self.a == other.a &&
51 self.b == other.b &&
52 self.c == other.c &&
53 self.d == other.d &&
54 self.e == other.e
55 }
56}
57
9cc50fc6
SL
58impl Drop for Pakd {
59 fn drop(&mut self) {}
60}
61
9cc50fc6
SL
62fn test2() -> Pakd {
63 Pakd { a: 42, b: 42, c: 42, d: 42, e: () }
64}
65
66#[derive(PartialEq, Debug)]
67struct TupleLike(u64, u32);
68
9cc50fc6
SL
69fn test3() -> TupleLike {
70 TupleLike(42, 42)
71}
72
9cc50fc6
SL
73fn test4(x: fn(u64, u32) -> TupleLike) -> (TupleLike, TupleLike) {
74 let y = TupleLike;
75 (x(42, 84), y(42, 84))
76}
77
9cc50fc6
SL
78fn test5(x: fn(u32) -> Option<u32>) -> (Option<u32>, Option<u32>) {
79 let y = Some;
80 (x(42), y(42))
81}
82
83fn main() {
8bb4bdeb
XL
84 assert_eq!(test1(CEnum::Hello), 42);
85 assert_eq!(test1(CEnum::World), 0);
86 assert_eq!(test2(), Pakd { a: 42, b: 42, c: 42, d: 42, e: () });
87 assert_eq!(test3(), TupleLike(42, 42));
88 let t4 = test4(TupleLike);
89 assert_eq!(t4.0, t4.1);
90 let t5 = test5(Some);
91 assert_eq!(t5.0, t5.1);
9cc50fc6 92}