1 /* 2 TEST_OUTPUT: 3 --- 4 runnable/mixin1.d(959): Deprecation: The `delete` keyword has been deprecated. Use `object.destroy()` (and `core.memory.GC.free()` if applicable) instead. 5 --- 6 7 RUN_OUTPUT: 8 --- 9 Foo3.func() 10 Code3.func() 11 Foo4.func() 12 Foo5.func() 13 b.x = 5 14 x = 5 15 duff_for(1, 11) 16 fid = 1, 2 17 foo12 18 foo12 19 foo13 j = 1 20 foo13 j = 1 21 x14 = 6 22 x15 = 6 23 bar15() = 5 24 x16 = 6 25 bar() = 5 26 x17 = 5 27 b.x17 = 5 28 x17 = 3 29 x17 = 5 30 x17 = 4 31 x17 = 3 32 x17 = 5 33 in C20.f() 34 B22.foo() 35 5 36 5 37 a = 0 38 int 39 int 40 int 41 int 42 foo 1 43 foo 2 44 opCall 1 45 opCall 2 46 0 0 47 two 48 one 49 one 50 Class39 dtor 51 Mixed-in dtor 52 Mixed-in dtor 53 Base39 dtor 54 Success 55 --- 56 */ 57 58 module mixin1; 59 60 import core.stdc.stdio; 61 62 alias TypeTuple(T...) = T; 63 64 /*******************************************/ 65 66 mixin template Foo(T) 67 { 68 T x; 69 } 70 71 mixin Foo!(uint); 72 73 struct Bar 74 { 75 template Abc(T) 76 { 77 T y; 78 } 79 80 template Def(T) 81 { 82 T z; 83 } 84 } 85 86 mixin Bar.Abc!(int); 87 88 Bar b; 89 mixin typeof(b).Def!(int); 90 91 void test1() 92 { 93 x = 3; 94 assert(x == 3); 95 y = 4; 96 assert(y == 4); 97 z = 5; 98 assert(z == 5); 99 } 100 101 /*******************************************/ 102 103 template Foo2(T) 104 { 105 T x2 = T.sizeof; 106 } 107 108 mixin Foo2!(uint) B2; 109 mixin Foo2!(long) C2; 110 mixin Foo2!(int); 111 112 void test2() 113 { 114 B2.x2 = 3; 115 assert(B2.x2 == 3); 116 assert(C2.x2 == long.sizeof); 117 // assert(x2 == int.sizeof); 118 } 119 120 /*******************************************/ 121 122 template Foo3(T) 123 { 124 int func() { printf("Foo3.func()\n"); return 1; } 125 } 126 127 class Bar3 128 { 129 mixin Foo3!(int); 130 } 131 132 class Code3 : Bar3 133 { 134 override int func() { printf("Code3.func()\n"); return 2; } 135 } 136 137 void test3() 138 { 139 int i; 140 141 Bar3 b = new Bar3(); 142 i = b.func(); 143 assert(i == 1); 144 145 b = new Code3(); 146 i = b.func(); 147 assert(i == 2); 148 } 149 150 /*******************************************/ 151 152 template Foo4(T) 153 { 154 int func() { printf("Foo4.func()\n"); return 1; } 155 } 156 157 struct Bar4 158 { 159 mixin Foo4!(int); 160 } 161 162 void test4() 163 { 164 int i; 165 166 Bar4 b; 167 i = b.func(); 168 assert(i == 1); 169 } 170 171 /*******************************************/ 172 173 template Foo5() 174 { 175 int func() { printf("Foo5.func()\n"); return 1; } 176 } 177 178 struct Bar5 179 { 180 mixin Foo5; 181 } 182 183 void test5() 184 { 185 int i; 186 187 Bar5 b; 188 i = b.func(); 189 assert(i == 1); 190 } 191 192 /*******************************************/ 193 194 template Foo6() 195 { 196 int x = 5; 197 } 198 199 struct Bar6 200 { 201 mixin Foo6; 202 } 203 204 void test6() 205 { 206 int i; 207 208 Bar6 b; 209 i = b.x; 210 assert(i == 5); 211 assert(b.sizeof == int.sizeof); 212 } 213 214 /*******************************************/ 215 216 template Foo7() 217 { 218 int x = 5; 219 } 220 221 class Bar7 222 { 223 int y = 6; 224 mixin Foo7; 225 } 226 227 void test7() 228 { 229 int i; 230 231 Bar7 b = new Bar7(); 232 i = b.x; 233 printf("b.x = %d\n", b.x); 234 assert(i == 5); 235 } 236 237 /*******************************************/ 238 239 template Foo8() 240 { 241 int x = 5; 242 int bar() { return 7; } 243 } 244 245 void test8() 246 { 247 mixin Foo8; 248 printf("x = %d\n", x); 249 assert(x == 5); 250 assert(bar() == 7); 251 } 252 253 /*******************************************/ 254 255 template Foo9() 256 { 257 int abc() { return y; } 258 } 259 260 void test9() 261 { 262 int y = 8; 263 mixin Foo9; 264 assert(abc() == 8); 265 } 266 267 /*******************************************/ 268 269 template Foo10(alias b) 270 { 271 typeof(b) abc() { return b; } 272 } 273 274 void test10() 275 { 276 int y = 8; 277 mixin Foo10!(y); 278 assert(abc() == 8); 279 } 280 281 282 /*******************************************/ 283 284 template Foo11(alias b) 285 { 286 int abc() { return b; } 287 } 288 289 void test11() 290 { 291 int y = 8; 292 mixin Foo11!(y) B; 293 assert(B.abc() == 8); 294 } 295 296 /*******************************************/ 297 298 template duff_for(alias id1, alias id2, alias s) 299 { 300 301 void duff_for() 302 { 303 printf("duff_for(%d, %d)\n", id1, id2); 304 typeof(id1) id = id1; 305 printf("fid = %d, %d\n", id, (id2 - id) % 8); 306 switch ((id2 - id) % 8) 307 { 308 case 0: 309 while (id != id2) 310 { 311 printf("wid = %d\n", id); 312 s(); ++id; 313 goto case; 314 case 7: s(); ++id; goto case; 315 case 6: s(); ++id; goto case; 316 case 5: s(); ++id; goto case; 317 case 4: s(); ++id; goto case; 318 case 3: s(); ++id; goto case; 319 case 2: s(); ++id; goto case; 320 case 1: s(); ++id; 321 break; 322 default: assert(0); 323 } 324 } 325 } 326 } 327 328 void foo12() { printf("foo12\n"); } 329 330 void test12() 331 { 332 int i = 1; 333 int j = 11; 334 335 mixin duff_for!(i, j, delegate void() { foo12(); }); 336 duff_for(); 337 } 338 339 /*******************************************/ 340 341 template duff(alias id1, alias id2, alias s) 342 { 343 344 void duff() 345 { 346 s(); 347 s(); 348 } 349 } 350 351 void foo13(int j) 352 { 353 printf("foo13 j = %d\n", j); 354 assert(j == 1); 355 } 356 357 void test13() 358 { 359 int i = 1; 360 int j = 11; 361 362 mixin duff!(i, j, delegate { foo13(i); }); 363 duff(); 364 } 365 366 /*******************************************/ 367 368 template Foo14() 369 { 370 int x14 = 5; 371 } 372 373 void test14() 374 { 375 int x14 = 6; 376 mixin Foo14; 377 printf("x14 = %d\n", x14); 378 assert(x14 == 6); 379 } 380 381 /*******************************************/ 382 383 template Foo15() 384 { 385 int x15 = 5; 386 387 int bar15() { return x15; } 388 } 389 390 int x15 = 6; 391 mixin Foo15; 392 393 void test15() 394 { 395 396 printf("x15 = %d\n", x15); 397 printf("bar15() = %d\n", bar15()); 398 assert(x15 == 6); 399 assert(bar15() == 5); 400 } 401 402 /*******************************************/ 403 404 template Foo16() 405 { 406 int x16 = 5; 407 408 int bar() { return x16; } 409 } 410 411 mixin Foo16 A16; 412 int x16 = 6; 413 mixin Foo16 B16; 414 415 void test16() 416 { 417 418 printf("x16 = %d\n", x16); 419 printf("bar() = %d\n", A16.bar()); 420 assert(x16 == 6); 421 assert(A16.x16 == 5); 422 assert(B16.x16 == 5); 423 assert(A16.bar() == 5); 424 assert(B16.bar() == 5); 425 } 426 427 /*******************************************/ 428 429 template Foo17() 430 { 431 int x17 = 5; 432 } 433 434 mixin Foo17; 435 436 struct Bar17 437 { 438 mixin Foo17; 439 } 440 441 void test17() 442 { 443 printf("x17 = %d\n", x17); // prints 5 444 assert(x17 == 5); 445 { Bar17 b; 446 int x17 = 3; 447 448 printf("b.x17 = %d\n", b.x17); // prints 5 449 assert(b.x17 == 5); 450 printf("x17 = %d\n", x17); // prints 3 451 assert(x17 == 3); 452 { 453 mixin Foo17; 454 printf("x17 = %d\n", x17); // prints 5 455 assert(x17 == 5); 456 x17 = 4; 457 printf("x17 = %d\n", x17); // prints 4 458 assert(x17 == 4); 459 } 460 printf("x17 = %d\n", x17); // prints 3 461 assert(x17 == 3); 462 } 463 printf("x17 = %d\n", x17); // prints 5 464 assert(x17 == 5); 465 } 466 467 /*******************************************/ 468 469 template Foo18() { int z = 3; } 470 471 struct Bar18(alias Tmpl) 472 { 473 mixin Tmpl; 474 } 475 476 Bar18!(Foo18) b18; 477 478 void test18() 479 { 480 assert(b18.z == 3); 481 } 482 483 /*******************************************/ 484 485 template Mix1(T) 486 { 487 int foo19(T a) { return 2*a; } 488 } 489 490 template Mix2(T) 491 { 492 mixin Mix1!(T); 493 494 int bar19(T a) { return foo19(a); } 495 } 496 497 mixin Mix2!(int); 498 499 void test19() 500 { 501 int i; 502 503 i = bar19(7); 504 assert(i == 14); 505 } 506 507 /*******************************************/ 508 509 interface A20 { int f(); } 510 511 template Foo20() 512 { 513 int f() 514 { 515 printf("in C20.f()\n"); 516 return 6; 517 } 518 } 519 520 class C20 : A20 521 { 522 mixin Foo20; 523 // void f() { printf("in C20.f()\n"); } 524 } 525 526 void test20() 527 { 528 C20 c = new C20(); 529 int i = c.f(); 530 assert(i == 6); 531 } 532 533 /*******************************************/ 534 535 template Mix21() { this(int x) { printf("mix1\n"); }} 536 537 class Bar21 538 { 539 int myx; 540 mixin Mix21; // wouldn't compile 541 542 this() { myx = 15; } 543 544 // mixin Mix21; // placing it here compiles 545 } 546 547 void test21() 548 { 549 Bar21 bar = new Bar21(); 550 } 551 552 /*******************************************/ 553 554 template A22(T) 555 { 556 this() 557 { int i; 558 i = super.foo(); 559 assert(i == 67); 560 } 561 } 562 563 564 class B22 565 { 566 int foo() { printf("B22.foo()\n"); return 67; } 567 } 568 569 class C22 : B22 570 { 571 mixin A22!(C22); 572 } 573 574 void test22() 575 { 576 C22 c = new C22; 577 } 578 579 580 /*******************************************/ 581 582 template Foo23() 583 { 584 const int x = 5; 585 } 586 587 class C23 588 { 589 mixin Foo23 F; 590 } 591 592 struct D23 593 { 594 mixin Foo23 F; 595 } 596 597 void test23() 598 { 599 C23 c = new C23; 600 601 printf("%d\n",c.F.x); 602 assert(c.F.x == 5); 603 604 D23 d; 605 606 printf("%d\n",d.F.x); 607 assert(d.F.x == 5); 608 } 609 610 /*******************************************/ 611 612 template T24() 613 { 614 void foo24() { return cast(void)0; } 615 // alias foo24 foo24; 616 } 617 618 mixin T24; 619 620 void test24() 621 { 622 foo24(); 623 } 624 625 626 /*******************************************/ 627 628 template ctor25() 629 { 630 this() { this(null); } 631 this( Object o ) {} 632 } 633 634 class Foo25 635 { 636 mixin ctor25; 637 } 638 639 void test25() 640 { 641 Foo25 foo = new Foo25(); 642 } 643 644 645 /*******************************************/ 646 647 template Get26(T) 648 { 649 Reader get (ref T x) 650 { 651 return this; 652 } 653 } 654 655 class Reader 656 { 657 mixin Get26!(byte) bar; 658 alias bar.get get; 659 mixin Get26!(int) beq; 660 alias beq.get get; 661 } 662 663 void test26() 664 { 665 Reader r = new Reader; 666 Reader s; 667 byte q; 668 s = r.get (q); 669 assert(s == r); 670 } 671 672 /*******************************************/ 673 674 template Template(int L) 675 { 676 int i = L; 677 int foo(int b = Template!(9).i) { 678 return b; 679 } 680 } 681 682 void test27() 683 { 684 int i = 10; 685 int foo(int b = Template!(9).i) { 686 return b; 687 } 688 assert(foo()==9); 689 } 690 691 /*******************************************/ 692 693 template Blah28(int a, alias B) 694 { 695 mixin Blah28!(a-1, B); 696 //mixin Blah28!(0, B); 697 } 698 699 template Blah28(int a:0, alias B) 700 { 701 } 702 703 void test28() 704 { 705 int a; 706 mixin Blah28!(5,a); 707 printf("a = %d\n", a); 708 } 709 710 /*******************************************/ 711 712 template T29() 713 { 714 int x; 715 } 716 717 struct S29 718 { 719 mixin T29; 720 int y; 721 } 722 723 const S29 s29 = { x:2, y:3 }; 724 725 void test29() 726 { 727 assert(s29.x == 2); 728 assert(s29.y == 3); 729 } 730 731 /*******************************************/ 732 733 class A30 734 { 735 template ctor(Type) 736 { 737 this(Type[] arr) 738 { 739 foreach(Type v; arr) 740 { 741 const str = typeid(typeof(v)).toString(); 742 printf("%.*s\n", cast(int)str.length, str.ptr); 743 } 744 } 745 } 746 747 mixin ctor!(int); 748 } 749 750 void test30() 751 { 752 static int[] ints = [0,1,2,3]; 753 A30 a = new A30(ints); 754 } 755 756 /*******************************************/ 757 758 template Share(T) { 759 const bool opEquals(ref const T x) { return true; } 760 } 761 762 struct List31(T) { 763 764 //int opEquals(List31 x) { return 0; } 765 mixin Share!(List31); 766 } 767 768 void test31() 769 { 770 List31!(int) x; 771 List31!(int) y; 772 int i = x == y; 773 assert(i == 1); 774 } 775 776 /*******************************************/ 777 778 template Blah(int a, alias B) 779 { 780 mixin Blah!(a-1, B); 781 } 782 783 template Blah(int a:0, alias B) 784 { 785 int foo() 786 { return B + 1; 787 } 788 } 789 790 void test32() 791 { 792 int a = 3; 793 mixin Blah!(5,a); 794 795 assert(foo() == 4); 796 } 797 798 /*******************************************/ 799 800 template T33( int i ) 801 { 802 int foo() 803 { 804 printf("foo %d\n", i ); 805 return i; 806 } 807 int opCall() 808 { 809 printf("opCall %d\n", i ); 810 return i; 811 } 812 } 813 814 815 class C33 816 { 817 mixin T33!( 1 ) t1; 818 mixin T33!( 2 ) t2; 819 } 820 821 void test33() 822 { 823 int i; 824 C33 c1 = new C33; 825 i = c1.t1.foo(); 826 assert(i == 1); 827 i = c1.t2.foo(); 828 assert(i == 2); 829 i = c1.t1(); 830 assert(i == 1); 831 i = c1.t2(); 832 assert(i == 2); 833 } 834 835 836 /*******************************************/ 837 838 template mix34() 839 { 840 int i; 841 void print() 842 { 843 printf( "%d %d\n", i, j ); 844 assert(i == 0); 845 assert(j == 0); 846 } 847 } 848 849 void test34() 850 { 851 int j; 852 mixin mix34!(); 853 854 print(); 855 //printf( "%i\n", i ); 856 } 857 858 /*******************************************/ 859 860 mixin T35!(int) m35; 861 862 template T35(t) 863 { 864 t a; 865 } 866 867 void test35() 868 { 869 m35.a = 3; 870 } 871 872 /*******************************************/ 873 874 struct Foo36 875 { 876 int a; 877 mixin T!(int) m; 878 template T(t) 879 { 880 t b; 881 } 882 int c; 883 } 884 885 void test36() 886 { 887 Foo36 f; 888 assert(f.sizeof == 12); 889 890 f.a = 1; 891 f.m.b = 2; 892 f.c = 3; 893 894 assert(f.a == 1); 895 assert(f.m.b == 2); 896 assert(f.c == 3); 897 } 898 899 /*******************************************/ 900 901 template Foo37() 902 { 903 template func() { 904 int func() { 905 return 6; 906 } 907 } 908 } 909 910 class Baz37 911 { 912 mixin Foo37 bar; 913 } 914 915 void test37() 916 { 917 Baz37 b = new Baz37; 918 auto i = b.bar.func!()(); 919 assert(i == 6); 920 i = (new Baz37).bar.func!()(); 921 assert(i == 6); 922 } 923 924 /*******************************************/ 925 926 template Foo38() 927 { 928 int a = 4; 929 930 ~this() 931 { 932 printf("one\n"); 933 assert(a == 4); 934 assert(b == 5); 935 c++; 936 } 937 } 938 939 class Outer38 940 { int b = 5; 941 942 static int c; 943 944 mixin Foo38!() bar; 945 mixin Foo38!() abc; 946 947 ~this() 948 { 949 printf("two\n"); 950 assert(b == 5); 951 assert(c == 0); 952 c++; 953 } 954 } 955 956 void test38() 957 { 958 Outer38 o = new Outer38(); 959 delete o; 960 assert(Outer38.c == 3); 961 } 962 963 /*******************************************/ 964 965 template TDtor() 966 { 967 ~this() 968 { 969 printf("Mixed-in dtor\n"); 970 } 971 } 972 973 class Base39 974 { 975 ~this() 976 { 977 printf("Base39 dtor\n"); 978 } 979 } 980 981 class Class39 : Base39 982 { 983 mixin TDtor A; 984 mixin TDtor B; 985 986 ~this() 987 { 988 printf("Class39 dtor\n"); 989 } 990 } 991 992 void test39() 993 { 994 scope test = new Class39; 995 } 996 997 998 /*******************************************/ 999 1000 template Mix40() 1001 { 1002 int i; 1003 } 1004 1005 struct Z40 1006 { 1007 union { mixin Mix40; } 1008 } 1009 1010 void test40() 1011 { 1012 Z40 z; 1013 z.i = 3; 1014 } 1015 1016 /*******************************************/ 1017 1018 1019 class X41(P...) 1020 { 1021 alias P[0] Q; 1022 mixin Q!(); 1023 } 1024 1025 template MYP() 1026 { 1027 void foo() { } 1028 } 1029 1030 void test41() 1031 { 1032 X41!(MYP) x; 1033 } 1034 1035 /*******************************************/ 1036 // https://issues.dlang.org/show_bug.cgi?id=2245 1037 1038 template TCALL2245a(ARGS...) 1039 { 1040 int makecall(ARGS args) 1041 { 1042 return args.length; 1043 } 1044 } 1045 1046 template TCALL2245b(int n) 1047 { 1048 int makecall2(ARGS...)(ARGS args) if (ARGS.length == n) 1049 { 1050 return args.length; 1051 } 1052 } 1053 1054 class C2245 1055 { 1056 mixin TCALL2245a!(); 1057 mixin TCALL2245a!(int); 1058 mixin TCALL2245a!(int,int); 1059 1060 mixin TCALL2245b!(0); 1061 mixin TCALL2245b!(1); 1062 mixin TCALL2245b!(2); 1063 } 1064 1065 struct S2245 1066 { 1067 mixin TCALL2245a!(); 1068 mixin TCALL2245a!(int); 1069 mixin TCALL2245a!(int,int); 1070 1071 mixin TCALL2245b!(0); 1072 mixin TCALL2245b!(1); 1073 mixin TCALL2245b!(2); 1074 } 1075 1076 void test2245() 1077 { 1078 auto c = new C2245; 1079 assert(c.makecall() == 0); 1080 assert(c.makecall(0) == 1); 1081 assert(c.makecall(0,1) == 2); 1082 1083 assert(c.makecall2() == 0); 1084 assert(c.makecall2(0) == 1); 1085 assert(c.makecall2(0,1) == 2); 1086 1087 assert(c.makecall2!()() == 0); 1088 assert(c.makecall2!(int)(0) == 1); 1089 assert(c.makecall2!(int, int)(0,1) == 2); 1090 1091 auto s = S2245(); 1092 assert(s.makecall() == 0); 1093 assert(s.makecall(0) == 1); 1094 assert(s.makecall(0,1) == 2); 1095 1096 assert(s.makecall2() == 0); 1097 assert(s.makecall2(0) == 1); 1098 assert(s.makecall2(0,1) == 2); 1099 1100 assert(s.makecall2!()() == 0); 1101 assert(s.makecall2!(int)(0) == 1); 1102 assert(s.makecall2!(int, int)(0,1) == 2); 1103 } 1104 1105 /*******************************************/ 1106 // https://issues.dlang.org/show_bug.cgi?id=2481 1107 1108 template M2481() { int i; } 1109 class Z2481a { struct { mixin M2481!(); } } 1110 class Z2481b { struct { int i; } } 1111 1112 void test2481() 1113 { 1114 Z2481a z1; 1115 Z2481b z2; 1116 static assert(z1.i.offsetof == z2.i.offsetof); 1117 } 1118 1119 /*******************************************/ 1120 // https://issues.dlang.org/show_bug.cgi?id=2740 1121 1122 interface IFooable2740 1123 { 1124 bool foo(); 1125 } 1126 abstract class CFooable2740 1127 { 1128 bool foo(); 1129 } 1130 1131 mixin template MFoo2740() 1132 { 1133 override bool foo() { return true; } 1134 } 1135 1136 class Foo2740i1 : IFooable2740 1137 { 1138 override bool foo() { return false; } 1139 mixin MFoo2740; 1140 } 1141 class Foo2740i2 : IFooable2740 1142 { 1143 mixin MFoo2740; 1144 override bool foo() { return false; } 1145 } 1146 1147 class Foo2740c1 : CFooable2740 1148 { 1149 override bool foo() { return false; } 1150 mixin MFoo2740; 1151 } 1152 class Foo2740c2 : CFooable2740 1153 { 1154 mixin MFoo2740; 1155 override bool foo() { return false; } 1156 } 1157 1158 void test2740() 1159 { 1160 { 1161 auto p = new Foo2740i1(); 1162 IFooable2740 i = p; 1163 assert(p.foo() == false); 1164 assert(i.foo() == false); 1165 } 1166 { 1167 auto p = new Foo2740i2(); 1168 IFooable2740 i = p; 1169 assert(p.foo() == false); 1170 assert(i.foo() == false); 1171 } 1172 1173 { 1174 auto p = new Foo2740c1(); 1175 CFooable2740 i = p; 1176 assert(p.foo() == false); 1177 assert(i.foo() == false); 1178 } 1179 { 1180 auto p = new Foo2740c2(); 1181 CFooable2740 i = p; 1182 assert(p.foo() == false); 1183 assert(i.foo() == false); 1184 } 1185 } 1186 1187 /*******************************************/ 1188 1189 mixin template MTestFoo() 1190 { 1191 int foo(){ return 2; } 1192 } 1193 class TestFoo 1194 { 1195 mixin MTestFoo!() test; 1196 int foo(){ return 1; } 1197 } 1198 void test42() 1199 { 1200 auto p = new TestFoo(); 1201 assert(p.foo() == 1); 1202 assert(p.test.foo() == 2); 1203 } 1204 1205 /*******************************************/ 1206 // https://issues.dlang.org/show_bug.cgi?id=7744 1207 1208 class ZeroOrMore7744(Expr) 1209 { 1210 enum name = "ZeroOrMore7744!("~Expr.name~")"; 1211 } 1212 class Range7744(char begin, char end) 1213 { 1214 enum name = "Range7744!("~begin~","~end~")"; 1215 } 1216 1217 mixin(q{ 1218 class RubySource7744 : ZeroOrMore7744!(DecLiteral7744) 1219 { 1220 } 1221 class DecLiteral7744 : Range7744!('0','9') 1222 { 1223 } 1224 }); 1225 1226 /*******************************************/ 1227 // https://issues.dlang.org/show_bug.cgi?id=8032 1228 1229 mixin template T8032() 1230 { 1231 void f() { } 1232 } 1233 1234 class A8032a 1235 { 1236 mixin T8032; // Named mixin causes the error too 1237 void f() { } 1238 } 1239 class B8032a : A8032a 1240 { 1241 override void f() { } 1242 } 1243 1244 class A8032b 1245 { 1246 void f() { } 1247 mixin T8032; // Named mixin causes the error too 1248 } 1249 class B8032b : A8032b 1250 { 1251 override void f() { } 1252 } 1253 1254 /*********************************************/ 1255 // https://issues.dlang.org/show_bug.cgi?id=9417 1256 1257 mixin template Foo9417() 1258 { 1259 void foo() {} 1260 } 1261 1262 void test9417() 1263 { 1264 struct B 1265 { 1266 mixin Foo9417; 1267 } 1268 } 1269 1270 /*******************************************/ 1271 // https://issues.dlang.org/show_bug.cgi?id=11487 1272 1273 template X11487() 1274 { 1275 struct R() 1276 { 1277 C11487 c; 1278 1279 ~this() 1280 { 1281 static assert(is(typeof(c.front) == void)); 1282 } 1283 } 1284 template Mix(alias R) 1285 { 1286 R!() range; 1287 @property front() inout {} 1288 } 1289 } 1290 1291 class C11487 1292 { 1293 alias X11487!() M; 1294 mixin M.Mix!(M.R); 1295 } 1296 1297 /*******************************************/ 1298 // https://issues.dlang.org/show_bug.cgi?id=11767 1299 1300 mixin template M11767() 1301 { 1302 struct S11767 {} 1303 } 1304 mixin M11767!(); 1305 mixin M11767!(); // OK 1306 static assert(!__traits(compiles, S11767)); 1307 1308 void test11767() 1309 { 1310 mixin M11767!(); 1311 alias S1 = S11767; 1312 { 1313 mixin M11767!(); 1314 alias S2 = S11767; 1315 static assert(!is(S1 == S2)); 1316 static assert(S1.mangleof == "S6mixin19test11767FZ8__mixin16S11767"); 1317 static assert(S2.mangleof == "S6mixin19test11767FZ8__mixin26S11767"); 1318 } 1319 mixin M11767!(); 1320 static assert(!__traits(compiles, S11767)); 1321 } 1322 1323 /*******************************************/ 1324 // https://issues.dlang.org/show_bug.cgi?id=12023 1325 1326 void Delete12023(Object obj) {} 1327 1328 template MessageCode12023() 1329 { 1330 alias typeof(this) C; 1331 1332 struct MessageDeinitHelper 1333 { 1334 C m_outer; 1335 1336 ~this() 1337 { 1338 m_outer.DoDeinitMessaging(); 1339 } 1340 } 1341 1342 CToClient toClient = null; 1343 TypeTuple!(CToClient) toClients; 1344 1345 class CToClient {} 1346 1347 void DoDeinitMessaging() 1348 { 1349 Delete12023(toClient); 1350 Delete12023(toClients); 1351 } 1352 } 1353 1354 class TurretCannon12023(ProjectileClass) 1355 { 1356 mixin MessageCode12023; 1357 } 1358 1359 void test12023() 1360 { 1361 auto tc = new TurretCannon12023!Object(); 1362 } 1363 1364 /*******************************************/ 1365 // https://issues.dlang.org/show_bug.cgi?id=14243 1366 1367 mixin template Mix14243a(int n) 1368 { 1369 static assert(n > 0); 1370 import core.stdc.stdio; 1371 enum { enumMember = 1 } 1372 1373 auto a = A14243(n); 1374 } 1375 1376 mixin template Mix14243b(int n) 1377 { 1378 static if (n > 0) 1379 { 1380 auto b = A14243(n); 1381 } 1382 } 1383 1384 template foo14243(alias v) { auto bar() { return &v; } } 1385 mixin template Mix14243c(alias v) 1386 { 1387 // instantiate template in TemplateMixin 1388 auto c = foo14243!v.bar(); 1389 } 1390 1391 mixin template Mix14243d(int n) 1392 { 1393 // Type declaration in TemplateMixin 1394 struct NS { int x = n; } 1395 mixin("auto d" ~ ('0' + n) ~ " = NS();"); 1396 } 1397 1398 mixin template Mix14243e(int n) 1399 { 1400 @safe: 1401 nothrow: 1402 int foo() { return var; } 1403 1404 static: 1405 struct S { int x; void f() {} } 1406 int bar() { return n; } 1407 } 1408 1409 int test14243() 1410 { 1411 int[] ctor; 1412 int[] dtor; 1413 struct A14243 1414 { 1415 int x; 1416 this(int x) { ctor ~= x; this.x = x; } 1417 ~this() { dtor ~= x; } 1418 } 1419 1420 { 1421 /**/ 1422 assert(ctor == [] && dtor == []); mixin Mix14243a!(1); 1423 assert(ctor == [1] && dtor == []); mixin Mix14243b!(12) b1; 1424 assert(ctor == [1,12] && dtor == []); mixin Mix14243b!(24) b2; 1425 assert(ctor == [1,12,24] && dtor == []); 1426 assert(a.x == 1); 1427 static assert(!__traits(compiles, b > 0)); // ambiguous symbol access 1428 assert(b1.b.x == 12); 1429 assert(b2.b.x == 24); 1430 1431 int x; 1432 mixin Mix14243c!(x); 1433 assert(c == &x); 1434 1435 mixin Mix14243d!(1); 1436 mixin Mix14243d!(2); 1437 static assert(!is(typeof(d1) == typeof(d2))); 1438 assert(d1.x == 1); 1439 assert(d2.x == 2); 1440 1441 assert(ctor == [1,12,24] && dtor == []); 1442 } 1443 assert(ctor == [1,12,24] && dtor == [24,12,1]); 1444 1445 { 1446 int var = 1; 1447 mixin Mix14243e!12; 1448 static assert(is(typeof(&foo) == int delegate() @safe nothrow pure @nogc)); 1449 static assert(is(typeof(&bar) == int function() @safe nothrow pure @nogc)); 1450 static assert(S.sizeof == int.sizeof); // s is static struct 1451 assert(foo() == 1); 1452 assert(bar() == 12); 1453 } 1454 return 1; 1455 } 1456 static assert(test14243()); // changed to be workable 1457 1458 /*******************************************/ 1459 // https://issues.dlang.org/show_bug.cgi?id=10492 1460 1461 class TestClass10492 {} 1462 1463 mixin template mix10492(string name) 1464 { 1465 mixin("scope " ~ name ~ " = new TestClass10492;" ); 1466 } 1467 1468 void test10492() 1469 { 1470 mixin mix10492!("var"); 1471 } 1472 1473 /*******************************************/ 1474 1475 int main() 1476 { 1477 test1(); 1478 test2(); 1479 test3(); 1480 test4(); 1481 test5(); 1482 test6(); 1483 test7(); 1484 test8(); 1485 test9(); 1486 test10(); 1487 test11(); 1488 test12(); 1489 test13(); 1490 test14(); 1491 test15(); 1492 test16(); 1493 test17(); 1494 test18(); 1495 test19(); 1496 test20(); 1497 test21(); 1498 test22(); 1499 test23(); 1500 test24(); 1501 test25(); 1502 test26(); 1503 test27(); 1504 test28(); 1505 test29(); 1506 test30(); 1507 test31(); 1508 test32(); 1509 test33(); 1510 test34(); 1511 test35(); 1512 test36(); 1513 test37(); 1514 test38(); 1515 test39(); 1516 test40(); 1517 test41(); 1518 test2245(); 1519 test2740(); 1520 test42(); 1521 test9417(); 1522 test11767(); 1523 test12023(); 1524 test14243(); 1525 test10492(); 1526 1527 printf("Success\n"); 1528 return 0; 1529 }