]> git.proxmox.com Git - rustc.git/blob - src/test/codegen/repr-transparent.rs
New upstream version 1.37.0+dfsg1
[rustc.git] / src / test / codegen / repr-transparent.rs
1 // compile-flags: -C no-prepopulate-passes
2
3 #![crate_type="lib"]
4 #![feature(repr_simd, transparent_enums, transparent_unions)]
5
6 use std::marker::PhantomData;
7
8 #[derive(Copy, Clone)]
9 pub struct Zst1;
10 #[derive(Copy, Clone)]
11 pub struct Zst2(());
12
13 #[derive(Copy, Clone)]
14 #[repr(transparent)]
15 pub struct F32(f32);
16
17 // CHECK: define float @test_F32(float %arg0)
18 #[no_mangle]
19 pub extern fn test_F32(_: F32) -> F32 { loop {} }
20
21 #[repr(transparent)]
22 pub struct Ptr(*mut u8);
23
24 // CHECK: define i8* @test_Ptr(i8* %arg0)
25 #[no_mangle]
26 pub extern fn test_Ptr(_: Ptr) -> Ptr { loop {} }
27
28 #[repr(transparent)]
29 pub struct WithZst(u64, Zst1);
30
31 // CHECK: define i64 @test_WithZst(i64 %arg0)
32 #[no_mangle]
33 pub extern fn test_WithZst(_: WithZst) -> WithZst { loop {} }
34
35 #[repr(transparent)]
36 pub struct WithZeroSizedArray(*const f32, [i8; 0]);
37
38 // Apparently we use i32* when newtype-unwrapping f32 pointers. Whatever.
39 // CHECK: define i32* @test_WithZeroSizedArray(i32* %arg0)
40 #[no_mangle]
41 pub extern fn test_WithZeroSizedArray(_: WithZeroSizedArray) -> WithZeroSizedArray { loop {} }
42
43 #[repr(transparent)]
44 pub struct Generic<T>(T);
45
46 // CHECK: define double @test_Generic(double %arg0)
47 #[no_mangle]
48 pub extern fn test_Generic(_: Generic<f64>) -> Generic<f64> { loop {} }
49
50 #[repr(transparent)]
51 pub struct GenericPlusZst<T>(T, Zst2);
52
53 #[repr(u8)]
54 pub enum Bool { True, False, FileNotFound }
55
56 // CHECK: define{{( zeroext)?}} i8 @test_Gpz(i8{{( zeroext)?}} %arg0)
57 #[no_mangle]
58 pub extern fn test_Gpz(_: GenericPlusZst<Bool>) -> GenericPlusZst<Bool> { loop {} }
59
60 #[repr(transparent)]
61 pub struct LifetimePhantom<'a, T: 'a>(*const T, PhantomData<&'a T>);
62
63 // CHECK: define i16* @test_LifetimePhantom(i16* %arg0)
64 #[no_mangle]
65 pub extern fn test_LifetimePhantom(_: LifetimePhantom<i16>) -> LifetimePhantom<i16> { loop {} }
66
67 // This works despite current alignment resrictions because PhantomData is always align(1)
68 #[repr(transparent)]
69 pub struct UnitPhantom<T, U> { val: T, unit: PhantomData<U> }
70
71 pub struct Px;
72
73 // CHECK: define float @test_UnitPhantom(float %arg0)
74 #[no_mangle]
75 pub extern fn test_UnitPhantom(_: UnitPhantom<f32, Px>) -> UnitPhantom<f32, Px> { loop {} }
76
77 #[repr(transparent)]
78 pub struct TwoZsts(Zst1, i8, Zst2);
79
80 // CHECK: define{{( signext)?}} i8 @test_TwoZsts(i8{{( signext)?}} %arg0)
81 #[no_mangle]
82 pub extern fn test_TwoZsts(_: TwoZsts) -> TwoZsts { loop {} }
83
84 #[repr(transparent)]
85 pub struct Nested1(Zst2, Generic<f64>);
86
87 // CHECK: define double @test_Nested1(double %arg0)
88 #[no_mangle]
89 pub extern fn test_Nested1(_: Nested1) -> Nested1 { loop {} }
90
91 #[repr(transparent)]
92 pub struct Nested2(Nested1, Zst1);
93
94 // CHECK: define double @test_Nested2(double %arg0)
95 #[no_mangle]
96 pub extern fn test_Nested2(_: Nested2) -> Nested2 { loop {} }
97
98 #[repr(simd)]
99 struct f32x4(f32, f32, f32, f32);
100
101 #[repr(transparent)]
102 pub struct Vector(f32x4);
103
104 // CHECK: define <4 x float> @test_Vector(<4 x float> %arg0)
105 #[no_mangle]
106 pub extern fn test_Vector(_: Vector) -> Vector { loop {} }
107
108 trait Mirror { type It: ?Sized; }
109 impl<T: ?Sized> Mirror for T { type It = Self; }
110
111 #[repr(transparent)]
112 pub struct StructWithProjection(<f32 as Mirror>::It);
113
114 // CHECK: define float @test_Projection(float %arg0)
115 #[no_mangle]
116 pub extern fn test_Projection(_: StructWithProjection) -> StructWithProjection { loop {} }
117
118 #[repr(transparent)]
119 pub enum EnumF32 {
120 Variant(F32)
121 }
122
123 // CHECK: define float @test_EnumF32(float %arg0)
124 #[no_mangle]
125 pub extern fn test_EnumF32(_: EnumF32) -> EnumF32 { loop {} }
126
127 #[repr(transparent)]
128 pub enum EnumF32WithZsts {
129 Variant(Zst1, F32, Zst2)
130 }
131
132 // CHECK: define float @test_EnumF32WithZsts(float %arg0)
133 #[no_mangle]
134 pub extern fn test_EnumF32WithZsts(_: EnumF32WithZsts) -> EnumF32WithZsts { loop {} }
135
136 #[repr(transparent)]
137 pub union UnionF32 {
138 field: F32,
139 }
140
141 // CHECK: define float @test_UnionF32(float %arg0)
142 #[no_mangle]
143 pub extern fn test_UnionF32(_: UnionF32) -> UnionF32 { loop {} }
144
145 #[repr(transparent)]
146 pub union UnionF32WithZsts {
147 zst1: Zst1,
148 field: F32,
149 zst2: Zst2,
150 }
151
152 // CHECK: define float @test_UnionF32WithZsts(float %arg0)
153 #[no_mangle]
154 pub extern fn test_UnionF32WithZsts(_: UnionF32WithZsts) -> UnionF32WithZsts { loop {} }
155
156
157 // All that remains to be tested are aggregates. They are tested in separate files called repr-
158 // transparent-*.rs with `only-*` or `ignore-*` directives, because the expected LLVM IR
159 // function signatures vary so much that it's not reasonably possible to cover all of them with a
160 // single CHECK line.
161 //
162 // You may be wondering why we don't just compare the return types and argument types for equality
163 // with FileCheck regex captures. Well, rustc doesn't perform newtype unwrapping on newtypes
164 // containing aggregates. This is OK on all ABIs we support, but because LLVM has not gotten rid of
165 // pointee types yet, the IR function signature will be syntactically different (%Foo* vs
166 // %FooWrapper*).