]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/testsuite/gdc.test/runnable/funclit.d
d: Import dmd b8384668f, druntime e6caaab9, phobos 5ab9ad256 (v2.098.0-beta.1)
[thirdparty/gcc.git] / gcc / testsuite / gdc.test / runnable / funclit.d
1 /*
2 TEST_OUTPUT:
3 ---
4 int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe
5 int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe
6 int
7 int
8 int[]
9 int delegate() pure nothrow @nogc @safe function() pure nothrow @safe
10 ---
11
12 RUN_OUTPUT:
13 ---
14 Success
15 ---
16 */
17 import core.vararg;
18
19 extern (C) int printf(const char*, ...);
20
21 /***************************************************/
22 // lambda syntax check
23
24 auto una(alias dg)(int n)
25 {
26 return dg(n);
27 }
28 auto bin(alias dg)(int n, int m)
29 {
30 return dg(n, m);
31 }
32 void test1()
33 {
34 assert(una!( a => a*2 )(2) == 4);
35 assert(una!( ( a) => a*2 )(2) == 4);
36 assert(una!( (int a) => a*2 )(2) == 4);
37 assert(una!( ( a){ return a*2; } )(2) == 4);
38 assert(una!( function ( a){ return a*2; } )(2) == 4);
39 assert(una!( function int( a){ return a*2; } )(2) == 4);
40 assert(una!( function (int a){ return a*2; } )(2) == 4);
41 assert(una!( function int(int a){ return a*2; } )(2) == 4);
42 assert(una!( delegate ( a){ return a*2; } )(2) == 4);
43 assert(una!( delegate int( a){ return a*2; } )(2) == 4);
44 assert(una!( delegate (int a){ return a*2; } )(2) == 4);
45 assert(una!( delegate int(int a){ return a*2; } )(2) == 4);
46
47 // partial parameter specialization syntax
48 assert(bin!( ( a, b) => a*2+b )(2,1) == 5);
49 assert(bin!( (int a, b) => a*2+b )(2,1) == 5);
50 assert(bin!( ( a, int b) => a*2+b )(2,1) == 5);
51 assert(bin!( (int a, int b) => a*2+b )(2,1) == 5);
52 assert(bin!( ( a, b){ return a*2+b; } )(2,1) == 5);
53 assert(bin!( (int a, b){ return a*2+b; } )(2,1) == 5);
54 assert(bin!( ( a, int b){ return a*2+b; } )(2,1) == 5);
55 assert(bin!( (int a, int b){ return a*2+b; } )(2,1) == 5);
56 assert(bin!( function ( a, b){ return a*2+b; } )(2,1) == 5);
57 assert(bin!( function (int a, b){ return a*2+b; } )(2,1) == 5);
58 assert(bin!( function ( a, int b){ return a*2+b; } )(2,1) == 5);
59 assert(bin!( function (int a, int b){ return a*2+b; } )(2,1) == 5);
60 assert(bin!( function int( a, b){ return a*2+b; } )(2,1) == 5);
61 assert(bin!( function int(int a, b){ return a*2+b; } )(2,1) == 5);
62 assert(bin!( function int( a, int b){ return a*2+b; } )(2,1) == 5);
63 assert(bin!( function int(int a, int b){ return a*2+b; } )(2,1) == 5);
64 assert(bin!( delegate ( a, b){ return a*2+b; } )(2,1) == 5);
65 assert(bin!( delegate (int a, b){ return a*2+b; } )(2,1) == 5);
66 assert(bin!( delegate ( a, int b){ return a*2+b; } )(2,1) == 5);
67 assert(bin!( delegate (int a, int b){ return a*2+b; } )(2,1) == 5);
68 assert(bin!( delegate int( a, b){ return a*2+b; } )(2,1) == 5);
69 assert(bin!( delegate int(int a, b){ return a*2+b; } )(2,1) == 5);
70 assert(bin!( delegate int( a, int b){ return a*2+b; } )(2,1) == 5);
71 assert(bin!( delegate int(int a, int b){ return a*2+b; } )(2,1) == 5);
72 }
73
74 /***************************************************/
75 // on initializer
76
77 void test2()
78 {
79 // explicit typed binding ignite parameter types inference
80 int function(int) fn1 = a => a*2; assert(fn1(2) == 4);
81 int function(int) fn2 = ( a){ return a*2; }; assert(fn2(2) == 4);
82 int function(int) fn3 = function ( a){ return a*2; }; assert(fn3(2) == 4);
83 int function(int) fn4 = function int( a){ return a*2; }; assert(fn4(2) == 4);
84 int function(int) fn5 = function (int a){ return a*2; }; assert(fn5(2) == 4);
85 int function(int) fn6 = function int(int a){ return a*2; }; assert(fn6(2) == 4);
86 int delegate(int) dg1 = a => a*2; assert(dg1(2) == 4);
87 int delegate(int) dg2 = ( a){ return a*2; }; assert(dg2(2) == 4);
88 int delegate(int) dg3 = delegate ( a){ return a*2; }; assert(dg3(2) == 4);
89 int delegate(int) dg4 = delegate int( a){ return a*2; }; assert(dg4(2) == 4);
90 int delegate(int) dg5 = delegate (int a){ return a*2; }; assert(dg5(2) == 4);
91 int delegate(int) dg6 = delegate int(int a){ return a*2; }; assert(dg6(2) == 4);
92
93 // function/delegate mismatching always raises an error
94 static assert(!__traits(compiles, { int function(int) xfg3 = delegate ( a){ return a*2; }; }));
95 static assert(!__traits(compiles, { int function(int) xfg4 = delegate int( a){ return a*2; }; }));
96 static assert(!__traits(compiles, { int function(int) xfg5 = delegate (int a){ return a*2; }; }));
97 static assert(!__traits(compiles, { int function(int) xfg6 = delegate int(int a){ return a*2; }; }));
98 static assert(!__traits(compiles, { int delegate(int) xdn3 = function ( a){ return a*2; }; }));
99 static assert(!__traits(compiles, { int delegate(int) xdn4 = function int( a){ return a*2; }; }));
100 static assert(!__traits(compiles, { int delegate(int) xdn5 = function (int a){ return a*2; }; }));
101 static assert(!__traits(compiles, { int delegate(int) xdn6 = function int(int a){ return a*2; }; }));
102
103 // auto binding requires explicit parameter types at least
104 static assert(!__traits(compiles, { auto afn1 = a => a*2; }));
105 static assert(!__traits(compiles, { auto afn2 = ( a){ return a*2; }; }));
106 static assert(!__traits(compiles, { auto afn3 = function ( a){ return a*2; }; }));
107 static assert(!__traits(compiles, { auto afn4 = function int( a){ return a*2; }; }));
108 static assert(!__traits(compiles, { auto adg3 = delegate ( a){ return a*2; }; }));
109 static assert(!__traits(compiles, { auto adg4 = delegate int( a){ return a*2; }; }));
110 auto afn5 = function (int a){ return a*2; }; assert(afn5(2) == 4);
111 auto afn6 = function int(int a){ return a*2; }; assert(afn6(2) == 4);
112 auto adg5 = delegate (int a){ return a*2; }; assert(adg5(2) == 4);
113 auto adg6 = delegate int(int a){ return a*2; }; assert(adg6(2) == 4);
114
115 // partial specialized lambda
116 string delegate(int, string) dg =
117 (n, string s){
118 string r = "";
119 foreach (_; 0..n) r~=s;
120 return r;
121 };
122 assert(dg(2, "str") == "strstr");
123 }
124
125 /***************************************************/
126 // on return statement
127
128 void test3()
129 {
130 // inference matching system is same as on initializer
131 int delegate(int) mul(int x)
132 {
133 return a => a * x;
134 }
135 assert(mul(5)(2) == 10);
136 }
137
138 /***************************************************/
139 // on function arguments
140
141 auto foo4(int delegate(int) dg) { return dg(10); }
142 auto foo4(int delegate(int, int) dg) { return dg(10, 20); }
143
144 void nbar4fp(void function(int) fp) { }
145 void nbar4dg(void delegate(int) dg) { }
146 void tbar4fp(T,R)(R function(T) dg) { static assert(is(typeof(dg) == void function(int))); }
147 void tbar4dg(T,R)(R delegate(T) dg) { static assert(is(typeof(dg) == void delegate(int))); }
148
149 auto nbaz4(void function() fp) { return 1; }
150 auto nbaz4(void delegate() dg) { return 2; }
151 auto tbaz4(R)(R function() dg) { static assert(is(R == void)); return 1; }
152 auto tbaz4(R)(R delegate() dg) { static assert(is(R == void)); return 2; }
153
154 auto thoo4(T)(T lambda){ return lambda; }
155
156 void tfun4a()(int function(int) a){}
157 void tfun4b(T)(T function(T) a){}
158 void tfun4c(T)(T f){}
159
160 void test4()
161 {
162 int v;
163 static void sfoo() {}
164 void nfoo() {}
165
166 // parameter type inference + overload resolution
167 assert(foo4((a) => a * 2) == 20);
168 assert(foo4((a,b) => a * 2 + b) == 40);
169
170 // function/delegate inference
171 nbar4fp((int x){ });
172 nbar4dg((int x){ });
173 tbar4fp((int x){ });
174 tbar4dg((int x){ });
175
176 // function/delegate inference + overload resolution
177 assert(nbaz4({ }) == 1);
178 assert(nbaz4({ v = 1; }) == 2);
179 assert(nbaz4({ sfoo(); }) == 1); // https://issues.dlang.org/show_bug.cgi?id=8836
180 assert(nbaz4({ nfoo(); }) == 2);
181
182 assert(tbaz4({ }) == 1);
183 assert(tbaz4({ v = 1; }) == 2);
184 assert(tbaz4({ sfoo(); }) == 1);
185 assert(tbaz4({ nfoo(); }) == 2);
186
187 // template function deduction
188 static assert(is(typeof(thoo4({ })) : void function()));
189 static assert(is(typeof(thoo4({ v = 1; })) : void delegate()));
190
191 tfun4a(a => a);
192 static assert(!__traits(compiles, { tfun4b(a => a); }));
193 static assert(!__traits(compiles, { tfun4c(a => a); }));
194 }
195
196 void fsvarg4(int function(int)[] a...){}
197 void fcvarg4(int dummy, ...){}
198
199 void tsvarg4a()(int function(int)[] a...){}
200 void tsvarg4b(T)(T function(T)[] a...){}
201 void tsvarg4c(T)(T [] a...){}
202 void tcvarg4()(int dummy, ...){}
203
204 void test4v()
205 {
206 fsvarg4(function(int a){ return a; }); // OK
207 fsvarg4(a => a); // OK
208
209 fcvarg4(0, function(int a){ return a; }); // OK
210 static assert(!__traits(compiles, { fcvarg4(0, a => a); }));
211
212 tsvarg4a(function(int a){ return a; }); // OK
213 tsvarg4b(function(int a){ return a; }); // OK
214 tsvarg4c(function(int a){ return a; }); // OK
215 tsvarg4a(a => a);
216 static assert(!__traits(compiles, { tsvarg4b(a => a); }));
217 static assert(!__traits(compiles, { tsvarg4c(a => a); }));
218
219 tcvarg4(0, function(int a){ return a; }); // OK
220 static assert(!__traits(compiles, { tcvarg4(0, a => a); }));
221 }
222
223 // A lambda in function default argument should be deduced to delegate, by the
224 // preparation inferType call in TypeFunction.semantic.
225 void test4_findRoot(scope bool delegate(real lo, real hi) tolerance = (real a, real b) => false)
226 {}
227
228 /***************************************************/
229 // on CallExp::e1
230
231 void test5()
232 {
233 assert((a => a*2)(10) == 20);
234 assert(( a, s){ return s~s; }(10, "str") == "strstr");
235 assert((int a, s){ return s~s; }(10, "str") == "strstr");
236 assert(( a, string s){ return s~s; }(10, "str") == "strstr");
237 assert((int a, string s){ return s~s; }(10, "str") == "strstr");
238 }
239
240 /***************************************************/
241 // escape check to nested function symbols
242
243 void checkNestedRef(alias dg)(bool isnested)
244 {
245 static if (is(typeof(dg) == delegate))
246 enum isNested = true;
247 else static if ((is(typeof(dg) PF == F*, F) && is(F == function)))
248 enum isNested = false;
249 else
250 static assert(0);
251
252 assert(isnested == isNested);
253 dg();
254 }
255
256 void freeFunc(){}
257
258 void test6()
259 {
260 static void localFunc(){}
261 void nestedLocalFunc(){}
262
263 checkNestedRef!({ })(false);
264
265 checkNestedRef!({ freeFunc(); })(false);
266 checkNestedRef!({ localFunc(); })(false);
267 checkNestedRef!({ nestedLocalFunc(); })(true);
268 checkNestedRef!({ void inner(){} inner(); })(false);
269
270 checkNestedRef!({ auto f = &freeFunc; })(false);
271 checkNestedRef!({ auto f = &localFunc; })(false);
272 checkNestedRef!({ auto f = &nestedLocalFunc; })(true);
273 checkNestedRef!({ void inner(){} auto f = &inner; })(false);
274 }
275
276 /***************************************************/
277 // on AssignExp::e2
278
279 void test7()
280 {
281 int function(int) fp;
282 fp = a => a;
283 fp = (int a) => a;
284 fp = function(int a) => a;
285 fp = function int(int a) => a;
286 static assert(!__traits(compiles, { fp = delegate(int a) => a; }));
287 static assert(!__traits(compiles, { fp = delegate int(int a) => a; }));
288
289 int delegate(int) dg;
290 dg = a => a;
291 dg = (int a) => a;
292 dg = delegate(int a) => a;
293 dg = delegate int(int a) => a;
294 static assert(!__traits(compiles, { dg = function(int a) => a; }));
295 static assert(!__traits(compiles, { dg = function int(int a) => a; }));
296 }
297
298 /***************************************************/
299 // on StructLiteralExp::elements
300
301 void test8()
302 {
303 struct S
304 {
305 int function(int) fp;
306 }
307 auto s1 = S(a => a);
308 static assert(!__traits(compiles, { auto s2 = S((a, b) => a); }));
309 }
310
311 /***************************************************/
312 // on concat operation
313
314 void test9()
315 {
316 int function(int)[] a2;
317 a2 ~= x => x;
318 }
319
320 /***************************************************/
321 // on associative array key
322
323 void test10()
324 {
325 int[int function()] aa;
326 assert(!aa.remove(() => 1));
327
328 int[int function(int)] aa2;
329 assert(!aa2.remove(x => 1));
330 }
331
332 /***************************************************/
333 // on common type deduction
334
335 void test11()
336 {
337 auto a1 = [x => x, (int x) => x * 2];
338 static assert(is(typeof(a1[0]) == int function(int) pure @safe nothrow @nogc));
339 assert(a1[0](10) == 10);
340 assert(a1[1](10) == 20);
341
342 //int n = 10;
343 //auto a2 = [x => n, (int x) => x * 2];
344 //static assert(is(typeof(a2[0]) == int delegate(int) @safe nothrow));
345 //assert(a2[0](99) == 10);
346 //assert(a2[1](10) == 20);
347
348 int function(int) fp = true ? (x => x) : (x => x*2);
349 assert(fp(10) == 10);
350
351 int m = 10;
352 int delegate(int) dg = true ? (x => x) : (x => m*2);
353 assert(dg(10) == 10);
354 }
355
356 /***************************************************/
357 // https://issues.dlang.org/show_bug.cgi?id=3235
358
359 void test3235()
360 {
361 // from TDPL
362 auto f = (int i) {};
363 static if (is(typeof(f) _ == F*, F) && is(F == function))
364 {} else static assert(0);
365 }
366
367 /***************************************************/
368 // https://issues.dlang.org/show_bug.cgi?id=6714
369
370 void foo6714x(int function (int, int) a){}
371 void bar6714x(int delegate (int, int) a){}
372
373 int bar6714y(double delegate(int, int) a){ return 1; }
374 int bar6714y( int delegate(int, int) a){ return 2; }
375
376 void test6714()
377 {
378 foo6714x((a, b) { return a + b; });
379 bar6714x((a, b) { return a + b; });
380
381 assert(bar6714y((a, b){ return 1.0; }) == 1);
382 assert(bar6714y((a, b){ return 1.0f; }) == 1);
383 assert(bar6714y((a, b){ return a; }) == 2);
384 }
385
386 /***************************************************/
387 // https://issues.dlang.org/show_bug.cgi?id=7193
388
389 void test7193()
390 {
391 static assert(!__traits(compiles, {
392 delete a => a;
393 }));
394 }
395
396 /***************************************************/
397 // https://issues.dlang.org/show_bug.cgi?id=7207
398 // on CastExp
399
400 void test7202()
401 {
402 auto dg = cast(int function(int))(a => a);
403 assert(dg(10) == 10);
404 }
405
406 /***************************************************/
407 // https://issues.dlang.org/show_bug.cgi?id=7288
408
409 void test7288()
410 {
411 // 7288 -> OK
412 auto foo()
413 {
414 int x;
415 return () { return () => x; };
416 }
417 pragma(msg, typeof(&foo));
418 alias int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe Dg;
419 pragma(msg, Dg);
420 static assert(is(typeof(&foo) == Dg)); // should pass
421 }
422
423 /***************************************************/
424 // https://issues.dlang.org/show_bug.cgi?id=7499
425
426 void test7499()
427 {
428 int function(int)[] a1 = [ x => x ]; // 7499
429 int function(int)[][] a2 = [[x => x]]; // +a
430 assert(a1[0] (10) == 10);
431 assert(a2[0][0](10) == 10);
432 }
433
434 /***************************************************/
435 // https://issues.dlang.org/show_bug.cgi?id=7500
436
437 void test7500()
438 {
439 alias immutable bool function(int[]) Foo;
440 Foo f = a => true;
441 }
442
443 /***************************************************/
444 // https://issues.dlang.org/show_bug.cgi?id=7525
445
446 void test7525()
447 {
448 {
449 char[] delegate() a = { return null; };
450 int delegate() b = { return 1U; };
451 uint delegate() c = { return 1; };
452 float delegate() d = { return 1.0; };
453 double delegate() e = { return 1.0f; };
454 }
455
456 {
457 char[] delegate(int) a = (x){ return null; };
458 int delegate(int) b = (x){ return 1U; };
459 uint delegate(int) c = (x){ return 1; };
460 float delegate(int) d = (x){ return 1.0; };
461 double delegate(int) e = (x){ return 1.0f; };
462 }
463 }
464
465 /***************************************************/
466 // https://issues.dlang.org/show_bug.cgi?id=7582
467
468 void test7582()
469 {
470 void delegate(int) foo;
471 void delegate(int) foo2;
472 foo = (a) {
473 foo2 = (b) { };
474 };
475 }
476
477 /***************************************************/
478 // https://issues.dlang.org/show_bug.cgi?id=7649
479
480 void test7649()
481 {
482 void foo(int function(int) fp = x => 1)
483 {
484 assert(fp(1) == 1);
485 }
486 foo();
487 }
488
489 /***************************************************/
490 // https://issues.dlang.org/show_bug.cgi?id=7650
491
492 void test7650()
493 {
494 int[int function(int)] aa1 = [x=>x:1, x=>x*2:2];
495 foreach (k, v; aa1) {
496 if (v == 1) assert(k(10) == 10);
497 if (v == 2) assert(k(10) == 20);
498 }
499
500 int function(int)[int] aa2 = [1:x=>x, 2:x=>x*2];
501 assert(aa2[1](10) == 10);
502 assert(aa2[2](10) == 20);
503
504 int n = 10;
505 int[int delegate(int)] aa3 = [x=>n+x:1, x=>n+x*2:2];
506 foreach (k, v; aa3) {
507 if (v == 1) assert(k(10) == 20);
508 if (v == 2) assert(k(10) == 30);
509 }
510
511 int delegate(int)[int] aa4 = [1:x=>n+x, 2:x=>n+x*2];
512 assert(aa4[1](10) == 20);
513 assert(aa4[2](10) == 30);
514 }
515
516 /***************************************************/
517 // https://issues.dlang.org/show_bug.cgi?id=7705
518
519 void test7705()
520 {
521 void foo1(void delegate(ref int ) dg){ int x=10; dg(x); }
522 foo1((ref x){ pragma(msg, typeof(x)); assert(x == 10); });
523 static assert(!__traits(compiles, foo1((x){}) ));
524
525 void foo2(void delegate(int, ...) dg){ dg(20, 3.14); }
526 foo2((x,...){ pragma(msg, typeof(x)); assert(x == 20); });
527
528 void foo3(void delegate(int[]...) dg){ dg(1, 2, 3); }
529 foo3((x ...){ pragma(msg, typeof(x)); assert(x == [1,2,3]); });
530 }
531
532 /***************************************************/
533 // https://issues.dlang.org/show_bug.cgi?id=7713
534
535 void foo7713(T)(T delegate(in Object) dlg)
536 {}
537 void test7713()
538 {
539 foo7713( (in obj) { return 15; } ); // line 6
540 }
541
542 /***************************************************/
543 // https://issues.dlang.org/show_bug.cgi?id=7743
544
545 auto foo7743a()
546 {
547 int x = 10;
548 return () nothrow {
549 return x;
550 };
551 }
552 auto foo7743b()
553 {
554 int x = 10;
555 return () nothrow => x;
556 }
557 void test7743()
558 {
559 pragma(msg, typeof(&foo7743a));
560 static assert(is(typeof(&foo7743a) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
561 assert(foo7743a()() == 10);
562
563 static assert(is(typeof(&foo7743b) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
564 assert(foo7743b()() == 10);
565 }
566
567 /***************************************************/
568 // https://issues.dlang.org/show_bug.cgi?id=7761
569
570 enum dg7761 = (int a) pure => 2 * a;
571
572 void test7761()
573 {
574 static assert(is(typeof(dg7761) == int function(int) pure @safe nothrow @nogc));
575 assert(dg7761(10) == 20);
576 }
577
578 /***************************************************/
579 // https://issues.dlang.org/show_bug.cgi?id=7941
580
581 void test7941()
582 {
583 static assert(!__traits(compiles, { enum int c = function(){}; }));
584 }
585
586 /***************************************************/
587 // https://issues.dlang.org/show_bug.cgi?id=8005
588
589 void test8005()
590 {
591 auto n = (a, int n = 2){ return n; }(1);
592 assert(n == 2);
593 }
594
595 /***************************************************/
596 // test8198
597
598 void test8198()
599 {
600 T delegate(T) zero(T)(T delegate(T) f)
601 {
602 return x => x;
603 }
604
605 T delegate(T) delegate(T delegate(T)) succ(T)(T delegate(T) delegate(T delegate(T)) n)
606 {
607 return f => x => f(n(f)(x));
608 }
609
610 uint delegate(uint) delegate(uint delegate(uint)) n = &zero!uint;
611 foreach (i; 0..10)
612 {
613 assert(n(x => x + 1)(0) == i);
614 n = succ(n);
615 }
616 }
617
618 /***************************************************/
619 // https://issues.dlang.org/show_bug.cgi?id=8226
620
621 immutable f8226 = (int x) => x * 2;
622
623 void test8226()
624 {
625 assert(f8226(10) == 20);
626 }
627
628 /***************************************************/
629 // https://issues.dlang.org/show_bug.cgi?id=8241
630
631 auto exec8241a(alias a = function(x) => x, T...)(T as)
632 {
633 return a(as);
634 }
635
636 auto exec8241b(alias a = (x) => x, T...)(T as)
637 {
638 return a(as);
639 }
640
641 void test8241()
642 {
643 exec8241a(2);
644 exec8241b(2);
645 }
646
647 /***************************************************/
648 // https://issues.dlang.org/show_bug.cgi?id=8242
649
650 template exec8242(alias a, T...)
651 {
652 auto func8242(T as)
653 {
654 return a(as);
655 }
656 }
657
658 mixin exec8242!(x => x, int);
659 mixin exec8242!((string x) => x, string);
660
661 void test8242()
662 {
663 func8242(1);
664 func8242("");
665 }
666
667 /***************************************************/
668 // https://issues.dlang.org/show_bug.cgi?id=8315
669
670 void test8315()
671 {
672 bool b;
673 foo8315!(a => b)();
674 }
675
676 void foo8315(alias pred)()
677 if (is(typeof(pred(1)) == bool))
678 {}
679
680 /***************************************************/
681 // https://issues.dlang.org/show_bug.cgi?id=8397
682
683 void test8397()
684 {
685 void function(int) f;
686 static assert(!is(typeof({
687 f = function(string x) {};
688 })));
689 }
690
691 /***************************************************/
692 // https://issues.dlang.org/show_bug.cgi?id=8496
693
694 void test8496()
695 {
696 alias extern (C) void function() Func;
697
698 Func fp = (){};
699
700 fp = (){};
701 }
702
703 /***************************************************/
704 // https://issues.dlang.org/show_bug.cgi?id=8575
705
706 template tfunc8575(func...)
707 {
708 auto tfunc8575(U)(U u) { return func[0](u); }
709 }
710 auto bar8575(T)(T t)
711 {
712 return tfunc8575!(a => a)(t);
713 }
714 void foo8575a() { assert(bar8575(uint.init + 1) == +1); }
715 void foo8575b() { assert(bar8575( int.init - 1) == -1); }
716
717 void test8575()
718 {
719 foo8575a();
720 foo8575b();
721 }
722
723 /***************************************************/
724 // https://issues.dlang.org/show_bug.cgi?id=9153
725
726 void writeln9153(string s){}
727
728 void test9153()
729 {
730 auto tbl1 = [
731 (string x) { writeln9153(x); },
732 (string x) { x ~= 'a'; },
733 ];
734 auto tbl2 = [
735 (string x) { x ~= 'a'; },
736 (string x) { writeln9153(x); },
737 ];
738 }
739
740 /***************************************************/
741 // https://issues.dlang.org/show_bug.cgi?id=9393
742
743 template ifThrown9393a(E)
744 {
745 void ifThrown9393a(T)(scope T delegate(E) errHandler)
746 {
747 }
748 }
749 void ifThrown9393b(E, T)(scope T delegate(E) errHandler)
750 {
751 }
752
753 void foo9393(T)(void delegate(T) dg){ dg(T.init); }
754 void foo9393()(void delegate(int) dg){ foo9393!int(dg); }
755
756 void test9393()
757 {
758 ifThrown9393a!Exception(e => 10);
759 ifThrown9393b!Exception(e => 10);
760
761 foo9393((x){ assert(x == int.init); });
762 }
763
764 /***************************************************/
765 // https://issues.dlang.org/show_bug.cgi?id=9415
766
767 void test9415()
768 {
769 int z;
770 typeof((int a){return z;}) dg;
771 dg = (int a){return z;};
772 }
773
774 /***************************************************/
775 // https://issues.dlang.org/show_bug.cgi?id=9628
776
777 template TypeTuple9628(TL...) { alias TypeTuple9628 = TL; }
778 void map9628(alias func)() { func(0); }
779
780 void test9628()
781 {
782 auto items = [[10, 20], [30]];
783 size_t[] res;
784
785 res = null;
786 foreach (_; 0 .. 2)
787 {
788 foreach (sub; items)
789 {
790 map9628!(( i){ res ~= sub.length; });
791 map9628!((size_t i){ res ~= sub.length; });
792 }
793 }
794 assert(res == [2,2,1,1, 2,2,1,1]);
795
796 res = null;
797 foreach (_; TypeTuple9628!(0, 1))
798 {
799 foreach (sub; items)
800 {
801 map9628!(( i){ res ~= sub.length; });
802 map9628!((size_t i){ res ~= sub.length; });
803 }
804 }
805 assert(res == [2,2,1,1, 2,2,1,1]);
806 }
807
808 /***************************************************/
809 // https://issues.dlang.org/show_bug.cgi?id=9928
810
811 void test9928()
812 {
813 void* smth = (int x) { return x; };
814 }
815
816 /***************************************************/
817 // https://issues.dlang.org/show_bug.cgi?id=10133
818
819 ptrdiff_t countUntil10133(alias pred, R)(R haystack)
820 {
821 typeof(return) i;
822
823 alias T = dchar;
824
825 foreach (T elem; haystack)
826 {
827 if (pred(elem)) return i;
828 ++i;
829 }
830
831 return -1;
832 }
833
834 bool func10133(string s)() if (countUntil10133!(x => x == 'x')(s) == 1)
835 {
836 return true;
837 }
838
839 bool func10133a(string s)() if (countUntil10133!(x => s == "x")(s) != -1)
840 {
841 return true;
842 }
843 bool func10133b(string s)() if (countUntil10133!(x => s == "x")(s) != -1)
844 {
845 return true;
846 }
847
848 void test10133()
849 {
850 func10133!("ax")();
851
852 func10133a!("x")();
853 static assert(!is(typeof(func10133a!("ax")())));
854 static assert(!is(typeof(func10133b!("ax")())));
855 func10133b!("x")();
856 }
857
858 /***************************************************/
859 // https://issues.dlang.org/show_bug.cgi?id=10219
860
861 void test10219()
862 {
863 interface I { }
864 class C : I { }
865
866 void test_dg(I delegate(C) dg)
867 {
868 C c = new C;
869 void* cptr = cast(void*) c;
870 void* iptr = cast(void*) cast(I) c;
871 void* xptr = cast(void*) dg(c);
872 assert(cptr != iptr);
873 assert(cptr != xptr); // should pass
874 assert(iptr == xptr); // should pass
875 }
876
877 C delegate(C c) dg = delegate C(C c) { return c; };
878 static assert(!__traits(compiles, { test_dg(dg); }));
879 static assert(!__traits(compiles, { test_dg(delegate C(C c) { return c; }); }));
880 static assert(!__traits(compiles, { I delegate(C) dg2 = dg; }));
881
882 // creates I delegate(C)
883 test_dg(c => c);
884 test_dg(delegate(C c) => c);
885
886 void test_fp(I function(C) fp)
887 {
888 C c = new C;
889 void* cptr = cast(void*) c;
890 void* iptr = cast(void*) cast(I) c;
891 void* xptr = cast(void*) fp(c);
892 assert(cptr != iptr);
893 assert(cptr != xptr); // should pass
894 assert(iptr == xptr); // should pass
895 }
896
897 C function(C c) fp = function C(C c) { return c; };
898 static assert(!__traits(compiles, { test_fp(fp); }));
899 static assert(!__traits(compiles, { test_fp(function C(C c) { return c; }); }));
900 static assert(!__traits(compiles, { I function(C) fp2 = fp; }));
901
902 // creates I function(C)
903 test_fp(c => c);
904 test_fp(function(C c) => c);
905 }
906
907 /***************************************************/
908 // https://issues.dlang.org/show_bug.cgi?id=10288
909
910 T foo10288(T)(T x)
911 {
912 void lambda() @trusted nothrow { x += 10; }
913 lambda();
914 return x;
915 }
916
917 T bar10288(T)(T x)
918 {
919 () @trusted { x += 10; } ();
920 return x;
921 }
922
923 T baz10288(T)(T arg)
924 {
925 static int g = 10;
926 () @trusted { x += g; } ();
927 return x;
928 }
929
930 void test10288() @safe pure nothrow
931 {
932 assert(foo10288(10) == 20); // OK
933 assert(bar10288(10) == 20); // OK <- NG
934 static assert(!__traits(compiles, baz10288(10)));
935 }
936
937 /***************************************************/
938 // https://issues.dlang.org/show_bug.cgi?id=10666
939
940 struct S10666
941 {
942 int val;
943 ~this() {}
944 }
945
946 void foo10666(S10666 s1)
947 {
948 S10666 s2;
949
950 /* Even if closureVars(s1 and s2) are accessed by directly called lambdas,
951 * they won't escape the scope of this function.
952 */
953 auto x1 = (){ return s1.val; }(); // OK
954 auto x2 = (){ return s2.val; }(); // OK
955 }
956
957 /***************************************************/
958 // https://issues.dlang.org/show_bug.cgi?id=11081
959
960 T ifThrown11081(E : Throwable, T)(T delegate(E) errorHandler)
961 {
962 return errorHandler();
963 }
964
965 void test11081()
966 {
967 static if (__traits(compiles, ifThrown11081!Exception(e => 0)))
968 {
969 }
970 static if (__traits(compiles, ifThrown11081!Exception(e => 0)))
971 {
972 }
973 }
974
975 /***************************************************/
976 // https://issues.dlang.org/show_bug.cgi?id=11220
977
978 int parsePrimaryExp11220(int x)
979 {
980 parseAmbig11220!( (parsed){ x += 1; } )();
981 return 1;
982 }
983
984 typeof(handler(1)) parseAmbig11220(alias handler)()
985 {
986 return handler(parsePrimaryExp11220(1));
987 }
988
989 /***************************************************/
990 // https://issues.dlang.org/show_bug.cgi?id=11230
991
992 template map11230(fun...)
993 {
994 auto map11230(Range)(Range r)
995 {
996 return MapResult11230!(fun, Range)(r);
997 }
998 }
999
1000 struct MapResult11230(alias fun, R)
1001 {
1002 R _input;
1003 this(R input) { _input = input; }
1004 }
1005
1006 class A11230 { A11230[] as; }
1007 class B11230 { A11230[] as; }
1008 class C11230 : A11230 {}
1009
1010 C11230 visit11230(A11230 a)
1011 {
1012 a.as.map11230!(a => visit11230);
1013 return null;
1014 }
1015 C11230 visit11230(B11230 b)
1016 {
1017 b.as.map11230!(a => visit11230);
1018 return null;
1019 }
1020 C11230 visit11230()
1021 {
1022 return null;
1023 }
1024
1025 /***************************************************/
1026 // https://issues.dlang.org/show_bug.cgi?id=10336
1027
1028 struct S10336
1029 {
1030 template opDispatch(string name)
1031 {
1032 enum opDispatch = function(int x) {
1033 return x;
1034 };
1035 }
1036 }
1037
1038 void test10336()
1039 {
1040 S10336 s;
1041 assert(s.hello(12) == 12);
1042 }
1043
1044 /***************************************************/
1045 // https://issues.dlang.org/show_bug.cgi?id=10928
1046
1047 struct D10928
1048 {
1049 int x;
1050 ~this() @nogc {}
1051 }
1052
1053 void f10928a(D10928 bar)
1054 {
1055 (){ bar.x++; }();
1056 }
1057 void f10928b(D10928 bar) @nogc
1058 {
1059 (){ bar.x++; }();
1060 }
1061
1062 void test10928()
1063 {
1064 f10928a(D10928.init);
1065 f10928b(D10928.init);
1066 }
1067
1068 /***************************************************/
1069 // https://issues.dlang.org/show_bug.cgi?id=11661
1070
1071 void test11661()
1072 {
1073 void delegate() dg = {}; // OK
1074 void function() fp = {}; // OK <- NG
1075 }
1076
1077 /***************************************************/
1078 // https://issues.dlang.org/show_bug.cgi?id=11774
1079
1080 void f11774(T, R)(R delegate(T[]) dg)
1081 {
1082 T[] src;
1083 dg(src);
1084 }
1085
1086 void test11774()
1087 {
1088 int[] delegate(int[]) dg = (int[] a) => a;
1089 f11774!int(dg);
1090 f11774!Object(a => a);
1091 f11774!int(dg);
1092 }
1093
1094 /***************************************************/
1095 // https://issues.dlang.org/show_bug.cgi?id=12421
1096
1097 void test12421()
1098 {
1099 void test(string decl, bool polymorphic = true)()
1100 {
1101 // parse AliasDeclaration in Statement
1102 mixin("alias f = " ~ decl ~ ";");
1103 assert(f(1) == 1);
1104 static if (polymorphic)
1105 assert(f("s") == "s");
1106
1107 // parse AliasDeclaration in DeclDefs
1108 mixin("template X() { alias g = " ~ decl ~ "; }");
1109 alias g = X!().g;
1110 assert(g(1) == 1);
1111 static if (polymorphic)
1112 assert(g("s") == "s");
1113 }
1114
1115 test!(q{ x => x });
1116 test!(q{ ( x) => x });
1117 test!(q{ (int x) => x }, false);
1118
1119 test!(q{ ( x){ return x; } });
1120 test!(q{ (int x){ return x; } }, false);
1121
1122 test!(q{ function ( x) => x });
1123 test!(q{ function (int x) => x }, false);
1124 test!(q{ function int ( x) => x }, false);
1125 test!(q{ function int (int x) => x }, false);
1126
1127 test!(q{ delegate ( x) => x });
1128 test!(q{ delegate (int x) => x }, false);
1129 test!(q{ delegate int ( x) => x }, false);
1130 test!(q{ delegate int (int x) => x }, false);
1131
1132 test!(q{ function ( x){ return x; } });
1133 test!(q{ function (int x){ return x; } }, false);
1134 test!(q{ function int ( x){ return x; } }, false);
1135 test!(q{ function int (int x){ return x; } }, false);
1136
1137 test!(q{ delegate ( x){ return x; } });
1138 test!(q{ delegate (int x){ return x; } }, false);
1139 test!(q{ delegate int ( x){ return x; } }, false);
1140 test!(q{ delegate int (int x){ return x; } }, false);
1141
1142 // This is problematic case, and should be disallowed in the future.
1143 alias f = x => y;
1144 int y = 10;
1145 assert(f(1) == 10);
1146 }
1147
1148 /***************************************************/
1149 // https://issues.dlang.org/show_bug.cgi?id=12508
1150
1151 interface A12508(T)
1152 {
1153 T getT();
1154 }
1155
1156 class C12508 : A12508!double
1157 {
1158 double getT() { return 1; }
1159 }
1160
1161 void f12508(A12508!double delegate() dg)
1162 {
1163 auto a = dg();
1164 assert(a !is null);
1165 assert(a.getT() == 1.0); // fails!
1166 }
1167
1168 void t12508(T)(A12508!T delegate() dg)
1169 {
1170 auto a = dg();
1171 assert(a !is null);
1172 assert(a.getT() == 1.0); // fails!
1173 }
1174
1175 ref alias Dg12508 = A12508!double delegate();
1176 void t12508(T)(Dg12508 dg) {}
1177
1178 void test12508()
1179 {
1180 f12508({ return new C12508(); });
1181 t12508({ return new C12508(); });
1182 static assert(!__traits(compiles, x12508({ return new C12508(); })));
1183 }
1184
1185 /***************************************************/
1186 // https://issues.dlang.org/show_bug.cgi?id=13879
1187
1188 void test13879()
1189 {
1190 bool function(int)[2] funcs1 = (int x) => true; // OK
1191 assert(funcs1[0] is funcs1[1]);
1192 funcs1[0] = x => true; // OK
1193 bool function(int)[2] funcs2 = x => true; // OK <- Error
1194 assert(funcs2[0] is funcs2[1]);
1195 }
1196
1197 /***************************************************/
1198 // https://issues.dlang.org/show_bug.cgi?id=14745
1199
1200 void test14745()
1201 {
1202 // qualified nested functions
1203 auto foo1() pure immutable { return 0; }
1204 auto foo2() pure const { return 0; }
1205
1206 // qualified lambdas (== implicitly marked as delegate literals)
1207 auto lambda1 = () pure immutable { return 0; };
1208 auto lambda2 = () pure const { return 0; };
1209 static assert(is(typeof(lambda1) : typeof(&foo1)));
1210 static assert(is(typeof(lambda2) : typeof(&foo2)));
1211
1212 // qualified delegate literals
1213 auto dg1 = delegate () pure immutable { return 0; };
1214 auto dg2 = delegate () pure const { return 0; };
1215 static assert(is(typeof(dg1) : typeof(&foo1)));
1216 static assert(is(typeof(dg2) : typeof(&foo2)));
1217 }
1218
1219 /***************************************************/
1220 // https://issues.dlang.org/show_bug.cgi?id=15794
1221
1222 struct Foo15794
1223 {
1224 static void fun(Holder)()
1225 {
1226 int i = Holder.fn();
1227 }
1228 }
1229
1230 struct Holder15794(alias Fn)
1231 {
1232 alias fn = Fn;
1233 }
1234
1235 void gun15794(alias fn, U...)()
1236 {
1237 Foo15794.fun!(Holder15794!fn)();
1238 }
1239
1240 void test15794()
1241 {
1242 gun15794!(() => 0)(); // Line 26
1243 }
1244
1245 /***************************************************/
1246 // https://issues.dlang.org/show_bug.cgi?id=16271
1247
1248 ref auto funa16271(alias dg, T)(ref T a)
1249 {
1250 return dg(a);
1251 }
1252
1253 ref auto func16271(alias dg)()
1254 {
1255 return dg();
1256 }
1257
1258 void assign16271(T)(ref T a, T b)
1259 {
1260 alias fun = ref (ref a) => a;
1261 fun(a) = b;
1262 }
1263
1264 void test16271()
1265 {
1266 int x;
1267 (ref () => x )() = 1; assert(x == 1);
1268 func16271!(ref () => x) = 2; assert(x == 2);
1269 assign16271(x, 3); assert(x == 3);
1270
1271 alias alx = func16271!(ref () => x);
1272 alx = 4; assert(x == 4);
1273
1274 alias alf = ref (ref a) => a;
1275 auto auf = ref (ref int a) => a;
1276 alf(x) = 5; assert(x == 5);
1277 auf(x) = 6; assert(x == 6);
1278
1279 assert((funa16271!( ref (ref a) => a)(x) += 1) == 7 );
1280 assert((funa16271!(function ref (ref a) => a)(x) += 1) == 8 );
1281 assert((funa16271!(function ref int(ref a) => a)(x) += 1) == 9 );
1282 assert((funa16271!(delegate ref (ref a) => a)(x) += 1) == 10);
1283 assert((funa16271!(delegate ref int(ref a) => a)(x) += 1) == 11);
1284 assert(x == 11);
1285
1286 alias aldc = ref () @trusted @nogc { return x; };
1287 auto audc = ref () @safe nothrow { return x; };
1288 alias alfuc = function ref (ref x) @trusted { return x; };
1289 alias aldec = delegate ref () @trusted { return x; };
1290 aldc() = 12; assert(x == 12);
1291 audc() = 13; assert(x == 13);
1292 alfuc(x) = 14; assert(x == 14);
1293 aldec() = 15; assert(x == 15);
1294
1295 template T()
1296 {
1297 int x;
1298 alias alf = ref () => x;
1299 auto auf = ref () => x;
1300 }
1301 T!().alf() = 1; assert(T!().x == 1);
1302 T!().auf() = 2; assert(T!().x == 2);
1303 }
1304
1305 /***************************************************/
1306
1307 int main()
1308 {
1309 test1();
1310 test2();
1311 test3();
1312 test4();
1313 test4v();
1314 test5();
1315 test6();
1316 test7();
1317 test8();
1318 test9();
1319 test10();
1320 test11();
1321 test3235();
1322 test6714();
1323 test7193();
1324 test7202();
1325 test7288();
1326 test7499();
1327 test7500();
1328 test7525();
1329 test7582();
1330 test7649();
1331 test7650();
1332 test7705();
1333 test7713();
1334 test7743();
1335 test7761();
1336 test7941();
1337 test8005();
1338 test8198();
1339 test8226();
1340 test8241();
1341 test8242();
1342 test8315();
1343 test8397();
1344 test8496();
1345 test8575();
1346 test9153();
1347 test9393();
1348 test9415();
1349 test9628();
1350 test9928();
1351 test10133();
1352 test10219();
1353 test10288();
1354 test10336();
1355 test10928();
1356 test11661();
1357 test11774();
1358 test12421();
1359 test12508();
1360 test13879();
1361 test14745();
1362 test15794();
1363 test16271();
1364
1365 printf("Success\n");
1366 return 0;
1367 }