Debian Codegraph @

gcc/testsuite/gdc.test/runnable/inline.d

raw
1 /*
2 RUN_OUTPUT:
3 ---
4 7
5 83
6 4
7 4
8 1.000000 2.000000 3.000000
9 Success
10 ---
11 */
12
13 import core.stdc.stdio;
14
15 // Test function inlining
16
17 debug = NRVO;
18
19 /************************************/
20
21 int foo(int i)
22 {
23 return i;
24 }
25
26 int bar()
27 {
28 return foo(3) + 4;
29 }
30
31 void test1()
32 {
33 printf("%d\n", bar());
34 assert(bar() == 7);
35 }
36
37
38 /************************************/
39
40 struct Foo2
41 {
42 int a,b,c,e,f,g;
43 }
44
45
46 int foo2(Foo2 f)
47 {
48 f.b += 73;
49 return f.b;
50 }
51
52 int bar2()
53 {
54 Foo2 gg;
55
56 gg.b = 6;
57 return foo2(gg) + 4;
58 }
59
60 void test2()
61 {
62 printf("%d\n", bar2());
63 assert(bar2() == 83);
64 }
65
66
67 /************************************/
68
69 struct Foo3
70 {
71 int bar() { return y + 3; }
72 int y = 4;
73 }
74
75 void test3()
76 {
77 Foo3 f;
78
79 assert(f.bar() == 7);
80 }
81
82
83 /************************************/
84
85 void func(void function () v)
86 {
87 }
88
89 void test4()
90 {
91 static void f1() { }
92
93 func(&f1);
94 //func(f1);
95 }
96
97
98 /************************************/
99
100 void foo5(ubyte[16] array)
101 {
102 bar5(array.ptr);
103 }
104
105 void bar5(ubyte *array)
106 {
107 }
108
109 void abc5(ubyte[16] array)
110 {
111 foo5(array);
112 }
113
114 void test5()
115 {
116 }
117
118 /************************************/
119
120 struct Struct
121 {
122 real foo()
123 {
124 return 0;
125 }
126
127 void bar(out Struct Q)
128 {
129 if (foo() < 0)
130 Q = this;
131 }
132 }
133
134 void test6()
135 {
136 }
137
138 /************************************/
139
140 struct S7(T)
141 {
142 immutable(T)[] s;
143 }
144
145 T foo7(T)(T t)
146 {
147 enum S7!(T)[] i = [{"hello"},{"world"}];
148 auto x = i[0].s;
149 return t;
150 }
151
152 void test7()
153 {
154 auto x = foo7('c');
155 }
156
157 /************************************/
158
159 // https://issues.dlang.org/show_bug.cgi?id=10833
160 string fun10833(T...)()
161 {
162 foreach (v ; T)
163 return v;
164 assert(0);
165 }
166
167 void test10833()
168 {
169 auto a = fun10833!("bar")();
170 }
171
172 /************************************/
173 // https://issues.dlang.org/show_bug.cgi?id=4825
174
175 int a8() {
176 int r;
177 return r;
178 }
179
180 int b8() {
181 return a8();
182 }
183
184 void test8() {
185 void d() {
186 auto e = b8();
187 }
188 static const int f = b8();
189 }
190
191 /************************************/
192 // https://issues.dlang.org/show_bug.cgi?id=4841
193
194 auto fun4841a()
195 {
196 int i = 42;
197 struct Result
198 {
199 this(int u) {}
200 auto bar()
201 {
202 // refer context of fun4841a
203 return i;
204 }
205 }
206 return Result();
207 }
208 void test4841a()
209 {
210 auto t = fun4841a();
211 auto x = t.bar();
212 assert(x == 42);
213 }
214
215 auto fun4841b()
216 {
217 int i = 40;
218 auto foo() // hasNestedFrameRefs() == false
219 {
220 //
221 struct Result
222 {
223 this(int u) {}
224 auto bar()
225 {
226 // refer context of fun4841b
227 return i + 2;
228 }
229 }
230 return Result();
231 }
232 return foo();
233 }
234 void test4841b()
235 {
236 auto t = fun4841b();
237 assert(cast(void*)t.tupleof[$-1] !is null); // Result to fun4841b
238 auto x = t.bar();
239 assert(x == 42);
240 }
241
242 auto fun4841c()
243 {
244 int i = 40;
245 auto foo() // hasNestedFrameRefs() == true
246 {
247 int g = 2;
248 struct Result
249 {
250 this(int u) {}
251 auto bar()
252 {
253 // refer context of fun4841c and foo
254 return i + g;
255 }
256 }
257 return Result();
258 }
259 return foo();
260 }
261 void test4841c()
262 {
263 auto t = fun4841c();
264 assert( cast(void*)t.tupleof[$-1] !is null); // Result to foo
265 assert(*cast(void**)t.tupleof[$-1] !is null); // foo to fun4841c
266 auto x = t.bar();
267 assert(x == 42);
268 }
269
270 void test4841()
271 {
272 test4841a();
273 test4841b();
274 test4841c();
275 }
276
277 /************************************/
278 // https://issues.dlang.org/show_bug.cgi?id=7261
279
280 struct AbstractTask
281 {
282 ubyte taskStatus;
283 }
284
285 struct Task
286 {
287 AbstractTask base;
288 alias base this;
289
290 void opAssign(Task rhs)
291 {
292 }
293
294 ~this()
295 {
296 if (taskStatus != 3) { }
297 }
298 }
299
300 /************************************/
301 // https://issues.dlang.org/show_bug.cgi?id=9356
302
303 void test9356()
304 {
305 static inout(char)[] bar (inout(char)[] a)
306 {
307 return a;
308 }
309
310 string result;
311 result ~= bar("abc");
312 assert(result == "abc");
313 }
314
315 /************************************/
316 // https://issues.dlang.org/show_bug.cgi?id=12079
317
318 void test12079()
319 {
320 string[string][string] foo;
321
322 foo.get("bar", null).get("baz", null);
323 }
324
325 /************************************/
326 // https://issues.dlang.org/show_bug.cgi?id=12243
327
328 char f12243() { return 'a'; }
329
330 void test12243()
331 {
332 string s;
333 s ~= f12243();
334 }
335
336 /************************************/
337 // https://issues.dlang.org/show_bug.cgi?id=11201
338
339 struct Foo11201
340 {
341 int a;
342 float b;
343
344 Foo11201 func()() const { return this; }
345 }
346
347 auto f11201()(Foo11201 a) { return a; }
348
349 void test11201()
350 {
351 auto a = Foo11201(0, 1);
352
353 assert(f11201(a.func!()()) == a);
354 }
355
356 /************************************/
357 // https://issues.dlang.org/show_bug.cgi?id=11223
358
359 struct Tuple11223(T...)
360 {
361 T values;
362
363 void opAssign(Tuple11223 rhs)
364 {
365 if (0)
366 values = rhs.values;
367 else
368 assert(1);
369 }
370 }
371
372 void test11223()
373 {
374 Tuple11223!string tmp;
375 tmp = Tuple11223!string();
376 }
377
378 /************************************/
379
380
381 void foo3918()
382 {
383 import core.stdc.stdlib : alloca;
384 void[] mem = alloca(1024)[0..1024];
385 }
386
387 void test3918()
388 {
389 foreach(i; 0 .. 10_000_000)
390 {
391 foo3918();
392 }
393 }
394
395 /************************************/
396 // https://issues.dlang.org/show_bug.cgi?id=11314
397
398 struct Tuple11314(T...)
399 {
400 T values;
401
402 void opAssign(typeof(this) rhs)
403 {
404 if (0)
405 values[] = rhs.values[];
406 else
407 assert(1);
408 }
409 }
410
411 struct S11314 {}
412
413 void test11314()
414 {
415 Tuple11314!S11314 t;
416 t = Tuple11314!S11314(S11314.init);
417 }
418
419 /************************************/
420 // https://issues.dlang.org/show_bug.cgi?id=11224
421
422 S11224* ptr11224;
423
424 struct S11224
425 {
426 this(int)
427 {
428 ptr11224 = &this;
429 /*printf("ctor &this = %p\n", &this);*/
430 }
431 this(this)
432 {
433 /*printf("cpctor &this = %p\n", &this);*/
434 }
435 int num;
436 }
437 S11224 foo11224()
438 {
439 S11224 s = S11224(1);
440 //printf("foo &this = %p\n", &s);
441 assert(ptr11224 is &s);
442 return s;
443 }
444 void test11224()
445 {
446 auto s = foo11224();
447 //printf("main &this = %p\n", &s);
448 assert(ptr11224 is &s);
449 }
450
451 /************************************/
452 // https://issues.dlang.org/show_bug.cgi?id=11322
453
454 bool b11322;
455 uint n11322;
456
457 ref uint fun11322()
458 {
459 if (b11322)
460 return n11322;
461 else
462 return n11322;
463 }
464
465 void test11322()
466 {
467 fun11322()++;
468 assert(n11322 == 1);
469 fun11322() *= 5;
470 assert(n11322 == 5);
471 }
472
473 /************************************/
474 // https://issues.dlang.org/show_bug.cgi?id=11394
475
476 debug(NRVO) static void* p11394a, p11394b, p11394c;
477
478 static int[5] make11394(in int x) pure
479 {
480 typeof(return) a;
481 a[0] = x;
482 a[1] = x + 1;
483 a[2] = x + 2;
484 a[3] = x + 3;
485 a[4] = x + 4;
486 debug(NRVO) p11394a = cast(void*)a.ptr;
487 return a;
488 }
489
490 struct Bar11394
491 {
492 immutable int[5] arr;
493
494 this(int x)
495 {
496 this.arr = make11394(x); // NRVO should work
497 debug(NRVO) p11394b = cast(void*)this.arr.ptr;
498 }
499 }
500
501 void test11394()
502 {
503 auto b = Bar11394(5);
504 debug(NRVO) p11394c = cast(void*)b.arr.ptr;
505 //debug(NRVO) printf("p1 = %p\np2 = %p\np3 = %p\n", p11394a, p11394b, p11394c);
506 debug(NRVO) assert(p11394a == p11394b);
507 debug(NRVO) assert(p11394b == p11394c);
508 }
509
510 /**********************************/
511 // https://issues.dlang.org/show_bug.cgi?id=12080
512
513 class TZ12080 {}
514
515 struct ST12080
516 {
517 ST12080 opBinary()() const pure nothrow
518 {
519 auto retval = ST12080();
520 return retval; // NRVO
521 }
522
523 long _stdTime;
524 immutable TZ12080 _timezone;
525 }
526
527 class Foo12080
528 {
529
530 ST12080 bar;
531 bool quux;
532
533 public ST12080 sysTime()
534 out {}
535 do
536 {
537 if (quux)
538 return ST12080();
539
540 return bar.opBinary();
541 // returned value is set to __result
542 // --> Inliner wrongly created the second DeclarationExp for __result.
543 }
544 }
545
546 /**********************************/
547 // https://issues.dlang.org/show_bug.cgi?id=13503
548
549 void f13503a(string[] s...)
550 {
551 assert(s[0] == "Cheese");
552 }
553
554 auto f13503b(string arg)
555 {
556 string result = arg;
557 return result;
558 }
559
560 string f13503c(string arg)
561 {
562 string result = arg;
563 return result;
564 }
565
566 void test13503()
567 {
568 f13503a(f13503b("Cheese"));
569 f13503a(f13503c("Cheese"));
570 }
571
572 /**********************************/
573 // https://issues.dlang.org/show_bug.cgi?id=14267
574
575 // EXTRA_SOURCES: imports/a14267.d
576 import imports.a14267;
577
578 void test14267()
579 {
580 foreach (m; __traits(allMembers, SysTime14267))
581 {
582 static if (is(typeof(__traits(getMember, SysTime14267, m))))
583 {
584 foreach (func; __traits(getOverloads, SysTime14267, m))
585 {
586 auto prot = __traits(getProtection, func);
587 static if (__traits(isStaticFunction, func))
588 {
589 static assert(func.stringof == "min()");
590 auto result = func;
591 }
592 }
593 }
594 }
595 }
596
597 /**********************************/
598 // https://issues.dlang.org/show_bug.cgi?id=13244
599
600 struct MapResult13244(alias fun)
601 {
602 int[] input;
603 @property front() { return fun(input[0]); }
604 }
605
606 int[] array13244(R)(R r)
607 {
608 int[] a;
609 a ~= r.front;
610 return a;
611 }
612
613 void test13244()
614 {
615 auto arr = [[cast(ubyte)1]];
616 foreach (ref x; arr)
617 {
618 auto m = MapResult13244!(c => x[c])([0]);
619 array13244(m);
620 }
621 }
622
623 /**********************************/
624 // https://issues.dlang.org/show_bug.cgi?id=14306
625
626 struct MapResult(alias fun)
627 {
628 void front()
629 {
630 // while (1) { break; }
631 fun(1);
632 }
633 }
634
635 void bar(R)(R r)
636 {
637 foreach (i; 0..100)
638 {
639 r.front();
640 }
641 }
642
643 struct S
644 {
645 int x;
646 int bump()
647 {
648 while (1) { break; }
649 ++x;
650 return x;
651 }
652 }
653
654 void fun(ref S s)
655 {
656 MapResult!(y => s.bump())().bar;
657 // MapResult!((int x) => s.bump())().bar;
658
659 if (s.x != 100)
660 assert(0);
661 }
662
663 void test14306()
664 {
665 S t;
666 fun(t);
667 }
668
669 /**********************************/
670 // https://issues.dlang.org/show_bug.cgi?id=14754
671
672 auto aafunc14754(string k)
673 {
674 enum aa = [ "K": "V" ];
675 auto p = k in aa;
676 return null;
677 }
678
679 struct MapResult14754(alias fun, R)
680 {
681 R _input;
682
683 @property auto ref front()
684 {
685 return fun(_input[0]);
686 }
687 }
688
689 auto array14754(R)(R r)
690 {
691 alias E = typeof(r.front);
692 E[] result;
693 result ~= r.front;
694 return result;
695 }
696
697 auto mapfun14754(R)(R words, string k)
698 {
699 return array14754(MapResult14754!(s => aafunc14754(k), R)(words));
700 }
701
702 void test14754()
703 {
704 auto r = mapfun14754([""], "");
705 }
706
707 /**********************************/
708 // https://issues.dlang.org/show_bug.cgi?id=14606
709
710 struct S14606
711 {
712 this(long stdTime)
713 {
714 _stdTime = stdTime;
715 }
716
717 long _stdTime;
718 }
719
720 S14606 getS14606()
721 {
722 S14606 sysTime = S14606(0);
723 return sysTime;
724 }
725
726 struct T14606
727 {
728 this(string)
729 {
730 uint[3] arr;
731 s = getS14606();
732 }
733
734 S14606 s;
735 }
736
737 void test14606()
738 {
739 auto t = T14606(null);
740 }
741
742 /**********************************/
743 // https://issues.dlang.org/show_bug.cgi?id=14753
744
745 pragma(inline)
746 void test14753(string) { }
747
748 /**********************************/
749
750 struct S14975
751 {
752 int bar;
753
754 pragma(inline, true) this(int bar)
755 {
756 this.bar = bar;
757 }
758 }
759
760 void test14975()
761 {
762 S14975 baz = 1;
763 if (baz.bar != 1)
764 assert(0);
765 }
766
767 /**********************************/
768 // https://issues.dlang.org/show_bug.cgi?id=15210
769
770 struct BigInt15210 {}
771
772 struct Tuple15210(Types...)
773 {
774 Types field;
775
776 void opAssign(R)(R rhs)
777 {
778 field = rhs.field;
779 }
780 }
781
782 void test15210()
783 {
784 alias X = Tuple15210!BigInt15210;
785
786 X[BigInt15210] cache;
787
788 auto x = X();
789
790 cache[BigInt15210()] = x;
791 }
792
793 /**********************************/
794
795 int foo7625(int v)
796 {
797 return bar7625(2 * v);
798 }
799
800 int bar7625(int a)
801 {
802 ++a;
803 if (a > 0)
804 return 1;
805 return baz(a);
806 }
807
808 int baz(int a)
809 {
810 if (a > 0)
811 throw new Exception("a > 0");
812 return a - 1;
813 }
814
815 void test7625()
816 {
817 int x = foo7625(1);
818 if (x != 1)
819 assert(0);
820 }
821
822 /**********************************/
823 // https://issues.dlang.org/show_bug.cgi?id=9785 partial fix
824
825 void test9785()
826 {
827 int j = 3;
828
829 void loop(scope const void function(int x) dg) {
830 pragma(inline, true);
831 dg(++j);
832 }
833
834 loop((x) {
835 pragma(inline, true);
836 printf("%d\n", x);
837 assert(x == 4);
838 });
839 }
840
841
842 /**********************************/
843 // https://issues.dlang.org/show_bug.cgi?id=9785 partial fix
844
845 void test9785_2() {
846 int j = 3;
847
848 void loop(scope const void function(int x) dg) {
849 pragma(inline, true);
850 dg(++j);
851 }
852
853 static void func(int x) {
854 pragma(inline, true);
855 printf("%d\n", x);
856 assert(x == 4);
857 }
858
859 loop(&func);
860 }
861
862 /**********************************/
863 // https://issues.dlang.org/show_bug.cgi?id=9785 partial fix
864
865 void test9785_3() @nogc
866 {
867 int j = 3;
868
869 void loop(scope const void delegate(int x) @nogc dg) @nogc {
870 pragma(inline, true);
871 dg(++j);
872 }
873
874 loop((x) @nogc {
875 pragma(inline, true);
876 //printf("%d\n", x + j * 2);
877 assert(x == 4);
878 assert(j == 4);
879 });
880
881 j = 3;
882 void func(int x) @nogc {
883 pragma(inline, true);
884 //printf("%d\n", x + j * 2);
885 assert(x == 4);
886 assert(j == 4);
887 }
888
889 loop(&func);
890 }
891
892 /**********************************/
893 // https://issues.dlang.org/show_bug.cgi?id=15207
894
895 struct Vec15207
896 {
897 float x, y, z;
898
899 this(float x_, float y_, float z_)
900 {
901 x = x_;
902 y = y_;
903 z = z_;
904 }
905
906 Vec15207 clone()
907 {
908 // When the variable 'res' is replaced with a STCref temporary,
909 // this line was accidentally changed to reference initialization.
910 Vec15207 res = this;
911
912 return res;
913 }
914 }
915
916 class C15207
917 {
918 Vec15207 a;
919
920 this()
921 {
922 a = Vec15207(1, 2, 3).clone();
923
924 assert(a.x == 1);
925 assert(a.y == 2);
926 assert(a.z == 3);
927 printf("%f %f %f\n", a.x, a.y, a.z);
928 }
929 }
930
931 void test15207()
932 {
933 auto c = new C15207();
934 }
935
936 /**********************************/
937 // https://issues.dlang.org/show_bug.cgi?id=15253
938
939 struct MessageType15253
940 {
941 MessageType15253[] messageTypes;
942
943 const void toString1(scope void delegate(const(char)[]) sink)
944 {
945 messageTypes[0].toString1(sink);
946 }
947 }
948
949 struct ProtoPackage15253
950 {
951 MessageType15253[] messageTypes;
952
953 const void toString1(scope void delegate(const(char)[]) sink)
954 {
955 messageTypes[0].toString1(sink);
956 }
957 }
958
959 /**********************************/
960 // https://issues.dlang.org/show_bug.cgi?id=15296
961
962 static int x15296;
963
964 struct S15296
965 {
966 // Can be expanded only as statements.
967 pragma(inline, true)
968 void bar(size_t , size_t )
969 {
970 for (size_t w = 0; w < 2; w++) { ++x15296; }
971 }
972
973 pragma(inline, true)
974 void foo(size_t a, size_t b)
975 {
976 bar(a, b);
977 }
978 }
979
980 pragma(inline, true)
981 static void voidCall15296()
982 {
983 for (size_t w = 0; w < 3; w++) { ++x15296; }
984 }
985
986 void test15296()
987 {
988 bool cond = true;
989
990 S15296 s;
991
992 // CallExp at the top of ExpStatement
993 x15296 = 0;
994 s.foo(0, 0);
995 assert(x15296 == 2);
996
997 // CondExp at the top of ExpStatement
998 x15296 = 0;
999 (cond ? s.foo(0, 0) : voidCall15296());
1000 assert(x15296 == 2);
1001 (cond ? voidCall15296() : s.foo(0, 0));
1002 assert(x15296 == 2 + 3);
1003
1004 // CommaExp at the top of ExpStatement
1005 x15296 = 0;
1006 (s.foo(0, 0), voidCall15296());
1007 assert(x15296 == 3 + 2);
1008 }
1009
1010 // ----
1011
1012 struct File15296
1013 {
1014 struct Impl {}
1015 Impl* _p;
1016
1017 pragma(inline, true)
1018 ~this() { _p = null; }
1019
1020 struct LockingTextWriter
1021 {
1022 pragma(inline, true)
1023 this(ref File15296 f)
1024 {
1025 assert(f._p, "Attempting to write to closed File");
1026 }
1027 }
1028
1029 pragma(inline, true)
1030 auto lockingTextWriter() { return LockingTextWriter(this); }
1031
1032 pragma(inline, true)
1033 void write() { auto w = lockingTextWriter(); }
1034
1035 //pragma(inline, true)
1036 static uint formattedWrite(Writer)(Writer w) { return 0; }
1037
1038 pragma(inline, true)
1039 void writef() { formattedWrite(lockingTextWriter()); }
1040 }
1041
1042 __gshared File15296 stdout15296 = {new File15296.Impl()};
1043
1044 pragma(inline, true)
1045 @property File15296 trustedStdout15296() { return stdout15296; }
1046
1047 // ----
1048 // reduced case from runnable/test34.d test34()
1049
1050 void test15296b()
1051 {
1052 // trustedStdout() returns a temporary File object. Its dtor call
1053 // should be deferred till the end of expanded writef body statements.
1054 trustedStdout15296().writef();
1055 }
1056
1057 // ----
1058 // reduced case from runnable/xtest46.d test136()
1059
1060 struct Perm15296c
1061 {
1062 this(byte[] input)
1063 {
1064 foreach (elem; input)
1065 {
1066 // if vthis.isDataseg() is true in expandInline,
1067 // its edtor should not be called.
1068 stdout15296.write();
1069 }
1070 }
1071 }
1072
1073 void test15296c()
1074 {
1075 auto perm2 = Perm15296c([0, 1, 2]);
1076 }
1077
1078 /**********************************/
1079 // https://issues.dlang.org/show_bug.cgi?id=17676
1080 __gshared bool bgEnable = 1;
1081
1082 void test17676() nothrow
1083 {
1084 fullcollect();
1085 }
1086
1087 size_t fullcollect() nothrow
1088 {
1089 if(bgEnable)
1090 return fullcollectTrigger();
1091
1092 return fullcollectNow();
1093 }
1094
1095 size_t fullcollectNow() nothrow
1096 {
1097 if (bgEnable)
1098 assert(0);
1099 pragma(inline, false);
1100 return 1;
1101 }
1102
1103 size_t fullcollectTrigger() nothrow
1104 {
1105 pragma(inline, false);
1106 return 0;
1107 }
1108
1109 /**********************************/
1110
1111 int main()
1112 {
1113 test1();
1114 test2();
1115 test3();
1116 test3918();
1117 test4();
1118 test5();
1119 test9356();
1120 test6();
1121 test7();
1122 test8();
1123 test4841();
1124 test11201();
1125 test11223();
1126 test11314();
1127 test11224();
1128 test11322();
1129 test11394();
1130 test13503();
1131 test13244();
1132 test14306();
1133 test14754();
1134 test14606();
1135 test14975();
1136 test15210();
1137 test7625();
1138 test9785();
1139 test9785_2();
1140 test9785_3();
1141 test15207();
1142 test15296();
1143 test15296b();
1144 test15296c();
1145 test17676();
1146
1147 printf("Success\n");
1148 return 0;
1149 }