Debian Codegraph @

gcc/testsuite/gdc.test/compilable/noreturn3.d

raw
1 /*
2 REQUIRED_ARGS: -w -o- -d
3
4 More complex examples from the DIP
5 https://github.com/dlang/DIPs/blob/master/DIPs/accepted/DIP1034.md
6 */
7
8 alias noreturn = typeof(*null);
9 static assert (!is(noreturn == void));
10
11 void initialize()
12 {
13 noreturn a;
14 noreturn b = noreturn.init;
15 }
16
17 void foo(const noreturn);
18 void foo(const int);
19
20 noreturn bar();
21
22 void overloads()
23 {
24 noreturn n;
25 foo(n);
26
27 foo(bar());
28 }
29
30 // /*****************************************************************************/
31
32 auto inferNoreturn(int i)
33 {
34 if (i < 0)
35 return assert(false);
36 else if (i == 0)
37 return assert(false);
38 else
39 return assert(false);
40 }
41
42 auto inferReturn(int i)
43 {
44 if (i < 0)
45 return assert(false);
46 else if (i == 0)
47 return i;
48 else
49 return assert(false);
50 }
51
52 // /*****************************************************************************/
53 // // https://issues.dlang.org/show_bug.cgi?id=22004
54
55 alias fun22004 = _ => {}();
56 alias gun22004 = _ => assert(0);
57 auto bun22004(bool b)
58 {
59 if (b)
60 return gun22004(0);
61 else
62 return fun22004(0);
63 }
64
65 static assert(is(typeof(bun22004(true)) == void));
66
67 // // Reversed order
68 auto bun22004_reversed(bool b)
69 {
70 if (b)
71 return fun22004(0);
72 else
73 return gun22004(0);
74 }
75
76 static assert(is(typeof(bun22004_reversed(true)) == void));
77
78 // /*****************************************************************************/
79
80 // // Also works fine with non-void types and ref inference
81
82 int global;
83
84 auto ref forwardOrExit(ref int num)
85 {
86 if (num)
87 return num;
88 else
89 return assert(false);
90 }
91
92 static assert( is(typeof(forwardOrExit(global)) == int));
93
94 // Noreturn types do not affect `auto ref` deduction
95 static assert(is(typeof(&forwardOrExit(global))));
96
97 auto ref forwardOrExit2(ref int num)
98 {
99 if (num)
100 return assert(false);
101 else
102 return num;
103 }
104
105 static assert( is(typeof(forwardOrExit2(global)) == int));
106
107 // Noreturn types do not affect `auto ref` deduction
108 static assert(is(typeof(&forwardOrExit2(global))));
109
110 /*****************************************************************************/
111
112 void inference()
113 {
114 auto inf = cast(noreturn) 1;
115 static assert(is(typeof(inf) == noreturn));
116
117 noreturn n;
118 auto c = cast(const shared noreturn) n;
119 static assert(is(typeof(c) == const shared noreturn));
120 static assert(is(typeof(n) == noreturn));
121
122 auto c2 = cast(immutable noreturn) n;
123 static assert(is(typeof(c) == const shared noreturn));
124 static assert(is(typeof(c2) == immutable noreturn));
125 static assert(is(typeof(n) == noreturn));
126 }
127
128
129 /******************************************************************************/
130 // https://issues.dlang.org/show_bug.cgi?id=21957
131 // Calculate proper alignment and size for noreturn members
132
133 enum longPad = long.alignof - int.sizeof;
134
135 struct BasicStruct
136 {
137 int firstInt;
138 noreturn noRet;
139 long lastLong;
140 }
141
142 static assert(BasicStruct.sizeof == (int.sizeof + longPad + long.sizeof));
143
144 static assert(BasicStruct.firstInt.offsetof == 0);
145 static assert(BasicStruct.noRet.offsetof == 4);
146 static assert(BasicStruct.lastLong.offsetof == (4 + longPad));
147
148 struct AlignedStruct
149 {
150 int firstInt;
151 align(16) noreturn noRet;
152 long lastLong;
153 }
154
155 static assert(AlignedStruct.sizeof == 32);
156
157 static assert(AlignedStruct.firstInt.offsetof == 0);
158 static assert(AlignedStruct.noRet.offsetof == 16);
159 static assert(AlignedStruct.lastLong.offsetof == 16);
160
161 union BasicUnion
162 {
163 int firstInt;
164 noreturn noRet;
165 long lastLong;
166 }
167
168 static assert(BasicUnion.sizeof == 8);
169
170 static assert(BasicUnion.firstInt.offsetof == 0);
171 static assert(BasicUnion.noRet.offsetof == 0);
172 static assert(BasicUnion.lastLong.offsetof == 0);
173
174 union AlignedUnion
175 {
176 int firstInt;
177 align(16) noreturn noRet;
178 long lastLong;
179 }
180
181 static assert(AlignedUnion.sizeof == 16);
182
183 static assert(AlignedUnion.firstInt.offsetof == 0);
184 static assert(AlignedUnion.noRet.offsetof == 0);
185 static assert(AlignedUnion.lastLong.offsetof == 0);
186
187 class BasicClass
188 {
189 int firstInt;
190 noreturn noRet;
191 long lastLong;
192 }
193
194 enum objectMemberSize = __traits(classInstanceSize, Object);
195
196 static assert(__traits(classInstanceSize, BasicClass) == objectMemberSize + (int.sizeof + longPad + long.sizeof));
197
198 static assert(BasicClass.firstInt.offsetof == objectMemberSize + 0);
199 static assert(BasicClass.noRet.offsetof == objectMemberSize + 4);
200 static assert(BasicClass.lastLong.offsetof == objectMemberSize + (4 + longPad));
201
202 class AlignedClass
203 {
204 int firstInt;
205 align(16) noreturn noRet;
206 long lastLong;
207 }
208
209 enum offset = (objectMemberSize + 4 + 16) & ~15;
210
211 static assert(__traits(classInstanceSize, AlignedClass) == offset + 8);
212
213 static assert(AlignedClass.firstInt.offsetof == objectMemberSize + 0);
214 static assert(AlignedClass.noRet.offsetof == offset);
215 static assert(AlignedClass.lastLong.offsetof == offset);
216
217 struct EmptyStruct
218 {
219 noreturn noRet;
220 }
221
222 static assert(EmptyStruct.sizeof == 1);
223 static assert(EmptyStruct.noRet.offsetof == 0);
224
225 struct EmptyStruct2
226 {
227 noreturn[4] noRet;
228 }
229
230 static assert(EmptyStruct2.sizeof == 1);
231 static assert(EmptyStruct2.noRet.offsetof == 0);
232
233 // https://issues.dlang.org/show_bug.cgi?id=22858
234 // Shouldn't mess with the alignment of other zero-sized types.
235
236 struct S22858
237 {
238 int a;
239 void*[0] arr;
240 char c;
241 noreturn[0] arr2;
242 char c2;
243 }
244
245 static assert (S22858.arr.offsetof % size_t.sizeof == 0);
246 static assert (S22858.arr2.offsetof == S22858.c.offsetof + 1);
247 static assert (S22858.arr2.offsetof == S22858.c2.offsetof);