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