]> git.lizzy.rs Git - rust.git/blob - tests/mir-opt/sroa.rs
Rollup merge of #106766 - GuillaumeGomez:rm-stripper-dead-code, r=notriddle
[rust.git] / tests / mir-opt / sroa.rs
1 // unit-test: ScalarReplacementOfAggregates
2 // compile-flags: -Cpanic=abort
3 // no-prefer-dynamic
4
5 struct Tag(usize);
6
7 #[repr(C)]
8 struct S(Tag, Tag, Tag);
9
10 impl Drop for Tag {
11     #[inline(never)]
12     fn drop(&mut self) {}
13 }
14
15 // EMIT_MIR sroa.dropping.ScalarReplacementOfAggregates.diff
16 pub fn dropping() {
17     S(Tag(0), Tag(1), Tag(2)).1;
18 }
19
20 // EMIT_MIR sroa.enums.ScalarReplacementOfAggregates.diff
21 pub fn enums(a: usize) -> usize {
22     if let Some(a) = Some(a) { a } else { 0 }
23 }
24
25 // EMIT_MIR sroa.structs.ScalarReplacementOfAggregates.diff
26 pub fn structs(a: f32) -> f32 {
27     struct U {
28         _foo: usize,
29         a: f32,
30     }
31
32     U { _foo: 0, a }.a
33 }
34
35 // EMIT_MIR sroa.unions.ScalarReplacementOfAggregates.diff
36 pub fn unions(a: f32) -> u32 {
37     union Repr {
38         f: f32,
39         u: u32,
40     }
41     unsafe { Repr { f: a }.u }
42 }
43
44 struct Foo {
45     a: u8,
46     b: (),
47     c: &'static str,
48     d: Option<isize>,
49 }
50
51 fn g() -> u32 {
52     3
53 }
54
55 // EMIT_MIR sroa.flat.ScalarReplacementOfAggregates.diff
56 pub fn flat() {
57     let Foo { a, b, c, d } = Foo { a: 5, b: (), c: "a", d: Some(-4) };
58     let _ = a;
59     let _ = b;
60     let _ = c;
61     let _ = d;
62 }
63
64 #[repr(C)]
65 struct Escaping {
66     a: u32,
67     b: u32,
68     c: u32,
69 }
70
71 fn f(a: *const u32) {
72     println!("{}", unsafe { *a.add(2) });
73 }
74
75 // EMIT_MIR sroa.escaping.ScalarReplacementOfAggregates.diff
76 pub fn escaping() {
77     // Verify this struct is not flattened.
78     f(&Escaping { a: 1, b: 2, c: g() }.a);
79 }
80
81 fn main() {
82     dropping();
83     enums(5);
84     structs(5.);
85     unions(5.);
86     flat();
87     escaping();
88 }