Debian Codegraph @

gcc/testsuite/gdc.test/compilable/staticforeach.d

raw
1 // REQUIRED_ARGS: -o-
2 // EXTRA_FILES: imports/imp12242a1.d imports/imp12242a2.d
3 // PERMUTE_ARGS:
4 /*
5 TEST_OUTPUT:
6 ---
7 9
8 8
9 7
10 6
11 5
12 4
13 3
14 2
15 1
16 0
17 S(1, 2, 3, [0, 1, 2])
18 x0: 1
19 x1: 2
20 x2: 3
21 a: [0, 1, 2]
22 (int[], char[], bool[], Object[])
23 [0, 0]
24 x0: int
25 x1: double
26 x2: char
27 test(0)→ 0
28 test(1)→ 1
29 test(2)→ 2
30 test(3)→ 3
31 test(4)→ 4
32 test(5)→ 5
33 test(6)→ 6
34 test(7)→ 7
35 test(8)→ 8
36 test(9)→ 9
37 test(10)→ -1
38 test(11)→ -1
39 test(12)→ -1
40 test(13)→ -1
41 test(14)→ -1
42 1
43 [1, 2, 3]
44 2
45 [1, 2, 3]
46 3
47 [1, 2, 3]
48 0 1
49 1 2
50 2 3
51 1
52 3
53 4
54 object
55 Tuple
56 tuple
57 main
58 front
59 popFront
60 empty
61 back
62 popBack
63 Iota
64 iota
65 map
66 to
67 text
68 all
69 any
70 join
71 S
72 s
73 Seq
74 Overloads
75 Parameters
76 forward
77 foo
78 A
79 B
80 C
81 D
82 E
83 Types
84 Visitor
85 testVisitor
86 staticMap
87 arrayOf
88 StaticForeachLoopVariable
89 StaticForeachScopeExit
90 StaticForeachReverseHiding
91 UnrolledForeachReverse
92 StaticForeachReverse
93 StaticForeachByAliasDefault
94 NestedStaticForeach
95 TestAliasOutsideFunctionScope
96 OpApplyMultipleStaticForeach
97 OpApplyMultipleStaticForeachLowered
98 RangeStaticForeach
99 OpApplySingleStaticForeach
100 TypeStaticForeach
101 AliasForeach
102 EnumForeach
103 TestUninterpretable
104 SeqForeachConstant
105 SeqForeachBreakContinue
106 TestStaticForeach
107 testtest
108 fun
109 testEmpty
110 bug17660
111 breakContinueBan
112 MixinTemplate
113 testToStatement
114 bug17688
115 T
116 foo2
117 T2
118 TestStaticForeach2
119 issue22007
120 1 2 3
121 2 3 4
122 0 1
123 1 2
124 2 3
125 ---
126 */
127
128 module staticforeach;
129
130 struct Tuple(T...){
131 T expand;
132 alias expand this;
133 }
134 auto tuple(T...)(T t){ return Tuple!T(t); }
135
136 /+struct TupleStaticForeach{ // should work, but is not the fault of the static foreach implementation.
137 //pragma(msg, [tuple(1,"2",'3'),tuple(2,"3",'4')].map!((x)=>x));
138 static foreach(a,b,c;[tuple(1,"2",'3'),tuple(2,"3",'4')].map!((x)=>x)){
139 pragma(msg,a," ",b," ",c);
140 }
141 }+/
142
143 void main(){
144 static foreach(a,b,c;[tuple(1,"2",'3'),tuple(2,"3",'4')].map!((x)=>x)){
145 pragma(msg, a," ",b," ",c);
146 }
147 static struct S{
148 // (aggregate scope, forward referencing possible)
149 static assert(stripA("123")==1);
150 static assert(stripA([1],2)==2);
151 static foreach(i;0..2){
152 mixin(`import imports.imp12242a`~text(i+1)~`;`);
153 static assert(stripA("123")==1);
154 static assert(stripA([1],2)==2);
155 }
156 static assert(stripA("123")==1);
157 static assert(stripA([1],2)==2);
158 }
159 static foreach(i;0..2){
160 // (function scope, no forward referencing)
161 mixin(`import imports.imp12242a`~text(i+1)~`;`);
162 static assert(stripA("123")==1);
163 static if(i) static assert(stripA([1],2)==2);
164 }
165 static assert(stripA("123")==1);
166 static assert(stripA([1],2)==2);
167 }
168
169 auto front(T)(T[] a){ return a[0]; }
170 auto popFront(T)(ref T[] a){ a=a[1..$]; }
171 auto empty(T)(T[] a){ return !a.length; }
172 auto back(T)(T[] a){ return a[$-1]; }
173 auto popBack(T)(ref T[] a){ a=a[0..$-1]; }
174
175 struct Iota(T){
176 T s,e;
177 @property bool empty(){ return s>=e; }
178 @property T front(){ return s; }
179 @property T back(){ return cast(T)(e-1); }
180 void popFront(){ s++; }
181 void popBack(){ e--; }
182 }
183 auto iota(T)(T s, T e){ return Iota!T(s,e); }
184
185 template map(alias a){
186 struct Map(R){
187 R r;
188 @property front(){ return a(r.front); }
189 @property back(){ return a(r.back); }
190 @property bool empty(){ return r.empty; }
191 void popFront(){ r.popFront(); }
192 void popBack(){ r.popBack(); }
193 }
194 auto map(R)(R r){ return Map!R(r); }
195 }
196
197 template to(T:string){
198 string to(S)(S x)if(is(S:int)||is(S:size_t)||is(S:char)){
199 static if(is(S==char)) return cast(string)[x];
200 if(x<0) return "-"~to(-1 * x);
201 if(x==0) return "0";
202 return (x>=10?to(x/10):"")~cast(char)(x%10+'0');
203 }
204 }
205 auto text(T)(T arg){ return to!string(arg); };
206
207 template all(alias a){
208 bool all(R)(R r){
209 foreach(x;r) if(!a(x)) return false;
210 return true;
211 }
212 }
213 template any(alias a){
214 bool any(R)(R r){
215 foreach(x;r) if(a(x)) return true;
216 return false;
217 }
218 }
219 auto join(R)(R r,string sep=""){
220 string a;
221 int first=0;
222 foreach(x;r){
223 if(first++) a~=sep;
224 a~=x;
225 }
226 return a;
227 }
228
229 static foreach_reverse(x;iota(0,10).map!(to!string)){
230 pragma(msg, x);
231 }
232
233 // create struct members iteratively
234 struct S{
235 static foreach(i;a){
236 mixin("int x"~to!string(i)~";");
237 }
238 immutable int[] a = [0,1,2];
239 }
240 enum s=S(1,2,3);
241 pragma(msg, s);
242
243 // loop over struct members
244 static foreach(member;__traits(allMembers,S)){
245 pragma(msg, member,": ",mixin("s."~member));
246 }
247
248 // print prime numbers using overload sets as state variables.
249 /+
250 static assert(is(typeof(bad57)));
251 static assert(!is(typeof(bad53)));
252
253 static foreach(x;iota(2,100)){
254 static foreach(y;iota(2,x)){
255 static if(!(x%y)){
256 mixin("void bad"~to!string(x)~"();");
257 }
258 }
259 static if(!is(typeof(mixin("bad"~to!string(x))))){
260 static assert(iota(2,x).all!(y=>!!(x%y)));
261 pragma(msg, x);
262 }else{
263 static assert(iota(2,x).any!(y=>!(x%y)));
264 }
265 }
266 +/
267
268
269 alias Seq(T...)=T;
270
271 alias Overloads(alias a) = Seq!(__traits(getOverloads, __traits(parent, a), __traits(identifier, a)));
272
273 template Parameters(alias f){
274 static if(is(typeof(f) P == function)) alias Parameters=P;
275 }
276
277 template forward(alias a){
278 enum x=2;
279 static foreach(f;Overloads!a){
280 auto ref forward(Parameters!f args){
281 return f(args);
282 }
283 }
284 enum y=3;
285 }
286
287 int foo(int x){ return x; }
288 string foo(string x){ return x; }
289
290 static assert(forward!foo(2)==2 && forward!foo("hi") == "hi");
291
292
293 // simple boilerplate-free visitor pattern
294 static foreach(char T;'A'..'F'){
295 mixin("class "~T~q{{
296 void accept(Visitor v){
297 return v.visit(this);
298 }
299 }});
300 }
301 alias Types = Seq!(mixin("Seq!("~iota('A','F').map!(to!string).join(", ")~")"));
302 abstract class Visitor{
303 static foreach(T;Types){
304 abstract void visit(T);
305 }
306 }
307
308 string testVisitor(){
309 string r;
310 void writeln(T...)(T args){
311 static foreach(x;args) r~=x;
312 r~='\n';
313 }
314 class Visitor: .Visitor{
315 static foreach(T;Types){
316 override void visit(T){
317 writeln("visited: ",T.stringof);
318 }
319 }
320 }
321 void main(){
322 auto v=new Visitor;
323 static foreach(T;Types){
324 v.visit(new T);
325 }
326 }
327 main();
328 return r;
329 }
330 static assert(testVisitor()=="visited: A
331 visited: B
332 visited: C
333 visited: D
334 visited: E
335 ");
336
337 // iterative computation over AliasSeq:
338 template staticMap(alias F,T...){
339 alias state0=Seq!();
340 static foreach(i,A;T){
341 mixin("alias state"~to!string(i+1)~" = Seq!(state"~to!string(i)~",F!A);");
342 }
343 alias staticMap = Seq!(mixin("state"~to!string(T.length)));
344 }
345
346 alias arrayOf(T)=T[];
347 static assert(is(staticMap!(arrayOf,int,char,bool,Object)==Seq!(int[], char[], bool[], Object[])));
348 pragma(msg, staticMap!(arrayOf,int,char,bool,Object));
349
350
351 struct StaticForeachLoopVariable{
352 int x;
353 static foreach(i;0..1){
354 mixin("enum x"~text(i)~" = i;");
355 }
356 int y;
357 static assert(__traits(allMembers, StaticForeachLoopVariable).length==3);
358 static assert(!is(typeof(StaticForeachLoopVariable.i)));
359 static assert(!is(typeof(__traits(getMember, StaticForeachLoopVariable, "i"))));
360 }
361
362 struct StaticForeachScopeExit{
363 static:
364 int[] test(){
365 int[] r;
366 scope(exit) r ~= 1234;
367 {
368 static foreach(i;0..5){
369 scope(exit) r ~= i;
370 }
371 r ~= 5;
372 }
373 return r;
374 }
375 static assert(test()==[5,4,3,2,1,0]);
376 }
377
378 struct StaticForeachReverseHiding{
379 static foreach(i;[0]){
380 enum i = 1; // TODO: disallow?
381 static assert(i==0);
382 }
383 }
384
385 struct UnrolledForeachReverse{
386 static:
387 alias Seq(T...)=T;
388 int[] test(){
389 int[] r;
390 foreach_reverse(i;Seq!(0,1,2,3)){
391 r~=i;
392 }
393 return r;
394 }
395 static assert(test()==[3,2,1,0]);
396 }
397
398 struct StaticForeachReverse{
399 static:
400 alias Seq(T...)=T;
401 int[] test(){
402 int[] r;
403 static foreach_reverse(i;0..4){
404 r~=i;
405 }
406 return r;
407 }
408 static assert(test()==[3,2,1,0]);
409
410 int[] test2(){
411 int[] r;
412 static foreach_reverse(i;[0,1,2,3]){
413 r~=i;
414 }
415 return r;
416 }
417 static assert(test2()==[3,2,1,0]);
418
419 int[] test3(){
420 static struct S{
421 int opApplyReverse(scope int delegate(int) dg){
422 foreach_reverse(i;0..4) if(auto r=dg(i)) return r;
423 return 0;
424 }
425 }
426 int[] r;
427 static foreach_reverse(i;S()){
428 r~=i;
429 }
430 return r;
431 }
432 static assert(test3()==[3,2,1,0]);
433
434 int[] test4(){
435 int[] r;
436 static foreach_reverse(i;Seq!(0,1,2,3)){
437 r~=i;
438 }
439 return r;
440 }
441 static assert(test()==[3,2,1,0]);
442 }
443
444 struct StaticForeachByAliasDefault{
445 static:
446 alias Seq(T...)=T;
447
448 int[] test(){
449 int a,b,c;
450 static foreach(i,x;Seq!(a,b,c)) x=i;
451 return [a,b,c];
452 }
453 static assert(test()==[0,1,2]);
454
455 int[] test2(){
456 int x=0;
457 int foo(){ return ++x; }
458 static foreach(y;Seq!foo)
459 return [y,y,y];
460 }
461 static assert(test2()==[1,2,3]);
462
463 void test3(){
464 int x=0;
465 int foo(){ return ++x; }
466 static assert(!is(typeof({
467 static foreach(enum y;Seq!foo)
468 return [y,y,y];
469 })));
470 }
471 }
472
473 struct NestedStaticForeach{
474 static:
475 static foreach(i,name;["a"]){
476 static foreach(j,name2;["d"]){
477 mixin("enum int[] "~name~name2~"=[i, j];");
478 }
479 }
480 pragma(msg, ad);
481 }
482
483 struct TestAliasOutsideFunctionScope{
484 static:
485 alias Seq(T...)=T;
486 int a;
487 static foreach(alias x;Seq!(a)){
488 }
489 }
490
491 struct OpApplyMultipleStaticForeach{
492 static:
493 struct OpApply{
494 int opApply(scope int delegate(int,int) dg){
495 foreach(i;0..10) if(auto r=dg(i,i*i)) return r;
496 return 0;
497 }
498 }
499 static foreach(a,b;OpApply()){
500 mixin(`enum x`~cast(char)('0'+a)~"=b;");
501 }
502 static foreach(i;0..10){
503 static assert(mixin(`x`~cast(char)('0'+i))==i*i);
504 }
505 }
506
507
508 struct OpApplyMultipleStaticForeachLowered{
509 static:
510 struct OpApply{
511 int opApply(scope int delegate(int,int) dg){
512 foreach(i;0..10) if(auto r=dg(i,i*i)) return r;
513 return 0;
514 }
515 }
516 static foreach(x;{
517 static struct S(T...){ this(T k){ this.x=k; } T x; }
518 static s(T...)(T a){ return S!T(a); }
519 typeof({ foreach(a,b;OpApply()){ return s(a,b); } assert(0);}())[] r;
520 foreach(a,b;OpApply()) r~=s(a,b);
521 return r;
522 }()){
523 mixin(`enum x`~cast(char)('0'+x.x[0])~"=x.x[1];");
524 }
525 static foreach(i;0..10){
526 static assert(mixin(`x`~cast(char)('0'+i))==i*i);
527 }
528 }
529
530 struct RangeStaticForeach{
531 static:
532 struct Range{
533 int x=0;
534 this(int x){ this.x=x; }
535 @property int front(){ return x; }
536 void popFront(){ x += 2; }
537 @property bool empty(){ return x>=10; }
538 }
539 static foreach(i;Range()){
540 mixin(`enum x`~cast(char)('0'+i)~"=i;");
541 }
542 static foreach(i;0..5){
543 static assert(mixin(`x`~cast(char)('0'+2*i))==2*i);
544 }
545 static assert(!is(typeof({
546 struct S{
547 static foreach(i,k;Range()){}
548 }
549 })));
550 static foreach(k;Range()){} // ok
551 }
552
553 struct OpApplySingleStaticForeach{
554 static:
555 struct OpApply{
556 int opApply(scope int delegate(int) dg){
557 foreach(i;0..10) if(auto r=dg(i)) return r;
558 return 0;
559 }
560 }
561 static foreach(b;OpApply()){
562 mixin(`enum x`~cast(char)('0'+b)~"=b;");
563 }
564 static foreach(i;0..10){
565 static assert(mixin(`x`~cast(char)('0'+i))==i);
566 }
567 }
568
569 struct TypeStaticForeach{
570 static:
571 alias Seq(T...)=T;
572 static foreach(i,alias T;Seq!(int,double,char)){
573 mixin(`T x`~cast(char)('0'+i)~";");
574 }
575 pragma(msg, "x0: ",typeof(x0));
576 pragma(msg, "x1: ",typeof(x1));
577 pragma(msg, "x2: ",typeof(x2));
578 static assert(is(typeof(x0)==int));
579 static assert(is(typeof(x1)==double));
580 static assert(is(typeof(x2)==char));
581 }
582
583 struct AliasForeach{
584 static:
585 alias Seq(T...)=T;
586 int[] test(){
587 int a,b,c;
588 static foreach(x;Seq!(a,b,c,2)){
589 static if(is(typeof({x=2;}))) x=2;
590 }
591 int x,y,z;
592 static foreach(alias k;Seq!(x,y,z,2)){
593 static if(is(typeof({k=2;}))) k=2;
594 }
595 int j,k,l;
596 static assert(!is(typeof({
597 static foreach(ref x;Seq!(j,k,l,2)){
598 static if(is(typeof({x=2;}))) x=2;
599 }
600 })));
601 return [x,y,z];
602 }
603 static assert(test()==[2,2,2]);
604 }
605
606 struct EnumForeach{
607 static:
608 alias Seq(T...)=T;
609 int a=1;
610 int fun(){ return 1; }
611 int gun(){ return 2; }
612 int hun(){ return 3;}
613 auto test(){
614 static foreach(i,enum x;Seq!(fun,gun,hun)){
615 static assert(i+1==x);
616 }
617 foreach(i,enum x;Seq!(fun,gun,hun)){
618 static assert(i+1==x);
619 }
620 }
621 }
622
623 struct TestUninterpretable{
624 static:
625 alias Seq(T...)=T;
626 auto test(){
627 int k;
628 static assert(!is(typeof({
629 static foreach(x;[k]){}
630 })));
631 static assert(!is(typeof({
632 foreach(enum x;[1,2,3]){}
633 })));
634 static assert(!is(typeof({
635 foreach(alias x;[1,2,3]){}
636 })));
637 foreach(enum x;Seq!(1,2,3)){} // ok
638 foreach(alias x;Seq!(1,2,3)){} // ok
639 static foreach(enum x;[1,2,3]){} // ok
640 static foreach(alias x;[1,2,3]){} // ok
641 static assert(!is(typeof({
642 static foreach(enum alias x;[1,2,3]){}
643 })));
644 int x;
645 static foreach(i;Seq!x){ } // ok
646 static foreach(i,j;Seq!(1,2,x)){ } // ok
647 static assert(!is(typeof({
648 static foreach(ref x;[1,2,3]){}
649 })));
650 }
651 }
652
653 struct SeqForeachConstant{
654 static:
655 alias Seq(T...)=T;
656 static assert(!is(typeof({
657 foreach(x;Seq!1) x=2;
658 })));
659 int test2(){
660 int r=0;
661 foreach(x;Seq!(1,2,3)){
662 enum k=x;
663 r+=k;
664 }
665 return r;
666 }
667 static assert(test2()==6);
668 }
669
670 struct SeqForeachBreakContinue{
671 static:
672 alias Seq(T...)=T;
673 int[] test(){
674 int[] r;
675 foreach(i;Seq!(0,1,2,3,4,5)){
676 if(i==2) continue;
677 if(i==4) break;
678 r~=i;
679 }
680 return r;
681 }
682 static assert(test()==[0,1,3]);
683 }
684
685 struct TestStaticForeach{
686 static:
687 int test(int x){
688 int r=0;
689 label: switch(x){
690 static foreach(i;0..10){
691 case i: r=i; break label; // TODO: remove label when restriction is lifted
692 }
693 default: r=-1; break label;
694 }
695 return r;
696 }
697 static foreach(i;0..15){
698 pragma(msg, "test(",i,")→ ",test(i));
699 static assert(test(i)==(i<10?i:-1));
700 }
701
702 enum x=[1,2,3];
703
704 static foreach(i;x){
705 mixin("enum x"~cast(char)('0'+i)~"="~cast(char)('0'+i)~";");
706 }
707
708 static foreach(i;x){
709 pragma(msg, mixin("x"~cast(char)('0'+i)));
710 pragma(msg,x);
711 }
712
713 int[] noBreakNoContinue(){
714 int[] r;
715 static foreach(i;0..1){
716 // if(i==3) continue; // TODO: error?
717 // if(i==7) break; // TODO: error?
718 r~=i;
719 }
720 return r;
721 }
722
723 mixin("enum k=3;");
724 }
725
726 static foreach(i,j;[1,2,3]){
727 pragma(msg, int(i)," ",j);
728 }
729
730 void testtest(){
731 static foreach(i,v;[1,2,3]){
732 pragma(msg, int(i)," ",v);
733 static assert(i+1 == v);
734 }
735 }
736
737
738 static foreach(i;Seq!(1,2,3,4,int)){
739 static if(!is(i) && i!=2){
740 pragma(msg, i);
741 }
742 }
743
744 int fun(int x){
745 int r=0;
746 label: switch(x){
747 static foreach(i;Seq!(0,1,2,3,4,5,6)){
748 static if (i < 5)
749 case i: r=i; break label; // TODO: remove label when restriction is lifted
750 }
751 default: r=-1; break label;
752 }
753 return r;
754 }
755
756 static foreach(i;0..10) static assert(fun(i)==(i<5?i:-1));
757
758 static foreach(i;0..0) { }
759 void testEmpty(){
760 static foreach(i;0..0) { }
761 }
762
763 auto bug17660(){
764 int x;
765 static foreach (i; 0 .. 1) { return 3; }
766 return x;
767 }
768 static assert(bug17660()==3);
769
770 int breakContinueBan(){
771 static assert(!is(typeof({
772 for(;;){
773 static foreach(i;0..1){
774 break;
775 }
776 }
777 })));
778 static assert(!is(typeof({
779 for(;;){
780 static foreach(i;0..1){
781 continue;
782 }
783 }
784 })));
785 Louter1: for(;;){
786 static foreach(i;0..1){
787 break Louter1;
788 }
789 }
790 Louter2: foreach(i;0..10){
791 static foreach(j;0..1){
792 continue Louter2;
793 }
794 return 0;
795 }
796 static foreach(i;0..1){
797 for(;;){ break; } // ok
798 }
799 return 1;
800 }
801 static assert(breakContinueBan()==1);
802
803 mixin template MixinTemplate(){
804 static foreach(i;0..2){
805 mixin(`enum x`~cast(char)('0'+i)~"=i;");
806 }
807 static foreach(i;[0,1]){
808 mixin(`enum y`~cast(char)('0'+i)~"=i;");
809 }
810 }
811 void testToStatement(){
812 mixin MixinTemplate;
813 static assert(x0==0 && x1==1);
814 static assert(y0==0 && y1==1);
815 }
816
817 void bug17688(){
818 final switch(1) static foreach(x;0..1){ int y=3; case 1: return; }
819 static assert(!is(typeof(y)));
820 }
821
822 struct T{ enum n = 1; }
823 T foo(T v)@nogc{
824 static foreach(x;0..v.n){ }
825 return T.init;
826 }
827 T foo2(T v)@nogc{
828 static foreach(_;0..typeof(return).n){ }
829 return T.init;
830 }
831
832 //https://issues.dlang.org/show_bug.cgi?id=18698
833
834 static foreach(m; __traits(allMembers, staticforeach))
835 {
836 pragma(msg, m);
837 }
838
839 //https://issues.dlang.org/show_bug.cgi?id=20072
840 struct T2{
841 static foreach(i;0..1)
842 struct S{}
843 }
844 static assert(is(__traits(parent,T2.S)==T2));
845
846 struct TestStaticForeach2
847 {
848 static:
849 // StringExp
850 char[] test(string str)()
851 {
852 char[] s;
853 static foreach (c; str)
854 {
855 s ~= c;
856 }
857 return s;
858 }
859 static assert(test!"tёstñ" == ['t', '\xd1', '\x91', 's', 't', '\xc3', '\xb1']);
860
861 static foreach (c; "")
862 {
863 static assert(0);
864 }
865
866 // NullExp
867 enum int[] a = null;
868 static foreach (c; a)
869 {
870 static assert(0);
871 }
872 }
873
874 //https://issues.dlang.org/show_bug.cgi?id=22007
875 void issue22007()
876 {
877 immutable int[32] array = 1;
878 foreach (size_t a, int b; array) {}
879 static foreach (size_t a, int b; array) { }
880 }