Debian Codegraph @

gcc/testsuite/gdc.test/runnable/noreturn1.d

raw
1 alias noreturn = typeof(*null);
2
3 extern (C) noreturn exit();
4
5 /*****************************************/
6
7 bool testf(int i)
8 {
9 return i && assert(0);
10 }
11
12 bool testt(int i)
13 {
14 return i || assert(0);
15 }
16
17 int testa(int i)
18 {
19 if (i && exit())
20 return i + 1;
21 return i - 1;
22 }
23
24 int testb(int i)
25 {
26 if (i || exit())
27 return i + 1;
28 return i - 1;
29 }
30
31 void test1()
32 {
33 assert(testf(0) == false);
34 assert(testt(1) == true);
35
36 assert(testa(0) == -1);
37 assert(testb(3) == 4);
38 }
39
40 /*****************************************/
41
42 noreturn exit1() { assert(0); }
43 noreturn exit2() { assert(0); }
44
45
46 int heli1(int i)
47 {
48 return i ? exit1() : i - 1;
49 }
50
51 int heli2(int i)
52 {
53 return i ? i - 1 : exit1();
54 }
55
56 noreturn heli3(int i)
57 {
58 return i ? exit1() : exit2();
59 }
60
61 void test2()
62 {
63 assert(heli1(0) == -1);
64 assert(heli2(1) == 0);
65 }
66
67 /*****************************************/
68
69 struct BasicStruct
70 {
71 int firstInt;
72 noreturn noRet;
73 long lastLong;
74 }
75
76 struct AlignedStruct
77 {
78 int firstInt;
79 align(16) noreturn noRet;
80 long lastLong;
81 }
82
83 void takeBasic(BasicStruct bs)
84 {
85 assert(bs.firstInt == 13);
86 assert(bs.lastLong == 42);
87
88 assert(&bs.noRet == (&bs.firstInt + 1));
89 }
90
91 void takeAligned(AlignedStruct as)
92 {
93 assert(as.firstInt == 99);
94 assert(as.lastLong == 0xDEADBEEF);
95
96 assert(&as.noRet == &as.lastLong);
97 }
98
99 void test3()
100 {
101 {
102 BasicStruct bs;
103 bs.firstInt = 13;
104 bs.lastLong = 42;
105 takeBasic(bs);
106 }
107 {
108 AlignedStruct as;
109 as.firstInt = 99;
110 as.lastLong = 0xDEADBEEF;
111 takeAligned(as);
112 }
113 }
114
115 /*****************************************/
116
117 Exception collectException(void function() f)
118 {
119 try
120 {
121 f();
122 return null;
123 }
124 catch (Exception e)
125 return e;
126 }
127
128
129 int return_()
130 {
131 return throw new Exception("Return");
132 }
133
134 void ternary(int i)
135 {
136 i > 0 ? i++ : throw new Exception("Ternary");
137 }
138
139 void call()
140 {
141 ternary(throw new Exception("Call"));
142 }
143
144 void arrayLiteral()
145 {
146 int[] arr = [
147 1,
148 throw new Exception("ArrayLiteral"),
149 2
150 ];
151 }
152
153 void assocArrayLiteral()
154 {
155 int[string] arr = [
156 "A": 1,
157 "B": throw new Exception("AssocArrayLiteral"),
158 "C": 2
159 ];
160 }
161
162 void testThrowExpression()
163 {
164 Exception ae = collectException({ return_(); });
165 assert(ae);
166
167 ae = collectException({ ternary(1); });
168 assert(!ae);
169
170 ae = collectException({ ternary(-1); });
171 assert(ae);
172
173 ae = collectException(&call);
174 assert(ae);
175 assert(ae.msg == "Call");
176
177 ae = collectException(&arrayLiteral);
178 assert(ae);
179
180 ae = collectException(&assocArrayLiteral);
181 assert(ae);
182 }
183
184
185 /*****************************************/
186
187 /// Verify that throws does not screw with side effects
188 void testThrowSideEffect()
189 {
190 static void foo(bool, void*, int) {}
191
192 bool b;
193 int i;
194
195 try
196 {
197 foo(b = true, throw new Exception(""), i++);
198 assert(false);
199 }
200 catch (Exception) {}
201
202 assert(b == true);
203 assert(i == 0);
204 }
205
206 /// Verify that throws does not screw with dtors
207 void testThrowDtor()
208 {
209 static struct S
210 {
211 __gshared int destructed;
212 int id;
213
214 ~this()
215 {
216 assert(!(destructed & id));
217 destructed |= id;
218 }
219
220 string getMessage()
221 {
222 // Force runtime dependency
223 return destructed ? "Already destructed" : "Valid";
224 }
225 }
226
227 static void fooD(S, int, S) {}
228 bool caught;
229
230 try
231 {
232 fooD(S(1), throw new Exception(S(2).getMessage()), S(4));
233 assert(false);
234 }
235 catch (Exception e)
236 {
237 caught = true;
238 assert(e.msg == "Valid");
239 }
240 assert(caught);
241 assert(S.destructed == (1 | 2));
242
243
244 static void fooC(S, int, S) {}
245 caught = false;
246 S.destructed = 0;
247
248 try
249 {
250 fooC(S(1), throw new Exception(S(2).getMessage()), S(4));
251 assert(false);
252 }
253 catch (Exception e)
254 {
255 caught = true;
256 assert(e.msg == "Valid");
257 }
258 assert(caught);
259 assert(S.destructed == (1 | 2));
260 }
261
262 /*****************************************/
263
264 noreturn func()
265 {
266 throw new Exception("B");
267 }
268
269 // https://issues.dlang.org/show_bug.cgi?id=23120
270 void test23120()
271 {
272 string a;
273 try
274 {
275 noreturn q = throw new Exception ("A");
276 }
277 catch(Exception e)
278 {
279 a ~= e.msg;
280 }
281
282 try
283 {
284 noreturn z = func();
285 }
286 catch(Exception e)
287 {
288 a ~= e.msg;
289 }
290
291 assert(a == "AB");
292 }
293
294 /*****************************************/
295 int main()
296 {
297 test1();
298 test2();
299 test3();
300 testThrowExpression();
301 testThrowSideEffect();
302 testThrowDtor();
303 test23120();
304 return 0;
305 }