1 // PERMUTE_ARGS:
2 // REQUIRED_ARGS: -d -dip1000
3 
4 extern(C) int printf(const char*, ...);
5 
6 class Eh : Exception
7 {
this()8     this()
9     {
10         super("Eh thrown");
11     }
12 }
13 
14 
15 /********************************************/
16 
17 class Foo
18 {
19     static int x;
20 
this()21     this()
22     {
23         assert(x == 0);
24         x++;
25         printf("Foo.this()\n");
26         throw new Eh();
27         assert(0);
28     }
29 
~this()30     ~this()
31     {
32         printf("Foo.~this()\n");
33     }
34 }
35 
test1()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 
test2()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 
test3()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 
test4()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 
test5()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 
test6()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 
test7()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 
test8()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 
scp(int n)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 
test9()197 void test9()
198 {
199     scp(5);
200     assert(r9 == "12345");
201 }
202 
203 /********************************************/
204 
205 alias real T;
206 
readMessageBegin()207 T readMessageBegin() { return 3.0; }
208 
bar10()209 T bar10() { return 8.0; }
210 
foo10()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 
test10()221 void test10()
222 {
223     if (foo10() != 8.0)
224         assert(0);
225 }
226 
readMessageEnd()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 
test7435()236 void test7435() {
237   scope(failure)
238     debug printf("error\n");
239 
240   printf("do something\n");
241 }
242 
243 /********************************************/
244 
dup12()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 
foo12()253 char[] foo12()
254 {
255     char[10] buf;
256     return dup12(buf);
257 }
258 
259 /********************************************/
260 
test7049()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 
test16747()282 void test16747() @safe
283 {
284     scope o = new Object();
285 }
286 
287 /********************************************/
288 
bar11(int *,int *)289 void bar11(int*, int*) { }
290 
test11()291 void test11()
292 {
293     static int* p;
294     static int i;
295     bar11(p, &i);
296 
297     bar11((i,p), &i);  // comma expressions are deprecated, but need to test them
298 }
299 
300 /********************************************/
301 // https://issues.dlang.org/show_bug.cgi?id=17432
302 
test17432(scope int delegate ()dg)303 int test17432(scope int delegate() dg)
304 {
305 	return dg();
306 }
307 
308 // stripped down version of std.traits.Parameters
Parameters(alias func)309 template Parameters(alias func)
310 {
311     static if (is(typeof(func) P == function))
312         alias Parameters = P;
313     else
314         static assert(0, "unsupported");
315 }
316 
317 alias op = Parameters!(test17432)[0];
318 enum typeString = op.stringof;
319 static assert(typeString == "int delegate()"); // no scope added?
320 mixin(typeString ~ " dg;");
321 alias ty = typeof(dg);
322 
323 static assert(op.stringof == ty.stringof);
324 static assert(op.mangleof == ty.mangleof);
325 
test17432_2()326 void test17432_2()(scope void delegate () dg) { dg(); }
327 
328 static assert(typeof(&test17432_2!()).stringof == "void function(scope void delegate() dg) @system");
329 
330 /********************************************/
331 
typify13(T)332 byte typify13(T)(byte val) { return val; }
333 alias INT8_C13  = typify13!byte;
334 
335 /********************************************/
336 
test14(T)337 template test14(T)
338 {
339     alias test14 = int;
340 }
341 
342 test14!(char[] function(return char[])) x14;
343 
344 /********************************************/
345 
main()346 void main()
347 {
348     test1();
349     test2();
350     test3();
351     test4();
352     test5();
353     test6();
354     test7();
355     test8();
356     test9();
357     test10();
358     test7435();
359     test7049();
360     test16747();
361     test11();
362 
363     printf("Success\n");
364 }
365