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