]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/testscope.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / testscope.d
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 char[] dup12()(char[] a) // although inferred pure, don't infer a is 'return'
246 {
247 char[] res;
248 foreach (ref e; a)
249 {}
250 return res;
251 }
252
253 char[] foo12()
254 {
255 char[10] buf;
256 return dup12(buf);
257 }
258
259 /********************************************/
260
261 void test7049() @safe
262 {
263 int count = 0;
264 @safe void foo()
265 {
266 scope (failure) { count++; }
267 scope (failure) { count++; }
268 throw new Exception("failed");
269 }
270
271 try {
272 foo();
273 } catch(Exception e) {
274 }
275 assert(count == 2);
276 }
277
278 /********************************************/
279
280 // https://issues.dlang.org/show_bug.cgi?id=16747
281
282 void test16747() @safe
283 {
284 scope o = new Object();
285 }
286
287 /********************************************/
288
289 void bar11(int*, int*) { }
290
291 void test11()
292 {
293 static int* p;
294 static int i;
295 bar11(p, &i);
296 }
297
298 /********************************************/
299 // https://issues.dlang.org/show_bug.cgi?id=17432
300
301 int test17432(scope int delegate() dg)
302 {
303 return dg();
304 }
305
306 // stripped down version of std.traits.Parameters
307 template Parameters(alias func)
308 {
309 static if (is(typeof(func) P == function))
310 alias Parameters = P;
311 else
312 static assert(0, "unsupported");
313 }
314
315 alias op = Parameters!(test17432)[0];
316 enum typeString = op.stringof;
317 static assert(typeString == "int delegate()"); // no scope added?
318 mixin(typeString ~ " dg;");
319 alias ty = typeof(dg);
320
321 static assert(op.stringof == ty.stringof);
322 static assert(op.mangleof == ty.mangleof);
323
324 void test17432_2()(scope void delegate () dg) { dg(); }
325
326 static assert(typeof(&test17432_2!()).stringof == "void function(scope void delegate() dg) @system");
327
328 /********************************************/
329
330 byte typify13(T)(byte val) { return val; }
331 alias INT8_C13 = typify13!byte;
332
333 /********************************************/
334
335 template test14(T)
336 {
337 alias test14 = int;
338 }
339
340 test14!(char[] function(return char[])) x14;
341
342 /********************************************/
343 // https://issues.dlang.org/show_bug.cgi?id=17935
344
345 struct ByChunk(IO)
346 {
347 @safe:
348 ~this() scope
349 {}
350
351 ubyte[] buf;
352 IO io;
353 }
354
355 struct IO
356 {
357 ~this() @safe @nogc scope
358 {}
359 }
360
361 @safe @nogc void test17395()
362 {
363 ubyte[256] buf;
364 auto chunks = ByChunk!IO(buf[], IO());
365 chunks.__xdtor(); // auto-generated inclusive (fields and struct) dtor
366 }
367
368 /********************************************/
369 // https://issues.dlang.org/show_bug.cgi?id=20569
370
371 void test20569() @safe
372 {
373 static struct S
374 {
375 int value;
376 int* pointer;
377 }
378
379 /* explicit `scope`: */
380 scope S s1;
381 scope int* p1 = &s1.value;
382
383 /* inferred `scope`: */
384 int x;
385 S s2 = S(0, &x);
386 int* p2 = &s2.value;
387 }
388
389 /********************************************/
390
391 void main()
392 {
393 test1();
394 test2();
395 test3();
396 test4();
397 test5();
398 test6();
399 test7();
400 test8();
401 test9();
402 test10();
403 test7435();
404 test7049();
405 test16747();
406 test11();
407 test17395();
408 test20569();
409
410 printf("Success\n");
411 }