Debian Codegraph @

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

raw
1 // PERMUTE_ARGS:
2 // REQUIRED_ARGS: -d -preview=dip1000
3
4 extern(C) int printf(const char*, ...);
5
6 class Eh : Exception
7 {
8 this()
9 {
10 super("Eh thrown");
11 }
12 }
13
14
15 /********************************************/
16
17 class Foo
18 {
19 static int x;
20
21 this()
22 {
23 assert(x == 0);
24 x++;
25 printf("Foo.this()\n");
26 throw new Eh();
27 assert(0);
28 }
29
30 ~this()
31 {
32 printf("Foo.~this()\n");
33 }
34 }
35
36 void test1()
37 {
38 try
39 {
40 scope Foo f = new Foo();
41 assert(0);
42 }
43 catch (Eh)
44 {
45 assert(Foo.x == 1);
46 Foo.x++;
47 }
48 finally
49 {
50 assert(Foo.x == 2);
51 Foo.x++;
52 }
53 assert(Foo.x == 3);
54 }
55
56 /********************************************/
57
58 void test2()
59 {
60 int x;
61 {
62 scope (exit) { printf("test1\n"); assert(x == 3); x = 4; }
63 scope (exit) { printf("test2\n"); assert(x == 2); x = 3; }
64 scope (exit) { printf("test3\n"); assert(x == 1); x = 2; }
65 printf("test4\n"); assert(x == 0); x = 1;
66 }
67 assert(x == 4);
68 }
69
70 /********************************************/
71
72 void test3()
73 {
74 int x;
75 {
76 scope (success) { printf("test1\n"); assert(x == 3); x = 4; }
77 scope (success) { printf("test2\n"); assert(x == 2); x = 3; }
78 scope (success) { printf("test3\n"); assert(x == 1); x = 2; }
79 printf("test4\n"); assert(x == 0); x = 1;
80 }
81 assert(x == 4);
82 }
83
84 /********************************************/
85
86 void test4()
87 {
88 int x;
89 try
90 {
91 scope (exit) { printf("test1\n"); assert(x == 3); x = 4; }
92 scope (exit) { printf("test2\n"); assert(x == 2); x = 3; }
93 x = 2;
94 throw new Eh;
95 scope (exit) { printf("test3\n"); assert(x == 1); x = 2; }
96 printf("test4\n"); assert(x == 0); x = 1;
97 }
98 catch (Eh e)
99 {
100 }
101 assert(x == 4);
102 }
103
104 /********************************************/
105
106 void test5()
107 {
108 int x;
109 try
110 {
111 scope (success) { printf("test1\n"); assert(x == 3); x = 4; }
112 scope (success) { printf("test2\n"); assert(x == 2); x = 3; }
113 x = 2;
114 throw new Eh;
115 scope (success) { printf("test3\n"); assert(x == 1); x = 2; }
116 printf("test4\n"); assert(x == 0); x = 1;
117 }
118 catch (Eh e)
119 {
120 }
121 assert(x == 2);
122 }
123
124 /********************************************/
125
126 void test6()
127 {
128 int x;
129 scope (failure) { assert(0); }
130 try
131 {
132 scope (failure) { printf("test1\n"); assert(x == 3); x = 4; }
133 scope (failure) { printf("test2\n"); assert(x == 2); x = 3; }
134 x = 2;
135 throw new Eh;
136 scope (failure) { printf("test3\n"); assert(x == 1); x = 2; }
137 printf("test4\n"); assert(x == 0); x = 1;
138 }
139 catch (Eh e)
140 {
141 }
142 assert(x == 4);
143 }
144
145 /********************************************/
146
147 void test7()
148 { int i;
149 int x;
150
151 void foo()
152 {
153 scope (success) { assert(x == 1); x = 2; }
154 i = 2;
155 if (i == 2)
156 return;
157 }
158
159 i = 1;
160 x = 1;
161 foo();
162 assert(x == 2);
163 }
164
165 /********************************************/
166
167
168 void test8()
169 {
170 int i;
171 {
172 version (all)
173 {
174 scope (exit) i += 2;
175 }
176 assert(i == 0);
177 i += 1;
178 printf("betty\n");
179 }
180 assert(i == 3);
181 }
182
183 /********************************************/
184
185 char[] r9;
186
187 int scp( int n )
188 {
189 if( n==0 ) return 0;
190 scope(exit)
191 { printf("%d",n);
192 r9 ~= cast(char)(n + '0');
193 }
194 return scp(n-1);
195 }
196
197 void test9()
198 {
199 scp(5);
200 assert(r9 == "12345");
201 }
202
203 /********************************************/
204
205 alias real T;
206
207 T readMessageBegin() { return 3.0; }
208
209 T bar10() { return 8.0; }
210
211 T foo10() {
212 // Send RPC request, etc.
213 readMessageBegin();
214 scope (exit) readMessageEnd();
215
216 T result = bar10();
217 // Read message off the wire.
218 return result;
219 }
220
221 void test10()
222 {
223 if (foo10() != 8.0)
224 assert(0);
225 }
226
227 T readMessageEnd() {
228 static T d;
229 d = 4.0;
230 d = (((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d)))))/((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d)))))+((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d)))))/((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))))*(((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d)))))/((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d)))))+((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d)))))/((((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))+(((d-(2*d))+(d-(2*d)))*((d-(2*d))+(d-(2*d))))));
231 return 4.0;
232 }
233
234 /********************************************/
235
236 void test7435() {
237 scope(failure)
238 debug printf("error\n");
239
240 printf("do something\n");
241 }
242
243 /********************************************/
244
245 void test7049() @safe
246 {
247 int count = 0;
248 @safe void foo()
249 {
250 scope (failure) { count++; }
251 scope (failure) { count++; }
252 throw new Exception("failed");
253 }
254
255 try {
256 foo();
257 } catch(Exception e) {
258 }
259 assert(count == 2);
260 }
261
262 /********************************************/
263
264 // https://issues.dlang.org/show_bug.cgi?id=16747
265
266 void test16747() @safe
267 {
268 scope o = new Object();
269 }
270
271 /********************************************/
272
273 void bar11(int*, int*) { }
274
275 void test11()
276 {
277 static int* p;
278 static int i;
279 bar11(p, &i);
280 }
281
282 /********************************************/
283 // https://issues.dlang.org/show_bug.cgi?id=17432
284
285 int test17432(scope int delegate() dg)
286 {
287 return dg();
288 }
289
290 // stripped down version of std.traits.Parameters
291 template Parameters(alias func)
292 {
293 static if (is(typeof(func) P == function))
294 alias Parameters = P;
295 else
296 static assert(0, "unsupported");
297 }
298
299 alias op = Parameters!(test17432)[0];
300 enum typeString = op.stringof;
301 static assert(typeString == "int delegate()"); // no scope added?
302 mixin(typeString ~ " dg;");
303 alias ty = typeof(dg);
304
305 static assert(op.stringof == ty.stringof);
306 static assert(op.mangleof == ty.mangleof);
307
308 void test17432_2()(scope void delegate () dg) { dg(); }
309
310 static assert(typeof(&test17432_2!()).stringof == "void function(scope void delegate() dg) @system");
311
312 /********************************************/
313
314 byte typify13(T)(byte val) { return val; }
315 alias INT8_C13 = typify13!byte;
316
317 /********************************************/
318
319 template test14(T)
320 {
321 alias test14 = int;
322 }
323
324 test14!(char[] function(return char[])) x14;
325
326 /********************************************/
327 // https://issues.dlang.org/show_bug.cgi?id=17935
328
329 struct ByChunk(IO)
330 {
331 @safe:
332 ~this() scope
333 {}
334
335 ubyte[] buf;
336 IO io;
337 }
338
339 struct IO
340 {
341 ~this() @safe @nogc scope
342 {}
343 }
344
345 @safe @nogc void test17395()
346 {
347 ubyte[256] buf;
348 auto chunks = ByChunk!IO(buf[], IO());
349 chunks.__xdtor(); // auto-generated inclusive (fields and struct) dtor
350 }
351
352 /********************************************/
353 // https://issues.dlang.org/show_bug.cgi?id=20569
354
355 void test20569() @safe
356 {
357 static struct S
358 {
359 int value;
360 int* pointer;
361 }
362
363 /* explicit `scope`: */
364 scope S s1;
365 scope int* p1 = &s1.value;
366
367 /* inferred `scope`: */
368 int x;
369 S s2 = S(0, &x);
370 int* p2 = &s2.value;
371 }
372
373 /********************************************/
374
375 void main()
376 {
377 test1();
378 test2();
379 test3();
380 test4();
381 test5();
382 test6();
383 test7();
384 test8();
385 test9();
386 test10();
387 test7435();
388 test7049();
389 test16747();
390 test11();
391 test17395();
392 test20569();
393
394 printf("Success\n");
395 }