1 // PERMUTE_ARGS: 2 module traits; 3 4 import std.stdio; 5 6 alias int myint; 7 struct S { void bar() { } int x = 4; static int z = 5; } 8 class C { void bar() { } final void foo() { } static void abc() { } } 9 abstract class AC { } 10 class AC2 { abstract void foo(); } 11 class AC3 : AC2 { } 12 final class FC { void foo() { } } 13 enum E { EMEM } 14 15 /********************************************************/ 16 17 void test1() 18 { 19 auto t = __traits(isArithmetic, int); 20 writeln(t); 21 assert(t == true); 22 23 assert(__traits(isArithmetic) == false); 24 assert(__traits(isArithmetic, myint) == true); 25 assert(__traits(isArithmetic, S) == false); 26 assert(__traits(isArithmetic, C) == false); 27 assert(__traits(isArithmetic, E) == true); 28 assert(__traits(isArithmetic, void*) == false); 29 assert(__traits(isArithmetic, void[]) == false); 30 assert(__traits(isArithmetic, void[3]) == false); 31 assert(__traits(isArithmetic, int[char]) == false); 32 assert(__traits(isArithmetic, int, int) == true); 33 assert(__traits(isArithmetic, int, S) == false); 34 35 assert(__traits(isArithmetic, void) == false); 36 assert(__traits(isArithmetic, byte) == true); 37 assert(__traits(isArithmetic, ubyte) == true); 38 assert(__traits(isArithmetic, short) == true); 39 assert(__traits(isArithmetic, ushort) == true); 40 assert(__traits(isArithmetic, int) == true); 41 assert(__traits(isArithmetic, uint) == true); 42 assert(__traits(isArithmetic, long) == true); 43 assert(__traits(isArithmetic, ulong) == true); 44 assert(__traits(isArithmetic, float) == true); 45 assert(__traits(isArithmetic, double) == true); 46 assert(__traits(isArithmetic, real) == true); 47 assert(__traits(isArithmetic, ifloat) == true); 48 assert(__traits(isArithmetic, idouble) == true); 49 assert(__traits(isArithmetic, ireal) == true); 50 assert(__traits(isArithmetic, cfloat) == true); 51 assert(__traits(isArithmetic, cdouble) == true); 52 assert(__traits(isArithmetic, creal) == true); 53 assert(__traits(isArithmetic, char) == true); 54 assert(__traits(isArithmetic, wchar) == true); 55 assert(__traits(isArithmetic, dchar) == true); 56 57 int i; 58 assert(__traits(isArithmetic, i, i+1, int) == true); 59 assert(__traits(isArithmetic) == false); 60 } 61 62 /********************************************************/ 63 64 void test2() 65 { 66 auto t = __traits(isScalar, int); 67 writeln(t); 68 assert(t == true); 69 70 assert(__traits(isScalar) == false); 71 assert(__traits(isScalar, myint) == true); 72 assert(__traits(isScalar, S) == false); 73 assert(__traits(isScalar, C) == false); 74 assert(__traits(isScalar, E) == true); 75 assert(__traits(isScalar, void*) == true); 76 assert(__traits(isScalar, void[]) == false); 77 assert(__traits(isScalar, void[3]) == false); 78 assert(__traits(isScalar, int[char]) == false); 79 assert(__traits(isScalar, int, int) == true); 80 assert(__traits(isScalar, int, S) == false); 81 82 assert(__traits(isScalar, void) == false); 83 assert(__traits(isScalar, byte) == true); 84 assert(__traits(isScalar, ubyte) == true); 85 assert(__traits(isScalar, short) == true); 86 assert(__traits(isScalar, ushort) == true); 87 assert(__traits(isScalar, int) == true); 88 assert(__traits(isScalar, uint) == true); 89 assert(__traits(isScalar, long) == true); 90 assert(__traits(isScalar, ulong) == true); 91 assert(__traits(isScalar, float) == true); 92 assert(__traits(isScalar, double) == true); 93 assert(__traits(isScalar, real) == true); 94 assert(__traits(isScalar, ifloat) == true); 95 assert(__traits(isScalar, idouble) == true); 96 assert(__traits(isScalar, ireal) == true); 97 assert(__traits(isScalar, cfloat) == true); 98 assert(__traits(isScalar, cdouble) == true); 99 assert(__traits(isScalar, creal) == true); 100 assert(__traits(isScalar, char) == true); 101 assert(__traits(isScalar, wchar) == true); 102 assert(__traits(isScalar, dchar) == true); 103 } 104 105 /********************************************************/ 106 107 void test3() 108 { 109 assert(__traits(isIntegral) == false); 110 assert(__traits(isIntegral, myint) == true); 111 assert(__traits(isIntegral, S) == false); 112 assert(__traits(isIntegral, C) == false); 113 assert(__traits(isIntegral, E) == true); 114 assert(__traits(isIntegral, void*) == false); 115 assert(__traits(isIntegral, void[]) == false); 116 assert(__traits(isIntegral, void[3]) == false); 117 assert(__traits(isIntegral, int[char]) == false); 118 assert(__traits(isIntegral, int, int) == true); 119 assert(__traits(isIntegral, int, S) == false); 120 121 assert(__traits(isIntegral, void) == false); 122 assert(__traits(isIntegral, byte) == true); 123 assert(__traits(isIntegral, ubyte) == true); 124 assert(__traits(isIntegral, short) == true); 125 assert(__traits(isIntegral, ushort) == true); 126 assert(__traits(isIntegral, int) == true); 127 assert(__traits(isIntegral, uint) == true); 128 assert(__traits(isIntegral, long) == true); 129 assert(__traits(isIntegral, ulong) == true); 130 assert(__traits(isIntegral, float) == false); 131 assert(__traits(isIntegral, double) == false); 132 assert(__traits(isIntegral, real) == false); 133 assert(__traits(isIntegral, ifloat) == false); 134 assert(__traits(isIntegral, idouble) == false); 135 assert(__traits(isIntegral, ireal) == false); 136 assert(__traits(isIntegral, cfloat) == false); 137 assert(__traits(isIntegral, cdouble) == false); 138 assert(__traits(isIntegral, creal) == false); 139 assert(__traits(isIntegral, char) == true); 140 assert(__traits(isIntegral, wchar) == true); 141 assert(__traits(isIntegral, dchar) == true); 142 } 143 144 /********************************************************/ 145 146 void test4() 147 { 148 assert(__traits(isFloating) == false); 149 assert(__traits(isFloating, S) == false); 150 assert(__traits(isFloating, C) == false); 151 assert(__traits(isFloating, E) == false); 152 assert(__traits(isFloating, void*) == false); 153 assert(__traits(isFloating, void[]) == false); 154 assert(__traits(isFloating, void[3]) == false); 155 assert(__traits(isFloating, int[char]) == false); 156 assert(__traits(isFloating, float, float) == true); 157 assert(__traits(isFloating, float, S) == false); 158 159 assert(__traits(isFloating, void) == false); 160 assert(__traits(isFloating, byte) == false); 161 assert(__traits(isFloating, ubyte) == false); 162 assert(__traits(isFloating, short) == false); 163 assert(__traits(isFloating, ushort) == false); 164 assert(__traits(isFloating, int) == false); 165 assert(__traits(isFloating, uint) == false); 166 assert(__traits(isFloating, long) == false); 167 assert(__traits(isFloating, ulong) == false); 168 assert(__traits(isFloating, float) == true); 169 assert(__traits(isFloating, double) == true); 170 assert(__traits(isFloating, real) == true); 171 assert(__traits(isFloating, ifloat) == true); 172 assert(__traits(isFloating, idouble) == true); 173 assert(__traits(isFloating, ireal) == true); 174 assert(__traits(isFloating, cfloat) == true); 175 assert(__traits(isFloating, cdouble) == true); 176 assert(__traits(isFloating, creal) == true); 177 assert(__traits(isFloating, char) == false); 178 assert(__traits(isFloating, wchar) == false); 179 assert(__traits(isFloating, dchar) == false); 180 } 181 182 /********************************************************/ 183 184 void test5() 185 { 186 assert(__traits(isUnsigned) == false); 187 assert(__traits(isUnsigned, S) == false); 188 assert(__traits(isUnsigned, C) == false); 189 assert(__traits(isUnsigned, E) == false); 190 assert(__traits(isUnsigned, void*) == false); 191 assert(__traits(isUnsigned, void[]) == false); 192 assert(__traits(isUnsigned, void[3]) == false); 193 assert(__traits(isUnsigned, int[char]) == false); 194 assert(__traits(isUnsigned, float, float) == false); 195 assert(__traits(isUnsigned, float, S) == false); 196 197 assert(__traits(isUnsigned, void) == false); 198 assert(__traits(isUnsigned, byte) == false); 199 assert(__traits(isUnsigned, ubyte) == true); 200 assert(__traits(isUnsigned, short) == false); 201 assert(__traits(isUnsigned, ushort) == true); 202 assert(__traits(isUnsigned, int) == false); 203 assert(__traits(isUnsigned, uint) == true); 204 assert(__traits(isUnsigned, long) == false); 205 assert(__traits(isUnsigned, ulong) == true); 206 assert(__traits(isUnsigned, float) == false); 207 assert(__traits(isUnsigned, double) == false); 208 assert(__traits(isUnsigned, real) == false); 209 assert(__traits(isUnsigned, ifloat) == false); 210 assert(__traits(isUnsigned, idouble) == false); 211 assert(__traits(isUnsigned, ireal) == false); 212 assert(__traits(isUnsigned, cfloat) == false); 213 assert(__traits(isUnsigned, cdouble) == false); 214 assert(__traits(isUnsigned, creal) == false); 215 assert(__traits(isUnsigned, char) == true); 216 assert(__traits(isUnsigned, wchar) == true); 217 assert(__traits(isUnsigned, dchar) == true); 218 } 219 220 /********************************************************/ 221 222 void test6() 223 { 224 assert(__traits(isAssociativeArray) == false); 225 assert(__traits(isAssociativeArray, S) == false); 226 assert(__traits(isAssociativeArray, C) == false); 227 assert(__traits(isAssociativeArray, E) == false); 228 assert(__traits(isAssociativeArray, void*) == false); 229 assert(__traits(isAssociativeArray, void[]) == false); 230 assert(__traits(isAssociativeArray, void[3]) == false); 231 assert(__traits(isAssociativeArray, int[char]) == true); 232 assert(__traits(isAssociativeArray, float, float) == false); 233 assert(__traits(isAssociativeArray, float, S) == false); 234 235 assert(__traits(isAssociativeArray, void) == false); 236 assert(__traits(isAssociativeArray, byte) == false); 237 assert(__traits(isAssociativeArray, ubyte) == false); 238 assert(__traits(isAssociativeArray, short) == false); 239 assert(__traits(isAssociativeArray, ushort) == false); 240 assert(__traits(isAssociativeArray, int) == false); 241 assert(__traits(isAssociativeArray, uint) == false); 242 assert(__traits(isAssociativeArray, long) == false); 243 assert(__traits(isAssociativeArray, ulong) == false); 244 assert(__traits(isAssociativeArray, float) == false); 245 assert(__traits(isAssociativeArray, double) == false); 246 assert(__traits(isAssociativeArray, real) == false); 247 assert(__traits(isAssociativeArray, ifloat) == false); 248 assert(__traits(isAssociativeArray, idouble) == false); 249 assert(__traits(isAssociativeArray, ireal) == false); 250 assert(__traits(isAssociativeArray, cfloat) == false); 251 assert(__traits(isAssociativeArray, cdouble) == false); 252 assert(__traits(isAssociativeArray, creal) == false); 253 assert(__traits(isAssociativeArray, char) == false); 254 assert(__traits(isAssociativeArray, wchar) == false); 255 assert(__traits(isAssociativeArray, dchar) == false); 256 } 257 258 /********************************************************/ 259 260 void test7() 261 { 262 assert(__traits(isStaticArray) == false); 263 assert(__traits(isStaticArray, S) == false); 264 assert(__traits(isStaticArray, C) == false); 265 assert(__traits(isStaticArray, E) == false); 266 assert(__traits(isStaticArray, void*) == false); 267 assert(__traits(isStaticArray, void[]) == false); 268 assert(__traits(isStaticArray, void[3]) == true); 269 assert(__traits(isStaticArray, int[char]) == false); 270 assert(__traits(isStaticArray, float, float) == false); 271 assert(__traits(isStaticArray, float, S) == false); 272 273 assert(__traits(isStaticArray, void) == false); 274 assert(__traits(isStaticArray, byte) == false); 275 assert(__traits(isStaticArray, ubyte) == false); 276 assert(__traits(isStaticArray, short) == false); 277 assert(__traits(isStaticArray, ushort) == false); 278 assert(__traits(isStaticArray, int) == false); 279 assert(__traits(isStaticArray, uint) == false); 280 assert(__traits(isStaticArray, long) == false); 281 assert(__traits(isStaticArray, ulong) == false); 282 assert(__traits(isStaticArray, float) == false); 283 assert(__traits(isStaticArray, double) == false); 284 assert(__traits(isStaticArray, real) == false); 285 assert(__traits(isStaticArray, ifloat) == false); 286 assert(__traits(isStaticArray, idouble) == false); 287 assert(__traits(isStaticArray, ireal) == false); 288 assert(__traits(isStaticArray, cfloat) == false); 289 assert(__traits(isStaticArray, cdouble) == false); 290 assert(__traits(isStaticArray, creal) == false); 291 assert(__traits(isStaticArray, char) == false); 292 assert(__traits(isStaticArray, wchar) == false); 293 assert(__traits(isStaticArray, dchar) == false); 294 } 295 296 /********************************************************/ 297 298 void test8() 299 { 300 assert(__traits(isAbstractClass) == false); 301 assert(__traits(isAbstractClass, S) == false); 302 assert(__traits(isAbstractClass, C) == false); 303 assert(__traits(isAbstractClass, AC) == true); 304 assert(__traits(isAbstractClass, E) == false); 305 assert(__traits(isAbstractClass, void*) == false); 306 assert(__traits(isAbstractClass, void[]) == false); 307 assert(__traits(isAbstractClass, void[3]) == false); 308 assert(__traits(isAbstractClass, int[char]) == false); 309 assert(__traits(isAbstractClass, float, float) == false); 310 assert(__traits(isAbstractClass, float, S) == false); 311 312 assert(__traits(isAbstractClass, void) == false); 313 assert(__traits(isAbstractClass, byte) == false); 314 assert(__traits(isAbstractClass, ubyte) == false); 315 assert(__traits(isAbstractClass, short) == false); 316 assert(__traits(isAbstractClass, ushort) == false); 317 assert(__traits(isAbstractClass, int) == false); 318 assert(__traits(isAbstractClass, uint) == false); 319 assert(__traits(isAbstractClass, long) == false); 320 assert(__traits(isAbstractClass, ulong) == false); 321 assert(__traits(isAbstractClass, float) == false); 322 assert(__traits(isAbstractClass, double) == false); 323 assert(__traits(isAbstractClass, real) == false); 324 assert(__traits(isAbstractClass, ifloat) == false); 325 assert(__traits(isAbstractClass, idouble) == false); 326 assert(__traits(isAbstractClass, ireal) == false); 327 assert(__traits(isAbstractClass, cfloat) == false); 328 assert(__traits(isAbstractClass, cdouble) == false); 329 assert(__traits(isAbstractClass, creal) == false); 330 assert(__traits(isAbstractClass, char) == false); 331 assert(__traits(isAbstractClass, wchar) == false); 332 assert(__traits(isAbstractClass, dchar) == false); 333 334 assert(__traits(isAbstractClass, AC2) == true); 335 assert(__traits(isAbstractClass, AC3) == true); 336 } 337 338 /********************************************************/ 339 340 void test9() 341 { 342 assert(__traits(isFinalClass) == false); 343 assert(__traits(isFinalClass, C) == false); 344 assert(__traits(isFinalClass, FC) == true); 345 } 346 347 /********************************************************/ 348 349 void test10() 350 { 351 assert(__traits(isVirtualFunction, C.bar) == true); 352 assert(__traits(isVirtualFunction, S.bar) == false); 353 } 354 355 /********************************************************/ 356 357 void test11() 358 { 359 assert(__traits(isAbstractFunction, C.bar) == false); 360 assert(__traits(isAbstractFunction, S.bar) == false); 361 assert(__traits(isAbstractFunction, AC2.foo) == true); 362 } 363 364 /********************************************************/ 365 366 void test12() 367 { 368 assert(__traits(isFinalFunction, C.bar) == false); 369 assert(__traits(isFinalFunction, S.bar) == false); 370 assert(__traits(isFinalFunction, AC2.foo) == false); 371 assert(__traits(isFinalFunction, FC.foo) == true); 372 assert(__traits(isFinalFunction, C.foo) == true); 373 } 374 375 /********************************************************/ 376 377 void test13() 378 { 379 S s; 380 __traits(getMember, s, "x") = 7; 381 auto i = __traits(getMember, s, "x"); 382 assert(i == 7); 383 auto j = __traits(getMember, S, "z"); 384 assert(j == 5); 385 386 writeln(__traits(hasMember, s, "x")); 387 assert(__traits(hasMember, s, "x") == true); 388 assert(__traits(hasMember, S, "z") == true); 389 assert(__traits(hasMember, S, "aaa") == false); 390 391 auto k = __traits(classInstanceSize, C); 392 writeln(k); 393 assert(k == C.classinfo.init.length); 394 } 395 396 /********************************************************/ 397 // 7123 398 399 private struct DelegateFaker7123(F) 400 { 401 template GeneratingPolicy() {} 402 enum WITH_BASE_CLASS = __traits(hasMember, GeneratingPolicy!(), "x"); 403 } 404 405 auto toDelegate7123(F)(F fp) 406 { 407 alias DelegateFaker7123!F Faker; 408 } 409 410 411 void test7123() 412 { 413 static assert(is(typeof(toDelegate7123(&main)))); 414 } 415 416 /********************************************************/ 417 418 class D14 419 { 420 this() { } 421 ~this() { } 422 void foo() { } 423 int foo(int) { return 0; } 424 } 425 426 void test14() 427 { 428 auto a = [__traits(derivedMembers, D14)]; 429 writeln(a); 430 assert(a == ["__ctor","__dtor","foo", "__xdtor"]); 431 } 432 433 /********************************************************/ 434 435 class D15 436 { 437 this() { } 438 ~this() { } 439 void foo() { } 440 int foo(int) { return 2; } 441 } 442 443 void test15() 444 { 445 D15 d = new D15(); 446 447 foreach (t; __traits(getVirtualFunctions, D15, "foo")) 448 writeln(typeid(typeof(t))); 449 450 alias typeof(__traits(getVirtualFunctions, D15, "foo")) b; 451 foreach (t; b) 452 writeln(typeid(t)); 453 454 auto i = __traits(getVirtualFunctions, d, "foo")[1](1); 455 assert(i == 2); 456 } 457 458 /********************************************************/ 459 460 struct S16 { } 461 462 int foo16(); 463 int bar16(); 464 465 void test16() 466 { 467 assert(__traits(isSame, foo16, foo16) == true); 468 assert(__traits(isSame, foo16, bar16) == false); 469 assert(__traits(isSame, foo16, S16) == false); 470 assert(__traits(isSame, S16, S16) == true); 471 assert(__traits(isSame, std, S16) == false); 472 assert(__traits(isSame, std, std) == true); 473 } 474 475 /********************************************************/ 476 477 struct S17 478 { 479 static int s1; 480 int s2; 481 } 482 483 int foo17(); 484 485 void test17() 486 { 487 assert(__traits(compiles) == false); 488 assert(__traits(compiles, foo17) == true); 489 assert(__traits(compiles, foo17 + 1) == true); 490 assert(__traits(compiles, &foo17 + 1) == false); 491 assert(__traits(compiles, typeof(1)) == true); 492 assert(__traits(compiles, S17.s1) == true); 493 assert(__traits(compiles, S17.s3) == false); 494 assert(__traits(compiles, 1,2,3,int,long,std) == true); 495 assert(__traits(compiles, 3[1]) == false); 496 assert(__traits(compiles, 1,2,3,int,long,3[1]) == false); 497 } 498 499 /********************************************************/ 500 501 interface D18 502 { 503 extern(Windows): 504 void foo(); 505 int foo(int); 506 } 507 508 void test18() 509 { 510 auto a = __traits(allMembers, D18); 511 writeln(a); 512 assert(a.length == 1); 513 } 514 515 516 /********************************************************/ 517 518 class C19 519 { 520 void mutating_method(){} 521 522 const void const_method(){} 523 524 void bastard_method(){} 525 const void bastard_method(int){} 526 } 527 528 529 void test19() 530 { 531 auto a = __traits(allMembers, C19); 532 writeln(a); 533 assert(a.length == 9); 534 535 foreach( m; __traits(allMembers, C19) ) 536 writeln(m); 537 } 538 539 540 /********************************************************/ 541 542 void test20() 543 { 544 void fooref(ref int x) 545 { 546 static assert(__traits(isRef, x)); 547 static assert(!__traits(isOut, x)); 548 static assert(!__traits(isLazy, x)); 549 } 550 551 void fooout(out int x) 552 { 553 static assert(!__traits(isRef, x)); 554 static assert(__traits(isOut, x)); 555 static assert(!__traits(isLazy, x)); 556 } 557 558 void foolazy(lazy int x) 559 { 560 static assert(!__traits(isRef, x)); 561 static assert(!__traits(isOut, x)); 562 static assert(__traits(isLazy, x)); 563 } 564 } 565 566 /********************************************************/ 567 568 void test21() 569 { 570 assert(__traits(isStaticFunction, C.bar) == false); 571 assert(__traits(isStaticFunction, C.abc) == true); 572 assert(__traits(isStaticFunction, S.bar) == false); 573 } 574 575 /********************************************************/ 576 577 class D22 578 { 579 this() { } 580 ~this() { } 581 void foo() { } 582 int foo(int) { return 2; } 583 } 584 585 void test22() 586 { 587 D22 d = new D22(); 588 589 foreach (t; __traits(getOverloads, D22, "foo")) 590 writeln(typeid(typeof(t))); 591 592 alias typeof(__traits(getOverloads, D22, "foo")) b; 593 foreach (t; b) 594 writeln(typeid(t)); 595 596 auto i = __traits(getOverloads, d, "foo")[1](1); 597 assert(i == 2); 598 } 599 600 /********************************************************/ 601 602 string toString23(E)(E value) if (is(E == enum)) { 603 foreach (s; __traits(allMembers, E)) { 604 if (value == mixin("E." ~ s)) return s; 605 } 606 return null; 607 } 608 609 enum OddWord { acini, alembicated, prolegomena, aprosexia } 610 611 void test23() 612 { 613 auto w = OddWord.alembicated; 614 assert(toString23(w) == "alembicated"); 615 } 616 617 /********************************************************/ 618 619 struct Test24 620 { 621 public void test24(int){} 622 private void test24(int, int){} 623 } 624 625 static assert(__traits(getProtection, __traits(getOverloads, Test24, "test24")[1]) == "private"); 626 627 /********************************************************/ 628 // 1369 629 630 void test1369() 631 { 632 class C1 633 { 634 static int count; 635 void func() { count++; } 636 } 637 638 // variable symbol 639 C1 c1 = new C1; 640 __traits(getMember, c1, "func")(); // TypeIdentifier -> VarExp 641 __traits(getMember, mixin("c1"), "func")(); // Expression -> VarExp 642 assert(C1.count == 2); 643 644 // nested function symbol 645 @property C1 get() { return c1; } 646 __traits(getMember, get, "func")(); 647 __traits(getMember, mixin("get"), "func")(); 648 assert(C1.count == 4); 649 650 class C2 651 { 652 C1 c1; 653 this() { c1 = new C1; } 654 void test() 655 { 656 // variable symbol (this.outer.c1) 657 __traits(getMember, c1, "func")(); // TypeIdentifier -> VarExp -> DotVarExp 658 __traits(getMember, mixin("c1"), "func")(); // Expression -> VarExp -> DotVarExp 659 assert(C1.count == 6); 660 661 // nested function symbol (this.outer.get) 662 __traits(getMember, get, "func")(); 663 __traits(getMember, mixin("get"), "func")(); 664 assert(C1.count == 8); 665 } 666 } 667 C2 c2 = new C2; 668 c2.test(); 669 } 670 671 /********************************************************/ 672 673 template Foo2234(){ int x; } 674 675 struct S2234a{ mixin Foo2234; } 676 struct S2234b{ mixin Foo2234; mixin Foo2234; } 677 struct S2234c{ alias Foo2234!() foo; } 678 679 static assert([__traits(allMembers, S2234a)] == ["x"]); 680 static assert([__traits(allMembers, S2234b)] == ["x"]); 681 static assert([__traits(allMembers, S2234c)] == ["foo"]); 682 683 /********************************************************/ 684 // 5878 685 686 template J5878(A) 687 { 688 static if (is(A P == super)) 689 alias P J5878; 690 } 691 692 alias J5878!(A5878) Z5878; 693 694 class X5878 {} 695 class A5878 : X5878 {} 696 697 /********************************************************/ 698 699 mixin template Members6674() 700 { 701 static int i1; 702 static int i2; 703 static int i3; //comment out to make func2 visible 704 static int i4; //comment out to make func1 visible 705 } 706 707 class Test6674 708 { 709 mixin Members6674; 710 711 alias void function() func1; 712 alias bool function() func2; 713 } 714 715 static assert([__traits(allMembers,Test6674)] == [ 716 "i1","i2","i3","i4", 717 "func1","func2", 718 "toString","toHash","opCmp","opEquals","Monitor","factory"]); 719 720 /********************************************************/ 721 // 6073 722 723 struct S6073 {} 724 725 template T6073(M...) { 726 //alias int T; 727 } 728 alias T6073!traits V6073; // ok 729 alias T6073!(__traits(parent, S6073)) U6073; // error 730 static assert(__traits(isSame, V6073, U6073)); // same instantiation == same arguemnts 731 732 /********************************************************/ 733 // 7027 734 735 struct Foo7027 { int a; } 736 static assert(!__traits(compiles, { return Foo7027.a; })); 737 738 /********************************************************/ 739 // 9213 740 741 class Foo9213 { int a; } 742 static assert(!__traits(compiles, { return Foo9213.a; })); 743 744 /********************************************************/ 745 746 interface AA 747 { 748 int YYY(); 749 } 750 751 class CC : AA 752 { 753 final int YYY() { return 4; } 754 } 755 756 static assert(__traits(isVirtualMethod, CC.YYY)); 757 static assert(__traits(getVirtualMethods, CC, "YYY").length == 1); 758 759 class DD 760 { 761 final int YYY() { return 4; } 762 } 763 764 static assert(__traits(isVirtualMethod, DD.YYY) == false); 765 static assert(__traits(getVirtualMethods, DD, "YYY").length == 0); 766 767 class EE 768 { 769 int YYY() { return 0; } 770 } 771 772 class FF : EE 773 { 774 final override int YYY() { return 4; } 775 } 776 777 static assert(__traits(isVirtualMethod, FF.YYY)); 778 static assert(__traits(getVirtualMethods, FF, "YYY").length == 1); 779 780 /********************************************************/ 781 // 7608 782 783 struct S7608a(bool T) 784 { 785 static if (T) { int x; } 786 int y; 787 } 788 struct S7608b 789 { 790 version(none) { int x; } 791 int y; 792 } 793 template TypeTuple7608(T...){ alias T TypeTuple7608; } 794 void test7608() 795 { 796 alias TypeTuple7608!(__traits(allMembers, S7608a!false)) MembersA; 797 static assert(MembersA.length == 1); 798 static assert(MembersA[0] == "y"); 799 800 alias TypeTuple7608!(__traits(allMembers, S7608b)) MembersB; 801 static assert(MembersB.length == 1); 802 static assert(MembersB[0] == "y"); 803 } 804 805 /********************************************************/ 806 // 7858 807 808 void test7858() 809 { 810 class C 811 { 812 final void ffunc(){} 813 final void ffunc(int){} 814 815 void vfunc(){} 816 void vfunc(int){} 817 818 abstract void afunc(); 819 abstract void afunc(int); 820 821 static void sfunc(){} 822 static void sfunc(int){} 823 } 824 825 static assert(__traits(isFinalFunction, C.ffunc) == 826 __traits(isFinalFunction, __traits(getOverloads, C, "ffunc")[0])); // NG 827 static assert(__traits(isVirtualFunction, C.vfunc) == 828 __traits(isVirtualFunction, __traits(getOverloads, C, "vfunc")[0])); // NG 829 static assert(__traits(isVirtualMethod, C.vfunc) == 830 __traits(isVirtualMethod, __traits(getOverloads, C, "vfunc")[0])); // NG 831 static assert(__traits(isAbstractFunction, C.afunc) == 832 __traits(isAbstractFunction, __traits(getOverloads, C, "afunc")[0])); // OK 833 static assert(__traits(isStaticFunction, C.sfunc) == 834 __traits(isStaticFunction, __traits(getOverloads, C, "sfunc")[0])); // OK 835 836 static assert(__traits(isSame, C.ffunc, __traits(getOverloads, C, "ffunc")[0])); // NG 837 static assert(__traits(isSame, C.vfunc, __traits(getOverloads, C, "vfunc")[0])); // NG 838 static assert(__traits(isSame, C.afunc, __traits(getOverloads, C, "afunc")[0])); // NG 839 static assert(__traits(isSame, C.sfunc, __traits(getOverloads, C, "sfunc")[0])); // NG 840 } 841 842 /********************************************************/ 843 // 8971 844 845 template Tuple8971(TL...){ alias TL Tuple8971; } 846 847 class A8971 848 { 849 void bar() {} 850 851 void connect() 852 { 853 alias Tuple8971!(__traits(getOverloads, typeof(this), "bar")) overloads; 854 static assert(__traits(isSame, overloads[0], bar)); 855 } 856 } 857 858 /********************************************************/ 859 // 8972 860 861 struct A8972 862 { 863 void foo() {} 864 865 void connect() 866 { 867 alias Tuple8971!(__traits(getOverloads, typeof(this), "foo")) overloads; 868 static assert(__traits(isSame, overloads[0], foo)); 869 } 870 } 871 872 /********************************************************/ 873 874 private struct TestProt1 {} 875 package struct TestProt2 {} 876 protected struct TestProt3 {} 877 public struct TestProt4 {} 878 export struct TestProt5 {} 879 880 void getProtection() 881 { 882 class Test 883 { 884 private { int va; void fa(){} } 885 package { int vb; void fb(){} } 886 protected { int vc; void fc(){} } 887 public { int vd; void fd(){} } 888 export { int ve; void fe(){} } 889 } 890 Test t; 891 892 // TOKvar and VarDeclaration 893 static assert(__traits(getProtection, Test.va) == "private"); 894 static assert(__traits(getProtection, Test.vb) == "package"); 895 static assert(__traits(getProtection, Test.vc) == "protected"); 896 static assert(__traits(getProtection, Test.vd) == "public"); 897 static assert(__traits(getProtection, Test.ve) == "export"); 898 899 // TOKdotvar and VarDeclaration 900 static assert(__traits(getProtection, t.va) == "private"); 901 static assert(__traits(getProtection, t.vb) == "package"); 902 static assert(__traits(getProtection, t.vc) == "protected"); 903 static assert(__traits(getProtection, t.vd) == "public"); 904 static assert(__traits(getProtection, t.ve) == "export"); 905 906 // TOKvar and FuncDeclaration 907 static assert(__traits(getProtection, Test.fa) == "private"); 908 static assert(__traits(getProtection, Test.fb) == "package"); 909 static assert(__traits(getProtection, Test.fc) == "protected"); 910 static assert(__traits(getProtection, Test.fd) == "public"); 911 static assert(__traits(getProtection, Test.fe) == "export"); 912 913 // TOKdotvar and FuncDeclaration 914 static assert(__traits(getProtection, t.fa) == "private"); 915 static assert(__traits(getProtection, t.fb) == "package"); 916 static assert(__traits(getProtection, t.fc) == "protected"); 917 static assert(__traits(getProtection, t.fd) == "public"); 918 static assert(__traits(getProtection, t.fe) == "export"); 919 920 // TOKtype 921 static assert(__traits(getProtection, TestProt1) == "private"); 922 static assert(__traits(getProtection, TestProt2) == "package"); 923 static assert(__traits(getProtection, TestProt3) == "protected"); 924 static assert(__traits(getProtection, TestProt4) == "public"); 925 static assert(__traits(getProtection, TestProt5) == "export"); 926 927 // This specific pattern is important to ensure it always works 928 // through reflection, however that becomes implemented 929 static assert(__traits(getProtection, __traits(getMember, t, "va")) == "private"); 930 static assert(__traits(getProtection, __traits(getMember, t, "vb")) == "package"); 931 static assert(__traits(getProtection, __traits(getMember, t, "vc")) == "protected"); 932 static assert(__traits(getProtection, __traits(getMember, t, "vd")) == "public"); 933 static assert(__traits(getProtection, __traits(getMember, t, "ve")) == "export"); 934 static assert(__traits(getProtection, __traits(getMember, t, "fa")) == "private"); 935 static assert(__traits(getProtection, __traits(getMember, t, "fb")) == "package"); 936 static assert(__traits(getProtection, __traits(getMember, t, "fc")) == "protected"); 937 static assert(__traits(getProtection, __traits(getMember, t, "fd")) == "public"); 938 static assert(__traits(getProtection, __traits(getMember, t, "fe")) == "export"); 939 } 940 941 /********************************************************/ 942 // 9546 943 944 void test9546() 945 { 946 import imports.a9546 : S; 947 948 S s; 949 static assert(__traits(getProtection, s.privA) == "private"); 950 static assert(__traits(getProtection, s.protA) == "protected"); 951 static assert(__traits(getProtection, s.packA) == "package"); 952 static assert(__traits(getProtection, S.privA) == "private"); 953 static assert(__traits(getProtection, S.protA) == "protected"); 954 static assert(__traits(getProtection, S.packA) == "package"); 955 956 static assert(__traits(getProtection, mixin("s.privA")) == "private"); 957 static assert(__traits(getProtection, mixin("s.protA")) == "protected"); 958 static assert(__traits(getProtection, mixin("s.packA")) == "package"); 959 static assert(__traits(getProtection, mixin("S.privA")) == "private"); 960 static assert(__traits(getProtection, mixin("S.protA")) == "protected"); 961 static assert(__traits(getProtection, mixin("S.packA")) == "package"); 962 963 static assert(__traits(getProtection, __traits(getMember, s, "privA")) == "private"); 964 static assert(__traits(getProtection, __traits(getMember, s, "protA")) == "protected"); 965 static assert(__traits(getProtection, __traits(getMember, s, "packA")) == "package"); 966 static assert(__traits(getProtection, __traits(getMember, S, "privA")) == "private"); 967 static assert(__traits(getProtection, __traits(getMember, S, "protA")) == "protected"); 968 static assert(__traits(getProtection, __traits(getMember, S, "packA")) == "package"); 969 970 static assert(__traits(getProtection, s.privF) == "private"); 971 static assert(__traits(getProtection, s.protF) == "protected"); 972 static assert(__traits(getProtection, s.packF) == "package"); 973 static assert(__traits(getProtection, S.privF) == "private"); 974 static assert(__traits(getProtection, S.protF) == "protected"); 975 static assert(__traits(getProtection, S.packF) == "package"); 976 977 static assert(__traits(getProtection, mixin("s.privF")) == "private"); 978 static assert(__traits(getProtection, mixin("s.protF")) == "protected"); 979 static assert(__traits(getProtection, mixin("s.packF")) == "package"); 980 static assert(__traits(getProtection, mixin("S.privF")) == "private"); 981 static assert(__traits(getProtection, mixin("S.protF")) == "protected"); 982 static assert(__traits(getProtection, mixin("S.packF")) == "package"); 983 984 static assert(__traits(getProtection, __traits(getMember, s, "privF")) == "private"); 985 static assert(__traits(getProtection, __traits(getMember, s, "protF")) == "protected"); 986 static assert(__traits(getProtection, __traits(getMember, s, "packF")) == "package"); 987 static assert(__traits(getProtection, __traits(getMember, S, "privF")) == "private"); 988 static assert(__traits(getProtection, __traits(getMember, S, "protF")) == "protected"); 989 static assert(__traits(getProtection, __traits(getMember, S, "packF")) == "package"); 990 } 991 992 /********************************************************/ 993 // 9091 994 995 template isVariable9091(X...) if (X.length == 1) 996 { 997 enum isVariable9091 = true; 998 } 999 class C9091 1000 { 1001 int x; // some class members 1002 void func(int n){ this.x = n; } 1003 1004 void test() 1005 { 1006 alias T = C9091; 1007 enum is_x = isVariable9091!(__traits(getMember, T, "x")); 1008 1009 foreach (i, m; __traits(allMembers, T)) 1010 { 1011 enum x = isVariable9091!(__traits(getMember, T, m)); 1012 static if (i == 0) // x 1013 { 1014 __traits(getMember, T, m) = 10; 1015 assert(this.x == 10); 1016 } 1017 static if (i == 1) // func 1018 { 1019 __traits(getMember, T, m)(20); 1020 assert(this.x == 20); 1021 } 1022 } 1023 } 1024 } 1025 struct S9091 1026 { 1027 int x; // some struct members 1028 void func(int n){ this.x = n; } 1029 1030 void test() 1031 { 1032 alias T = S9091; 1033 enum is_x = isVariable9091!(__traits(getMember, T, "x")); 1034 1035 foreach (i, m; __traits(allMembers, T)) 1036 { 1037 enum x = isVariable9091!(__traits(getMember, T, m)); 1038 static if (i == 0) // x 1039 { 1040 __traits(getMember, T, m) = 10; 1041 assert(this.x == 10); 1042 } 1043 static if (i == 1) // func 1044 { 1045 __traits(getMember, T, m)(20); 1046 assert(this.x == 20); 1047 } 1048 } 1049 } 1050 } 1051 1052 void test9091() 1053 { 1054 auto c = new C9091(); 1055 c.test(); 1056 1057 auto s = S9091(); 1058 s.test(); 1059 } 1060 1061 /********************************************************/ 1062 1063 struct CtorS_9237 { this(int x) { } } // ctor -> POD 1064 struct DtorS_9237 { ~this() { } } // dtor -> nonPOD 1065 struct PostblitS_9237 { this(this) { } } // cpctor -> nonPOD 1066 1067 struct NonPOD1_9237 1068 { 1069 DtorS_9237 field; // nonPOD -> ng 1070 } 1071 1072 struct NonPOD2_9237 1073 { 1074 DtorS_9237[2] field; // static array of nonPOD -> ng 1075 } 1076 1077 struct POD1_9237 1078 { 1079 DtorS_9237* field; // pointer to nonPOD -> ok 1080 } 1081 1082 struct POD2_9237 1083 { 1084 DtorS_9237[] field; // dynamic array of nonPOD -> ok 1085 } 1086 1087 struct POD3_9237 1088 { 1089 int x = 123; 1090 } 1091 1092 class C_9273 { } 1093 1094 void test9237() 1095 { 1096 int x; 1097 struct NS_9237 // acceses .outer -> nested 1098 { 1099 void foo() { x++; } 1100 } 1101 1102 struct NonNS_9237 { } // doesn't access .outer -> non-nested 1103 static struct StatNS_9237 { } // can't access .outer -> non-nested 1104 1105 static assert(!__traits(isPOD, NS_9237)); 1106 static assert(__traits(isPOD, NonNS_9237)); 1107 static assert(__traits(isPOD, StatNS_9237)); 1108 static assert(__traits(isPOD, CtorS_9237)); 1109 static assert(!__traits(isPOD, DtorS_9237)); 1110 static assert(!__traits(isPOD, PostblitS_9237)); 1111 static assert(!__traits(isPOD, NonPOD1_9237)); 1112 static assert(!__traits(isPOD, NonPOD2_9237)); 1113 static assert(__traits(isPOD, POD1_9237)); 1114 static assert(__traits(isPOD, POD2_9237)); 1115 static assert(__traits(isPOD, POD3_9237)); 1116 1117 // static array of POD/non-POD types 1118 static assert(!__traits(isPOD, NS_9237[2])); 1119 static assert(__traits(isPOD, NonNS_9237[2])); 1120 static assert(__traits(isPOD, StatNS_9237[2])); 1121 static assert(__traits(isPOD, CtorS_9237[2])); 1122 static assert(!__traits(isPOD, DtorS_9237[2])); 1123 static assert(!__traits(isPOD, PostblitS_9237[2])); 1124 static assert(!__traits(isPOD, NonPOD1_9237[2])); 1125 static assert(!__traits(isPOD, NonPOD2_9237[2])); 1126 static assert(__traits(isPOD, POD1_9237[2])); 1127 static assert(__traits(isPOD, POD2_9237[2])); 1128 static assert(__traits(isPOD, POD3_9237[2])); 1129 1130 // non-structs are POD types 1131 static assert(__traits(isPOD, C_9273)); 1132 static assert(__traits(isPOD, int)); 1133 static assert(__traits(isPOD, int*)); 1134 static assert(__traits(isPOD, int[])); 1135 static assert(!__traits(compiles, __traits(isPOD, 123) )); 1136 } 1137 1138 /*************************************************************/ 1139 // 5978 1140 1141 void test5978() 1142 { 1143 () { 1144 int x; 1145 pragma(msg, __traits(identifier, __traits(parent, x))); 1146 } (); 1147 } 1148 1149 /*************************************************************/ 1150 1151 template T7408() { } 1152 1153 void test7408() 1154 { 1155 auto x = T7408!().stringof; 1156 auto y = T7408!().mangleof; 1157 static assert(__traits(compiles, T7408!().stringof)); 1158 static assert(__traits(compiles, T7408!().mangleof)); 1159 static assert(!__traits(compiles, T7408!().init)); 1160 static assert(!__traits(compiles, T7408!().offsetof)); 1161 } 1162 1163 /*************************************************************/ 1164 // 9552 1165 1166 class C9552 1167 { 1168 int f() { return 10; } 1169 int f(int n) { return n * 2; } 1170 } 1171 1172 void test9552() 1173 { 1174 auto c = new C9552; 1175 auto dg1 = &(__traits(getOverloads, c, "f")[0]); // DMD crashes 1176 assert(dg1() == 10); 1177 auto dg2 = &(__traits(getOverloads, c, "f")[1]); 1178 assert(dg2(10) == 20); 1179 } 1180 1181 /*************************************************************/ 1182 1183 void test9136() 1184 { 1185 int x; 1186 struct S1 { void f() { x++; } } 1187 struct U1 { void f() { x++; } } 1188 static struct S2 { } 1189 static struct S3 { S1 s; } 1190 static struct U2 { } 1191 void f1() { x++; } 1192 static void f2() { } 1193 1194 static assert(__traits(isNested, S1)); 1195 static assert(__traits(isNested, U1)); 1196 static assert(!__traits(isNested, S2)); 1197 static assert(!__traits(isNested, S3)); 1198 static assert(!__traits(isNested, U2)); 1199 static assert(!__traits(compiles, __traits(isNested, int) )); 1200 static assert(!__traits(compiles, __traits(isNested, f1, f2) )); 1201 static assert(__traits(isNested, f1)); 1202 static assert(!__traits(isNested, f2)); 1203 1204 static class A { static class SC { } class NC { } } 1205 static assert(!__traits(isNested, A)); 1206 static assert(!__traits(isNested, A.SC)); 1207 static assert(__traits(isNested, A.NC)); 1208 } 1209 1210 /********************************************************/ 1211 // 9939 1212 1213 struct Test9939 1214 { 1215 int f; 1216 enum /*Anonymous enum*/ 1217 { 1218 A, 1219 B 1220 } 1221 enum NamedEnum 1222 { 1223 C, 1224 D 1225 } 1226 } 1227 1228 static assert([__traits(allMembers, Test9939)] == ["f", "A", "B", "NamedEnum"]); 1229 1230 /********************************************************/ 1231 // 10043 1232 1233 void test10043() 1234 { 1235 struct X {} 1236 X d1; 1237 static assert(!__traits(compiles, d1.structuralCast!Refleshable)); 1238 } 1239 1240 /********************************************************/ 1241 // 10096 1242 1243 struct S10096X 1244 { 1245 string str; 1246 1247 invariant() {} 1248 invariant() {} 1249 unittest {} 1250 1251 this(int) {} 1252 this(this) {} 1253 ~this() {} 1254 } 1255 static assert( 1256 [__traits(allMembers, S10096X)] == 1257 ["str", "__ctor", "__postblit", "__dtor", "__xdtor", "__xpostblit", "opAssign"]); 1258 1259 // -------- 1260 1261 string foo10096(alias var, T = typeof(var))() 1262 { 1263 foreach (idx, member; __traits(allMembers, T)) 1264 { 1265 auto x = var.tupleof[idx]; 1266 } 1267 1268 return ""; 1269 } 1270 1271 string foo10096(T)(T var) 1272 { 1273 return ""; 1274 } 1275 1276 struct S10096 1277 { 1278 int i; 1279 string s; 1280 } 1281 1282 void test10096() 1283 { 1284 S10096 s = S10096(1, ""); 1285 auto x = foo10096!s; 1286 } 1287 1288 /********************************************************/ 1289 1290 unittest { } 1291 1292 struct GetUnitTests 1293 { 1294 unittest { } 1295 } 1296 1297 void test_getUnitTests () 1298 { 1299 // Always returns empty tuple if the -unittest flag isn't used 1300 static assert(__traits(getUnitTests, mixin(__MODULE__)).length == 0); 1301 static assert(__traits(getUnitTests, GetUnitTests).length == 0); 1302 } 1303 1304 /********************************************************/ 1305 1306 void test_getFunctionAttributes() 1307 { 1308 alias tuple(T...) = T; 1309 1310 struct S 1311 { 1312 int noF() { return 0; } 1313 int constF() const { return 0; } 1314 int immutableF() immutable { return 0; } 1315 int inoutF() inout { return 0; } 1316 int sharedF() shared { return 0; } 1317 1318 int x; 1319 ref int refF() { return x; } 1320 int propertyF() @property { return 0; } 1321 int nothrowF() nothrow { return 0; } 1322 int nogcF() @nogc { return 0; } 1323 1324 int systemF() @system { return 0; } 1325 int trustedF() @trusted { return 0; } 1326 int safeF() @safe { return 0; } 1327 1328 int pureF() pure { return 0; } 1329 } 1330 1331 static assert(__traits(getFunctionAttributes, S.noF) == tuple!("@system")); 1332 static assert(__traits(getFunctionAttributes, typeof(S.noF)) == tuple!("@system")); 1333 1334 static assert(__traits(getFunctionAttributes, S.constF) == tuple!("const", "@system")); 1335 static assert(__traits(getFunctionAttributes, typeof(S.constF)) == tuple!("const", "@system")); 1336 1337 static assert(__traits(getFunctionAttributes, S.immutableF) == tuple!("immutable", "@system")); 1338 static assert(__traits(getFunctionAttributes, typeof(S.immutableF)) == tuple!("immutable", "@system")); 1339 1340 static assert(__traits(getFunctionAttributes, S.inoutF) == tuple!("inout", "@system")); 1341 static assert(__traits(getFunctionAttributes, typeof(S.inoutF)) == tuple!("inout", "@system")); 1342 1343 static assert(__traits(getFunctionAttributes, S.sharedF) == tuple!("shared", "@system")); 1344 static assert(__traits(getFunctionAttributes, typeof(S.sharedF)) == tuple!("shared", "@system")); 1345 1346 static assert(__traits(getFunctionAttributes, S.refF) == tuple!("ref", "@system")); 1347 static assert(__traits(getFunctionAttributes, typeof(S.refF)) == tuple!("ref", "@system")); 1348 1349 static assert(__traits(getFunctionAttributes, S.propertyF) == tuple!("@property", "@system")); 1350 static assert(__traits(getFunctionAttributes, typeof(&S.propertyF)) == tuple!("@property", "@system")); 1351 1352 static assert(__traits(getFunctionAttributes, S.nothrowF) == tuple!("nothrow", "@system")); 1353 static assert(__traits(getFunctionAttributes, typeof(S.nothrowF)) == tuple!("nothrow", "@system")); 1354 1355 static assert(__traits(getFunctionAttributes, S.nogcF) == tuple!("@nogc", "@system")); 1356 static assert(__traits(getFunctionAttributes, typeof(S.nogcF)) == tuple!("@nogc", "@system")); 1357 1358 static assert(__traits(getFunctionAttributes, S.systemF) == tuple!("@system")); 1359 static assert(__traits(getFunctionAttributes, typeof(S.systemF)) == tuple!("@system")); 1360 1361 static assert(__traits(getFunctionAttributes, S.trustedF) == tuple!("@trusted")); 1362 static assert(__traits(getFunctionAttributes, typeof(S.trustedF)) == tuple!("@trusted")); 1363 1364 static assert(__traits(getFunctionAttributes, S.safeF) == tuple!("@safe")); 1365 static assert(__traits(getFunctionAttributes, typeof(S.safeF)) == tuple!("@safe")); 1366 1367 static assert(__traits(getFunctionAttributes, S.pureF) == tuple!("pure", "@system")); 1368 static assert(__traits(getFunctionAttributes, typeof(S.pureF)) == tuple!("pure", "@system")); 1369 1370 int pure_nothrow() nothrow pure { return 0; } 1371 static ref int static_ref_property() @property { return *(new int); } 1372 ref int ref_property() @property { return *(new int); } 1373 void safe_nothrow() @safe nothrow { } 1374 1375 static assert(__traits(getFunctionAttributes, pure_nothrow) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1376 static assert(__traits(getFunctionAttributes, typeof(pure_nothrow)) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1377 1378 static assert(__traits(getFunctionAttributes, static_ref_property) == tuple!("pure", "nothrow", "@property", "ref", "@safe")); 1379 static assert(__traits(getFunctionAttributes, typeof(&static_ref_property)) == tuple!("pure", "nothrow", "@property", "ref", "@safe")); 1380 1381 static assert(__traits(getFunctionAttributes, ref_property) == tuple!("pure", "nothrow", "@property", "ref", "@safe")); 1382 static assert(__traits(getFunctionAttributes, typeof(&ref_property)) == tuple!("pure", "nothrow", "@property", "ref", "@safe")); 1383 1384 static assert(__traits(getFunctionAttributes, safe_nothrow) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1385 static assert(__traits(getFunctionAttributes, typeof(safe_nothrow)) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1386 1387 struct S2 1388 { 1389 int pure_const() const pure { return 0; } 1390 int pure_sharedconst() const shared pure { return 0; } 1391 } 1392 1393 static assert(__traits(getFunctionAttributes, S2.pure_const) == tuple!("const", "pure", "@system")); 1394 static assert(__traits(getFunctionAttributes, typeof(S2.pure_const)) == tuple!("const", "pure", "@system")); 1395 1396 static assert(__traits(getFunctionAttributes, S2.pure_sharedconst) == tuple!("const", "shared", "pure", "@system")); 1397 static assert(__traits(getFunctionAttributes, typeof(S2.pure_sharedconst)) == tuple!("const", "shared", "pure", "@system")); 1398 1399 static assert(__traits(getFunctionAttributes, (int a) { }) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1400 static assert(__traits(getFunctionAttributes, typeof((int a) { })) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1401 1402 auto safeDel = delegate() @safe { }; 1403 static assert(__traits(getFunctionAttributes, safeDel) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1404 static assert(__traits(getFunctionAttributes, typeof(safeDel)) == tuple!("pure", "nothrow", "@nogc", "@safe")); 1405 1406 auto trustedDel = delegate() @trusted { }; 1407 static assert(__traits(getFunctionAttributes, trustedDel) == tuple!("pure", "nothrow", "@nogc", "@trusted")); 1408 static assert(__traits(getFunctionAttributes, typeof(trustedDel)) == tuple!("pure", "nothrow", "@nogc", "@trusted")); 1409 1410 auto systemDel = delegate() @system { }; 1411 static assert(__traits(getFunctionAttributes, systemDel) == tuple!("pure", "nothrow", "@nogc", "@system")); 1412 static assert(__traits(getFunctionAttributes, typeof(systemDel)) == tuple!("pure", "nothrow", "@nogc", "@system")); 1413 } 1414 1415 /********************************************************/ 1416 1417 class TestIsOverrideFunctionBase 1418 { 1419 void bar () {} 1420 } 1421 1422 class TestIsOverrideFunctionPass : TestIsOverrideFunctionBase 1423 { 1424 override void bar () {} 1425 } 1426 1427 void test_isOverrideFunction () 1428 { 1429 assert(__traits(isOverrideFunction, TestIsOverrideFunctionPass.bar) == true); 1430 assert(__traits(isOverrideFunction, TestIsOverrideFunctionBase.bar) == false); 1431 } 1432 1433 /********************************************************/ 1434 // 11711 - Add __traits(getAliasThis) 1435 1436 alias TypeTuple(T...) = T; 1437 1438 void test11711() 1439 { 1440 struct S1 1441 { 1442 string var; 1443 alias var this; 1444 } 1445 static assert(__traits(getAliasThis, S1) == TypeTuple!("var")); 1446 static assert(is(typeof(__traits(getMember, S1.init, __traits(getAliasThis, S1)[0])) 1447 == string)); 1448 1449 struct S2 1450 { 1451 TypeTuple!(int, string) var; 1452 alias var this; 1453 } 1454 static assert(__traits(getAliasThis, S2) == TypeTuple!("var")); 1455 static assert(is(typeof(__traits(getMember, S2.init, __traits(getAliasThis, S2)[0])) 1456 == TypeTuple!(int, string))); 1457 } 1458 1459 1460 /********************************************************/ 1461 // Issue 12278 1462 1463 class Foo12278 1464 { 1465 InPlace12278!Bar12278 inside; 1466 } 1467 1468 class Bar12278 { } 1469 1470 struct InPlace12278(T) 1471 { 1472 static assert(__traits(classInstanceSize, T) != 0); 1473 } 1474 1475 /********************************************************/ 1476 // 12571 1477 1478 mixin template getScopeName12571() 1479 { 1480 enum string scopeName = __traits(identifier, __traits(parent, scopeName)); 1481 } 1482 1483 void test12571() 1484 { 1485 mixin getScopeName12571; 1486 static assert(scopeName == "test12571"); 1487 } 1488 1489 /********************************************************/ 1490 // 12237 1491 1492 auto f12237(T)(T a) 1493 { 1494 static if (is(typeof(a) == int)) 1495 return f12237(""); 1496 else 1497 return 10; 1498 } 1499 1500 void test12237() 1501 { 1502 assert(f12237(1) == 10); 1503 1504 assert((a){ 1505 static if (is(typeof(a) == int)) 1506 { 1507 int x; 1508 return __traits(parent, x)(""); 1509 } 1510 else 1511 return 10; 1512 }(1) == 10); 1513 } 1514 1515 /********************************************************/ 1516 1517 int main() 1518 { 1519 test1(); 1520 test2(); 1521 test3(); 1522 test4(); 1523 test5(); 1524 test6(); 1525 test7(); 1526 test8(); 1527 test9(); 1528 test10(); 1529 test11(); 1530 test12(); 1531 test13(); 1532 test7123(); 1533 test14(); 1534 test15(); 1535 test16(); 1536 test17(); 1537 test18(); 1538 test19(); 1539 test20(); 1540 test21(); 1541 test22(); 1542 test23(); 1543 test1369(); 1544 test7608(); 1545 test7858(); 1546 test9091(); 1547 test5978(); 1548 test7408(); 1549 test9552(); 1550 test9136(); 1551 test10096(); 1552 test_getUnitTests(); 1553 test_getFunctionAttributes(); 1554 test_isOverrideFunction(); 1555 test12237(); 1556 1557 writeln("Success"); 1558 return 0; 1559 }