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 
una(alias dg)24 auto una(alias dg)(int n)
25 {
26     return dg(n);
27 }
bin(alias dg)28 auto bin(alias dg)(int n, int m)
29 {
30     return dg(n, m);
31 }
test1()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 
test2()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 
test3()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 
foo4(int delegate (int)dg)141 auto foo4(int delegate(int) dg) { return dg(10); }
foo4(int delegate (int,int)dg)142 auto foo4(int delegate(int, int) dg) { return dg(10, 20); }
143 
nbar4fp(void function (int)fp)144 void nbar4fp(void function(int) fp) { }
nbar4dg(void delegate (int)dg)145 void nbar4dg(void delegate(int) dg) { }
tbar4fp(T,R)146 void tbar4fp(T,R)(R function(T) dg) { static assert(is(typeof(dg) == void function(int))); }
tbar4dg(T,R)147 void tbar4dg(T,R)(R delegate(T) dg) { static assert(is(typeof(dg) == void delegate(int))); }
148 
nbaz4(void function ()fp)149 auto nbaz4(void function() fp) { return 1; }
nbaz4(void delegate ()dg)150 auto nbaz4(void delegate() dg) { return 2; }
tbaz4(R)151 auto tbaz4(R)(R function() dg) { static assert(is(R == void)); return 1; }
tbaz4(R)152 auto tbaz4(R)(R delegate() dg) { static assert(is(R == void)); return 2; }
153 
thoo4(T)154 auto thoo4(T)(T lambda){ return lambda; }
155 
tfun4a()156 void tfun4a()(int function(int) a){}
tfun4b(T)157 void tfun4b(T)(T function(T) a){}
tfun4c(T)158 void tfun4c(T)(T f){}
159 
test4()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);    // Bugzilla 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 
fsvarg4(int function (int)[]a...)196 void fsvarg4(int function(int)[] a...){}
fcvarg4(int dummy,...)197 void fcvarg4(int dummy, ...){}
198 
tsvarg4a()199 void tsvarg4a()(int function(int)[] a...){}
tsvarg4b(T)200 void tsvarg4b(T)(T function(T)[] a...){}
tsvarg4c(T)201 void tsvarg4c(T)(T [] a...){}
tcvarg4()202 void tcvarg4()(int dummy, ...){}
203 
test4v()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 
test5()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 
checkNestedRef(alias dg)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 
freeFunc()256 void freeFunc(){}
257 
test6()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 
test7()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 
test8()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 
test9()314 void test9()
315 {
316     int function(int)[] a2;
317     a2 ~= x => x;
318 }
319 
320 /***************************************************/
321 // on associative array key
322 
test10()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 
test11()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 // 3235
358 
test3235()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 // 6714
369 
foo6714x(int function (int,int)a)370 void foo6714x(int function (int, int) a){}
bar6714x(int delegate (int,int)a)371 void bar6714x(int delegate (int, int) a){}
372 
bar6714y(double delegate (int,int)a)373 int bar6714y(double delegate(int, int) a){ return 1; }
bar6714y(int delegate (int,int)a)374 int bar6714y(   int delegate(int, int) a){ return 2; }
375 
test6714()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 // 7193
388 
test7193()389 void test7193()
390 {
391     static assert(!__traits(compiles, {
392         delete a => a;
393     }));
394 }
395 
396 /***************************************************/
397 // 7207 : on CastExp
398 
test7202()399 void test7202()
400 {
401     auto dg = cast(int function(int))(a => a);
402     assert(dg(10) == 10);
403 }
404 
405 /***************************************************/
406 // 7288
407 
test7288()408 void test7288()
409 {
410     // 7288 -> OK
411     auto foo()
412     {
413         int x;
414         return () => { return x; };
415     }
416     pragma(msg, typeof(&foo));
417     alias int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe Dg;
418     pragma(msg, Dg);
419     static assert(is(typeof(&foo) == Dg));  // should pass
420 }
421 
422 /***************************************************/
423 // 7499
424 
test7499()425 void test7499()
426 {
427     int function(int)[]   a1 = [ x => x ];  // 7499
428     int function(int)[][] a2 = [[x => x]];  // +a
429     assert(a1[0]   (10) == 10);
430     assert(a2[0][0](10) == 10);
431 }
432 
433 /***************************************************/
434 // 7500
435 
test7500()436 void test7500()
437 {
438     alias immutable bool function(int[]) Foo;
439     Foo f = a => true;
440 }
441 
442 /***************************************************/
443 // 7525
444 
test7525()445 void test7525()
446 {
447     {
448         char[] delegate() a = { return null; };
449            int delegate() b = { return 1U; };
450           uint delegate() c = { return 1; };
451          float delegate() d = { return 1.0; };
452         double delegate() e = { return 1.0f; };
453     }
454 
455     {
456         char[] delegate(int) a = (x){ return null; };
457            int delegate(int) b = (x){ return 1U; };
458           uint delegate(int) c = (x){ return 1; };
459          float delegate(int) d = (x){ return 1.0; };
460         double delegate(int) e = (x){ return 1.0f; };
461     }
462 }
463 
464 /***************************************************/
465 // 7582
466 
test7582()467 void test7582()
468 {
469     void delegate(int) foo;
470     void delegate(int) foo2;
471     foo = (a) {
472         foo2 = (b) { };
473     };
474 }
475 
476 /***************************************************/
477 // 7649
478 
test7649()479 void test7649()
480 {
481     void foo(int function(int) fp = x => 1)
482     {
483         assert(fp(1) == 1);
484     }
485     foo();
486 }
487 
488 /***************************************************/
489 // 7650
490 
test7650()491 void test7650()
492 {
493     int[int function(int)] aa1 = [x=>x:1, x=>x*2:2];
494     foreach (k, v; aa1) {
495         if (v == 1) assert(k(10) == 10);
496         if (v == 2) assert(k(10) == 20);
497     }
498 
499     int function(int)[int] aa2 = [1:x=>x, 2:x=>x*2];
500     assert(aa2[1](10) == 10);
501     assert(aa2[2](10) == 20);
502 
503     int n = 10;
504     int[int delegate(int)] aa3 = [x=>n+x:1, x=>n+x*2:2];
505     foreach (k, v; aa3) {
506         if (v == 1) assert(k(10) == 20);
507         if (v == 2) assert(k(10) == 30);
508     }
509 
510     int delegate(int)[int] aa4 = [1:x=>n+x, 2:x=>n+x*2];
511     assert(aa4[1](10) == 20);
512     assert(aa4[2](10) == 30);
513 }
514 
515 /***************************************************/
516 // 7705
517 
test7705()518 void test7705()
519 {
520     void foo1(void delegate(ref int ) dg){ int x=10; dg(x); }
521     foo1((ref x){ pragma(msg, typeof(x)); assert(x == 10); });
522     static assert(!__traits(compiles, foo1((x){}) ));
523 
524     void foo2(void delegate(int, ...) dg){ dg(20, 3.14); }
525     foo2((x,...){ pragma(msg, typeof(x)); assert(x == 20); });
526 
527     void foo3(void delegate(int[]...) dg){ dg(1, 2, 3); }
528     foo3((x ...){ pragma(msg, typeof(x)); assert(x == [1,2,3]); });
529 }
530 
531 /***************************************************/
532 // 7713
533 
foo7713(T)534 void foo7713(T)(T delegate(in Object) dlg)
535 {}
test7713()536 void test7713()
537 {
538    foo7713( (in obj) { return 15; } );   // line 6
539 }
540 
541 /***************************************************/
542 // 7743
543 
foo7743a()544 auto foo7743a()
545 {
546     int x = 10;
547     return () nothrow {
548         return x;
549     };
550 }
foo7743b()551 auto foo7743b()
552 {
553     int x = 10;
554     return () nothrow => x;
555 }
test7743()556 void test7743()
557 {
558     pragma(msg, typeof(&foo7743a));
559     static assert(is(typeof(&foo7743a) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
560     assert(foo7743a()() == 10);
561 
562     static assert(is(typeof(&foo7743b) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe));
563     assert(foo7743b()() == 10);
564 }
565 
566 /***************************************************/
567 // 7761
568 
569 enum dg7761 = (int a) pure => 2 * a;
570 
test7761()571 void test7761()
572 {
573     static assert(is(typeof(dg7761) == int function(int) pure @safe nothrow @nogc));
574     assert(dg7761(10) == 20);
575 }
576 
577 /***************************************************/
578 // 7941
579 
test7941()580 void test7941()
581 {
582     static assert(!__traits(compiles, { enum int c = function(){}; }));
583 }
584 
585 /***************************************************/
586 // 8005
587 
test8005()588 void test8005()
589 {
590     auto n = (a, int n = 2){ return n; }(1);
591     assert(n == 2);
592 }
593 
594 /***************************************************/
595 // test8198
596 
test8198()597 void test8198()
598 {
599     T delegate(T) zero(T)(T delegate(T) f)
600     {
601         return x => x;
602     }
603 
604     T delegate(T) delegate(T delegate(T)) succ(T)(T delegate(T) delegate(T delegate(T)) n)
605     {
606         return f => x => f(n(f)(x));
607     }
608 
609     uint delegate(uint) delegate(uint delegate(uint)) n = &zero!uint;
610     foreach (i; 0..10)
611     {
612         assert(n(x => x + 1)(0) == i);
613         n = succ(n);
614     }
615 }
616 
617 /***************************************************/
618 // 8226
619 
620 immutable f8226 = (int x) => x * 2;
621 
test8226()622 void test8226()
623 {
624     assert(f8226(10) == 20);
625 }
626 
627 /***************************************************/
628 // 8241
629 
630 auto exec8241a(alias a = function(x) => x, T...)(T as)
631 {
632     return a(as);
633 }
634 
635 auto exec8241b(alias a = (x) => x, T...)(T as)
636 {
637     return a(as);
638 }
639 
test8241()640 void test8241()
641 {
642     exec8241a(2);
643     exec8241b(2);
644 }
645 
646 /***************************************************/
647 // 8242
648 
exec8242(alias a,T...)649 template exec8242(alias a, T...)
650 {
651     auto func8242(T as)
652     {
653         return a(as);
654     }
655 }
656 
657 mixin exec8242!(x => x, int);
658 mixin exec8242!((string x) => x, string);
659 
test8242()660 void test8242()
661 {
662     func8242(1);
663     func8242("");
664 }
665 
666 /***************************************************/
667 // 8315
668 
test8315()669 void test8315()
670 {
671     bool b;
672     foo8315!(a => b)();
673 }
674 
675 void foo8315(alias pred)()
676 if (is(typeof(pred(1)) == bool))
677 {}
678 
679 /***************************************************/
680 // 8397
681 
test8397()682 void test8397()
683 {
684     void function(int) f;
685   static assert(!is(typeof({
686     f = function(string x) {};
687   })));
688 }
689 
690 /***************************************************/
691 // 8496
692 
test8496()693 void test8496()
694 {
695     alias extern (C) void function() Func;
696 
697     Func fp = (){};
698 
699     fp = (){};
700 }
701 
702 /***************************************************/
703 // 8575
704 
tfunc8575(func...)705 template tfunc8575(func...)
706 {
707     auto tfunc8575(U)(U u) { return func[0](u); }
708 }
bar8575(T)709 auto bar8575(T)(T t)
710 {
711     return tfunc8575!(a => a)(t);
712 }
foo8575a()713 void foo8575a() { assert(bar8575(uint.init + 1) == +1); }
foo8575b()714 void foo8575b() { assert(bar8575( int.init - 1) == -1); }
715 
test8575()716 void test8575()
717 {
718     foo8575a();
719     foo8575b();
720 }
721 
722 /***************************************************/
723 // 9153
724 
writeln9153(string s)725 void writeln9153(string s){}
726 
test9153()727 void test9153()
728 {
729     auto tbl1 = [
730         (string x) { writeln9153(x); },
731         (string x) { x ~= 'a'; },
732     ];
733     auto tbl2 = [
734         (string x) { x ~= 'a'; },
735         (string x) { writeln9153(x); },
736     ];
737 }
738 
739 /***************************************************/
740 // 9393
741 
ifThrown9393a(E)742 template ifThrown9393a(E)
743 {
744     void ifThrown9393a(T)(scope T delegate(E) errHandler)
745     {
746     }
747 }
ifThrown9393b(E,T)748 void ifThrown9393b(E, T)(scope T delegate(E) errHandler)
749 {
750 }
751 
foo9393(T)752 void foo9393(T)(void delegate(T) dg){ dg(T.init); }
foo9393()753 void foo9393()(void delegate(int) dg){ foo9393!int(dg); }
754 
test9393()755 void test9393()
756 {
757     ifThrown9393a!Exception(e => 10);
758     ifThrown9393b!Exception(e => 10);
759 
760     foo9393((x){ assert(x == int.init); });
761 }
762 
763 /***************************************************/
764 // 9415
765 
test9415()766 void test9415()
767 {
768     int z;
769     typeof((int a){return z;}) dg;
770     dg = (int a){return z;};
771 }
772 
773 /***************************************************/
774 // 9628
775 
TypeTuple9628(TL...)776 template TypeTuple9628(TL...) { alias TypeTuple9628 = TL; }
map9628(alias func)777 void map9628(alias func)() { func(0); }
778 
test9628()779 void test9628()
780 {
781     auto items = [[10, 20], [30]];
782     size_t[] res;
783 
784     res = null;
785     foreach (_; 0 .. 2)
786     {
787         foreach (sub; items)
788         {
789             map9628!((       i){ res ~= sub.length; });
790             map9628!((size_t i){ res ~= sub.length; });
791         }
792     }
793     assert(res == [2,2,1,1, 2,2,1,1]);
794 
795     res = null;
796     foreach (_; TypeTuple9628!(0, 1))
797     {
798         foreach (sub; items)
799         {
800             map9628!((       i){ res ~= sub.length; });
801             map9628!((size_t i){ res ~= sub.length; });
802         }
803     }
804     assert(res == [2,2,1,1, 2,2,1,1]);
805 }
806 
807 /***************************************************/
808 // 9928
809 
test9928()810 void test9928()
811 {
812     void* smth = (int x) { return x; };
813 }
814 
815 /***************************************************/
816 // 10133
817 
countUntil10133(alias pred,R)818 ptrdiff_t countUntil10133(alias pred, R)(R haystack)
819 {
820     typeof(return) i;
821 
822     alias T = dchar;
823 
824     foreach (T elem; haystack)
825     {
826         if (pred(elem)) return i;
827         ++i;
828     }
829 
830     return -1;
831 }
832 
833 bool func10133(string s)() if (countUntil10133!(x => x == 'x')(s) == 1)
834 {
835     return true;
836 }
837 
838 bool func10133a(string s)() if (countUntil10133!(x => s == "x")(s) != -1)
839 {
840     return true;
841 }
842 bool func10133b(string s)() if (countUntil10133!(x => s == "x")(s) != -1)
843 {
844     return true;
845 }
846 
test10133()847 void test10133()
848 {
849     func10133!("ax")();
850 
851     func10133a!("x")();
852     static assert(!is(typeof(func10133a!("ax")())));
853     static assert(!is(typeof(func10133b!("ax")())));
854     func10133b!("x")();
855 }
856 
857 /***************************************************/
858 // 10219
859 
test10219()860 void test10219()
861 {
862     interface I { }
863     class C : I { }
864 
865     void test_dg(I delegate(C) dg)
866     {
867         C c = new C;
868         void* cptr = cast(void*) c;
869         void* iptr = cast(void*) cast(I) c;
870         void* xptr = cast(void*) dg(c);
871         assert(cptr != iptr);
872         assert(cptr != xptr); // should pass
873         assert(iptr == xptr); // should pass
874     }
875 
876     C delegate(C c) dg = delegate C(C c) { return c; };
877     static assert(!__traits(compiles, { test_dg(dg); }));
878     static assert(!__traits(compiles, { test_dg(delegate C(C c) { return c; }); }));
879     static assert(!__traits(compiles, { I delegate(C) dg2 = dg; }));
880 
881     // creates I delegate(C)
882     test_dg(c => c);
883     test_dg(delegate(C c) => c);
884 
885     void test_fp(I function(C) fp)
886     {
887         C c = new C;
888         void* cptr = cast(void*) c;
889         void* iptr = cast(void*) cast(I) c;
890         void* xptr = cast(void*) fp(c);
891         assert(cptr != iptr);
892         assert(cptr != xptr); // should pass
893         assert(iptr == xptr); // should pass
894     }
895 
896     C function(C c) fp = function C(C c) { return c; };
897     static assert(!__traits(compiles, { test_fp(fp); }));
898     static assert(!__traits(compiles, { test_fp(function C(C c) { return c; }); }));
899     static assert(!__traits(compiles, { I function(C) fp2 = fp; }));
900 
901     // creates I function(C)
902     test_fp(c => c);
903     test_fp(function(C c) => c);
904 }
905 
906 /***************************************************/
907 // 10288
908 
foo10288(T)909 T foo10288(T)(T x)
910 {
911     void lambda() @trusted nothrow { x += 10; }
912     lambda();
913     return x;
914 }
915 
bar10288(T)916 T bar10288(T)(T x)
917 {
918     () @trusted { x += 10; } ();
919     return x;
920 }
921 
baz10288(T)922 T baz10288(T)(T arg)
923 {
924     static int g = 10;
925     () @trusted { x += g; } ();
926     return x;
927 }
928 
test10288()929 void test10288() @safe pure nothrow
930 {
931     assert(foo10288(10) == 20); // OK
932     assert(bar10288(10) == 20); // OK <- NG
933     static assert(!__traits(compiles, baz10288(10)));
934 }
935 
936 /***************************************************/
937 // 10666
938 
939 struct S10666
940 {
941     int val;
~thisS10666942     ~this() {}
943 }
944 
foo10666(S10666 s1)945 void foo10666(S10666 s1)
946 {
947     S10666 s2;
948 
949     /* Even if closureVars(s1 and s2) are accessed by directly called lambdas,
950      * they won't escape the scope of this function.
951      */
952     auto x1 = (){ return s1.val; }();   // OK
953     auto x2 = (){ return s2.val; }();   // OK
954 }
955 
956 /***************************************************/
957 // 11081
958 
959 T ifThrown11081(E : Throwable, T)(T delegate(E) errorHandler)
960 {
961     return errorHandler();
962 }
963 
test11081()964 void test11081()
965 {
966     static if (__traits(compiles, ifThrown11081!Exception(e => 0)))
967     {
968     }
969     static if (__traits(compiles, ifThrown11081!Exception(e => 0)))
970     {
971     }
972 }
973 
974 /***************************************************/
975 // 11220
976 
parsePrimaryExp11220(int x)977 int parsePrimaryExp11220(int x)
978 {
979     parseAmbig11220!( (parsed){ x += 1; } )();
980     return 1;
981 }
982 
parseAmbig11220(alias handler)983 typeof(handler(1)) parseAmbig11220(alias handler)()
984 {
985     return handler(parsePrimaryExp11220(1));
986 }
987 
988 /***************************************************/
989 // 11230
990 
map11230(fun...)991 template map11230(fun...)
992 {
993     auto map11230(Range)(Range r)
994     {
995         return MapResult11230!(fun, Range)(r);
996     }
997 }
998 
MapResult11230(alias fun,R)999 struct MapResult11230(alias fun, R)
1000 {
1001     R _input;
1002     this(R input) { _input = input; }
1003 }
1004 
1005 class A11230 { A11230[] as; }
1006 class B11230 { A11230[] as; }
1007 class C11230 : A11230 {}
1008 
visit11230(A11230 a)1009 C11230 visit11230(A11230 a)
1010 {
1011     a.as.map11230!(a => visit11230);
1012     return null;
1013 }
visit11230(B11230 b)1014 C11230 visit11230(B11230 b)
1015 {
1016     b.as.map11230!(a => visit11230);
1017     return null;
1018 }
visit11230()1019 C11230 visit11230()
1020 {
1021     return null;
1022 }
1023 
1024 /***************************************************/
1025 // 10336
1026 
1027 struct S10336
1028 {
opDispatchS103361029     template opDispatch(string name)
1030     {
1031         enum opDispatch = function(int x) {
1032             return x;
1033         };
1034     }
1035 }
1036 
test10336()1037 void test10336()
1038 {
1039     S10336 s;
1040     assert(s.hello(12) == 12);
1041 }
1042 
1043 /***************************************************/
1044 // 10928
1045 
1046 struct D10928
1047 {
1048     int x;
~thisD109281049     ~this() @nogc {}
1050 }
1051 
f10928a(D10928 bar)1052 void f10928a(D10928 bar)
1053 {
1054     (){ bar.x++; }();
1055 }
f10928b(D10928 bar)1056 void f10928b(D10928 bar) @nogc
1057 {
1058     (){ bar.x++; }();
1059 }
1060 
test10928()1061 void test10928()
1062 {
1063     f10928a(D10928.init);
1064     f10928b(D10928.init);
1065 }
1066 
1067 /***************************************************/
1068 // 11661
1069 
test11661()1070 void test11661()
1071 {
1072     void delegate() dg = {};  // OK
1073     void function() fp = {};  // OK <- NG
1074 }
1075 
1076 /***************************************************/
1077 // 11774
1078 
f11774(T,R)1079 void f11774(T, R)(R delegate(T[]) dg)
1080 {
1081     T[] src;
1082     dg(src);
1083 }
1084 
test11774()1085 void test11774()
1086 {
1087     int[] delegate(int[]) dg = (int[] a) => a;
1088     f11774!int(dg);
1089     f11774!Object(a => a);
1090     f11774!int(dg);
1091 }
1092 
1093 /***************************************************/
1094 // 12421
1095 
test12421()1096 void test12421()
1097 {
1098     void test(string decl, bool polymorphic = true)()
1099     {
1100         // parse AliasDeclaration in Statement
1101         mixin("alias f = " ~ decl ~ ";");
1102         assert(f(1) == 1);
1103       static if (polymorphic)
1104         assert(f("s") == "s");
1105 
1106         // parse AliasDeclaration in DeclDefs
1107         mixin("template X() { alias g = " ~ decl ~ "; }");
1108         alias g = X!().g;
1109         assert(g(1) == 1);
1110       static if (polymorphic)
1111         assert(g("s") == "s");
1112     }
1113 
1114     test!(q{      x  => x });
1115     test!(q{ (    x) => x });
1116     test!(q{ (int x) => x }, false);
1117 
1118     test!(q{ (    x){ return x; } });
1119     test!(q{ (int x){ return x; } }, false);
1120 
1121     test!(q{ function     (    x) => x });
1122     test!(q{ function     (int x) => x }, false);
1123     test!(q{ function int (    x) => x }, false);
1124     test!(q{ function int (int x) => x }, false);
1125 
1126     test!(q{ delegate     (    x) => x });
1127     test!(q{ delegate     (int x) => x }, false);
1128     test!(q{ delegate int (    x) => x }, false);
1129     test!(q{ delegate int (int x) => x }, false);
1130 
1131     test!(q{ function     (    x){ return x; } });
1132     test!(q{ function     (int x){ return x; } }, false);
1133     test!(q{ function int (    x){ return x; } }, false);
1134     test!(q{ function int (int x){ return x; } }, false);
1135 
1136     test!(q{ delegate     (    x){ return x; } });
1137     test!(q{ delegate     (int x){ return x; } }, false);
1138     test!(q{ delegate int (    x){ return x; } }, false);
1139     test!(q{ delegate int (int x){ return x; } }, false);
1140 
1141     // This is problematic case, and should be disallowed in the future.
1142     alias f = x => y;
1143     int y = 10;
1144     assert(f(1) == 10);
1145 }
1146 
1147 /***************************************************/
1148 // 12508
1149 
A12508(T)1150 interface A12508(T)
1151 {
1152     T getT();
1153 }
1154 
1155 class C12508 : A12508!double
1156 {
getT()1157     double getT() { return 1; }
1158 }
1159 
1160 void f12508(A12508!double delegate() dg)
1161 {
1162     auto a = dg();
1163     assert(a !is null);
1164     assert(a.getT() == 1.0);    // fails!
1165 }
1166 
t12508(T)1167 void t12508(T)(A12508!T delegate() dg)
1168 {
1169     auto a = dg();
1170     assert(a !is null);
1171     assert(a.getT() == 1.0);    // fails!
1172 }
1173 
1174 ref alias Dg12508 = A12508!double delegate();
t12508(T)1175 void t12508(T)(Dg12508 dg) {}
1176 
test12508()1177 void test12508()
1178 {
1179     f12508({ return new C12508(); });
1180     t12508({ return new C12508(); });
1181     static assert(!__traits(compiles, x12508({ return new C12508(); })));
1182 }
1183 
1184 /***************************************************/
1185 // 13879
1186 
test13879()1187 void test13879()
1188 {
1189     bool function(int)[2] funcs1 = (int x) => true; // OK
1190     assert(funcs1[0] is funcs1[1]);
1191     funcs1[0] = x => true;                          // OK
1192     bool function(int)[2] funcs2 = x => true;       // OK <- Error
1193     assert(funcs2[0] is funcs2[1]);
1194 }
1195 
1196 /***************************************************/
1197 // 14745
1198 
test14745()1199 void test14745()
1200 {
1201     // qualified nested functions
1202     auto foo1() pure immutable { return 0; }
1203     auto foo2() pure const { return 0; }
1204 
1205     // qualified lambdas (== implicitly marked as delegate literals)
1206     auto lambda1 = () pure immutable { return 0; };
1207     auto lambda2 = () pure const { return 0; };
1208     static assert(is(typeof(lambda1) : typeof(&foo1)));
1209     static assert(is(typeof(lambda2) : typeof(&foo2)));
1210 
1211     // qualified delegate literals
1212     auto dg1 = delegate () pure immutable { return 0; };
1213     auto dg2 = delegate () pure const { return 0; };
1214     static assert(is(typeof(dg1) : typeof(&foo1)));
1215     static assert(is(typeof(dg2) : typeof(&foo2)));
1216 }
1217 
1218 /***************************************************/
1219 // 15794
1220 
1221 struct Foo15794
1222 {
funFoo157941223     static void fun(Holder)()
1224     {
1225         int i = Holder.fn();
1226     }
1227 }
1228 
Holder15794(alias Fn)1229 struct Holder15794(alias Fn)
1230 {
1231     alias fn = Fn;
1232 }
1233 
gun15794(alias fn,U...)1234 void gun15794(alias fn, U...)()
1235 {
1236     Foo15794.fun!(Holder15794!fn)();
1237 }
1238 
test15794()1239 void test15794()
1240 {
1241     gun15794!(() => 0)(); // Line 26
1242 }
1243 
1244 /***************************************************/
1245 // https://issues.dlang.org/show_bug.cgi?id=16271
1246 
funa16271(alias dg,T)1247 ref auto funa16271(alias dg, T)(ref T a)
1248 {
1249     return dg(a);
1250 }
1251 
func16271(alias dg)1252 ref auto func16271(alias dg)()
1253 {
1254     return dg();
1255 }
1256 
assign16271(T)1257 void assign16271(T)(ref T a, T b)
1258 {
1259     alias fun = ref (ref a) => a;
1260     fun(a) = b;
1261 }
1262 
test16271()1263 void test16271()
1264 {
1265     int x;
1266     (ref () => x )() = 1;           assert(x == 1);
1267     func16271!(ref () => x) = 2;    assert(x == 2);
1268     assign16271(x, 3);              assert(x == 3);
1269 
1270     alias alx = func16271!(ref () => x);
1271     alx = 4;    assert(x == 4);
1272 
1273     alias alf = ref (ref a) => a;
1274     auto  auf = ref (ref int a) => a;
1275     alf(x) = 5;    assert(x == 5);
1276     auf(x) = 6;    assert(x == 6);
1277 
1278     assert((funa16271!(         ref    (ref a) => a)(x) += 1) == 7 );
1279     assert((funa16271!(function ref    (ref a) => a)(x) += 1) == 8 );
1280     assert((funa16271!(function ref int(ref a) => a)(x) += 1) == 9 );
1281     assert((funa16271!(delegate ref    (ref a) => a)(x) += 1) == 10);
1282     assert((funa16271!(delegate ref int(ref a) => a)(x) += 1) == 11);
1283     assert(x == 11);
1284 
1285     alias aldc  = ref () @trusted @nogc { return x; };
1286     auto  audc  = ref () @safe nothrow  { return x; };
1287     alias alfuc = function ref (ref x) @trusted { return x; };
1288     alias aldec = delegate ref () @trusted { return x; };
1289     aldc()   = 12;    assert(x == 12);
1290     audc()   = 13;    assert(x == 13);
1291     alfuc(x) = 14;    assert(x == 14);
1292     aldec()  = 15;    assert(x == 15);
1293 
1294     template T()
1295     {
1296         int x;
1297         alias alf = ref () => x;
1298         auto auf = ref () => x;
1299     }
1300     T!().alf() = 1;  assert(T!().x == 1);
1301     T!().auf() = 2;  assert(T!().x == 2);
1302 }
1303 
1304 /***************************************************/
1305 
main()1306 int main()
1307 {
1308     test1();
1309     test2();
1310     test3();
1311     test4();
1312     test4v();
1313     test5();
1314     test6();
1315     test7();
1316     test8();
1317     test9();
1318     test10();
1319     test11();
1320     test3235();
1321     test6714();
1322     test7193();
1323     test7202();
1324     test7288();
1325     test7499();
1326     test7500();
1327     test7525();
1328     test7582();
1329     test7649();
1330     test7650();
1331     test7705();
1332     test7713();
1333     test7743();
1334     test7761();
1335     test7941();
1336     test8005();
1337     test8198();
1338     test8226();
1339     test8241();
1340     test8242();
1341     test8315();
1342     test8397();
1343     test8496();
1344     test8575();
1345     test9153();
1346     test9393();
1347     test9415();
1348     test9628();
1349     test9928();
1350     test10133();
1351     test10219();
1352     test10288();
1353     test10336();
1354     test10928();
1355     test11661();
1356     test11774();
1357     test12421();
1358     test12508();
1359     test13879();
1360     test14745();
1361     test15794();
1362     test16271();
1363 
1364     printf("Success\n");
1365     return 0;
1366 }
1367