3 import core
.stdc
.stdio
;
5 /*******************************************/
9 int foo(int b
) { return b
+ 1; }
19 /*******************************************/
25 int foo(int b
) { return b
+ c
+ 1; }
36 /*******************************************/
40 static int foo(int b
) { return b
+ 1; }
50 /*******************************************/
56 static int foo(int b
) { return b
+ c
+ 1; }
67 /*******************************************/
73 int foo(int b
) { return b
+ c
+ 1; }
80 assert(bar5(3) == 12);
84 /*******************************************/
90 int foob(int b
) { return b
+ c
+ 1; }
91 int fooa(int b
) { return foob(c
+ b
) * 7; }
98 assert(bar6(3) == 84);
102 /*******************************************/
108 static int foob(int b
) { return b
+ c
+ 1; }
110 int function(int) fp
= &foob
;
117 assert(bar7(3) == 8);
120 /*******************************************/
126 int foob(int b
) { return b
+ c
+ 1; }
128 int delegate(int) fp
= &foob
;
135 assert(bar8(3) == 8);
139 /*******************************************/
149 Abc9
*foo() { return &this; }
161 assert(x
.bar(3) == 10);
164 /*******************************************/
174 Abc10
foo() { return this; }
185 Abc10 x
= new Abc10();
187 assert(x
.bar(3) == 10);
191 /*******************************************/
197 void opApply(void delegate(int) fp
)
199 for (int i
= 0; i
< array
.length
; i
++)
204 int func11(Collection c
)
214 c
.opApply(&comp_max
);
220 Collection c
= new Collection();
231 /*******************************************/
233 void SimpleNestedFunction ()
235 int nest () { return 432; }
237 assert (nest () == 432);
238 int delegate () func
= &nest
;
239 assert (func () == 432);
242 void AccessParentScope ()
246 int nest () { assert (value
== 9); return 9; }
248 assert (nest () == 9);
251 void CrossNestedScope ()
255 void foo () { assert (x
== 45); }
256 void bar () { int z
= 16; foo (); }
260 void BadMultipleNested ()
268 //erroneous x = 4; // Should fail.
273 /* This one kind of depends upon memory layout. GlobalScopeSpoof should
274 be called with no "this" pointer; this is trying to ensure that
275 everything is working properly. Of course, in the DMD calling
276 convention it'll fail if the caller passes too much/little data. */
278 void GlobalScopeSpoof (int x
, int y
)
280 assert (x
== y
&& y
== 487);
285 void bar () { GlobalScopeSpoof (487, 487); }
295 void bar () { assert (x
== 6400); }
302 SimpleNestedFunction ();
303 AccessParentScope ();
306 (new TestClass
).foo ();
310 /*******************************************/
322 assert(a
.x
== 3 && a
.y
== 4);
326 /*******************************************/
335 int foo() { return y
; }
340 assert(a
.foo() == 4);
344 /*******************************************/
355 int foo() { return y
+ z
; }
360 assert(a
.foo() == 9);
364 /*******************************************/
375 int foo() { return y
+ z
; }
380 assert(a
.foo() == 9);
384 /*******************************************/
388 int function(int x
) fp
;
390 fp
= function int(int y
) { return y
+ 3; };
394 /*******************************************/
399 int function(int x
) fp
;
401 fp
= function int(int y
) { return y
+ a
; };
405 /*******************************************/
411 int delegate(int x
) fp
;
413 fp
= delegate int(int y
) { return y
+ a
; };
418 /*******************************************/
424 void opApply(void delegate(int) fp
)
426 for (int i
= 0; i
< array
.length
; i
++)
431 int func20(Collection20 c
)
435 c
.opApply(delegate(int i
) { if (i
> max
) max
= i
; });
441 Collection20 c
= new Collection20();
452 /*******************************************/
460 b
+= c
; // 4 is added to b
461 c
++; // bar.c is now 5
462 return b
+ c
; // 12 is returned
465 int i
= foo(a
); // i is set to 12
466 return i
+ c
; // returns 17
471 int i
= bar21(3); // i is assigned 17
475 /*******************************************/
477 void foo22(void delegate() baz
)
485 printf("%d,%d\n",i
,j
);
489 printf("%d,%d\n",i
,j
);
490 assert(i
== 12 && j
== 14);
503 /*******************************************/
505 void frelled(void delegate() baz
)
515 printf("%d,%d\n",i
,j
);
519 printf("%d,%d\n",i
,j
);
530 Foo23 f
= new Foo23();
536 /*******************************************/
538 void delegate () function (int x
) store24
;
540 void delegate () zoom24(int x
)
542 return delegate void () { };
552 /*******************************************/
556 delegate() { printf("stop the insanity!\n"); }();
557 delegate() { printf("stop the insanity! 2\n"); }();
560 /*******************************************/
562 alias bool delegate(int) callback26
;
565 bool foo26(callback26 a
)
574 printf("func(v=%d)\n", v
);
575 foo26(delegate bool(int a
)
577 printf("%d %d\n",a
,v
); return true;
589 Bar26 b
= new Bar26();
595 /*******************************************/
612 A27 myInstance
= new A27
;
613 int i
= myInstance
.myFunc();
619 /*******************************************/
621 void Foo28(void delegate() call)
632 Foo28(delegate void() { ++count
; } );
639 Bar28 b
= new Bar28();
645 /*******************************************/
649 void Func(void delegate() call)
651 for(int i
= 0; i
< 10; ++i
)
661 Foo29 ic
= new Foo29();
663 ic
.Func(delegate void() { ++count
; } );
670 Bar29 b
= new Bar29();
675 /*******************************************/
682 void Func30(Foo30 bar
)
684 void InnerFunc(int x
, int y
)
686 int a
= bar
.arr
[y
]; // Ok
688 if(bar
.arr
[y
]) // Access violation
706 /*******************************************/
708 void call31(int d
, void delegate(int d
) f
)
710 assert(d
== 100 || d
== 200);
711 printf("d = %d\n", d
);
717 call31(100, delegate void(int d1
)
719 printf("d1 = %d\n", d1
);
721 call31(200, delegate void(int d2
)
723 printf("d1 = %d\n", d1
);
724 printf("d2 = %d\n", d2
);
732 /*******************************************/
734 void call32(int d
, void delegate(int d
) f
)
736 assert(d
== 100 || d
== 200);
737 printf("d = %d\n", d
);
743 call32(100, delegate void(int d1
)
747 printf("d1 = %d, a = %d, b = %d\n", d1
, a
, b
);
752 call32(200, delegate void(int d2
)
754 printf("d1 = %d, a = %d\n", d1
, a
);
755 printf("d2 = %d, b = %d\n", d2
, b
);
765 /*******************************************/
769 extern (C
) int Foo1(int a
, int b
, int c
)
777 extern (D
) int Foo2(int a
, int b
, int c
)
785 extern (Windows
) int Foo3(int a
, int b
, int c
)
793 extern (Pascal
) int Foo4(int a
, int b
, int c
)
801 assert(Foo1(1, 2, 3) == 1);
802 assert(Foo2(1, 2, 3) == 2);
803 assert(Foo3(1, 2, 3) == 3);
804 assert(Foo4(1, 2, 3) == 4);
806 printf("test33 success\n");
809 /*******************************************/
819 int delegate() getDelegate()
846 Foo34 foo
= new Foo34();
847 int delegate() saydg
= foo
.bar
.getDelegate();
848 printf("This should print Hello:\n");
853 /*******************************************/
865 //writefln("x = %s", x);
866 //writefln("y = %s", y);
869 //static assert(is(typeof(this.outer) == void*)); // Bugzilla 14442
870 static assert(is(typeof(this.outer
) == Foo35
)); // Bugzilla 15839
878 Foo35 f
= new Foo35();
882 /*******************************************/
894 //writefln("x = %s", x);
895 //writefln("y = %s", y);
905 Foo36 f
= new Foo36();
908 /*******************************************/
922 //writefln("x = %s", x);
923 //writefln("y = %s", y);
936 Foo37 f
= new Foo37();
940 /*******************************************/
961 int delegate() bar
= foo();
979 /*******************************************/
997 int delegate() bar
= foo();
1015 /*******************************************/
1019 void get( string s
);
1028 I40 i
= new class() I40
1030 void get( string s
)
1037 void func( ){ assert(a
== 4); }
1046 /*******************************************/
1076 /*******************************************/
1114 /*******************************************/
1116 int foo43(alias X
)() { return X
; }
1125 assert(foo43
!(x
)() == 3);
1126 assert(foo43
!(y
)() == 4);
1131 assert(foo43
!(x
)() == 3);
1135 /*******************************************/
1141 Comb
Foo44(Comb
delegate()[] c
...)
1144 printf("1ec = %p\n", ec
);
1146 printf("2ec = %p\n", ec
);
1159 c44
= Foo44(Foo44(c44
));
1162 /*******************************************/
1169 Inner i
= new Inner
;
1178 Inner i
= new Inner
;
1192 Bar45 b
= new Bar45
;
1197 /*******************************************/
1213 class AnonAdapter
: Adapter
1226 Adapter a
= cast( Adapter
)( new AnonAdapter() );
1227 assert(a
.func() == 73);
1234 Foo46 f
= new Foo46();
1239 /*******************************************/
1243 void delegate() test =
1245 struct Foo
{int x
=3;}
1252 /*******************************************/
1258 this(int i
) { b
= i
; }
1268 auto x
= new Inner(a
);
1283 /*******************************************/
1288 void mainlocal(int x
)
1290 printf("mainlocal: j = %d, x = %d\n", j
, x
);
1298 void fun2local(int x
)
1300 printf("fun2local: k = %d, x = %d\n", k
, x
);
1318 /*******************************************/
1320 void funa50(alias pred1
, alias pred2
)()
1326 void funb50(alias pred1
)()
1328 void funb50local(int x
)
1330 printf("funb50local: k = %d, x = %d\n", k
, x
);
1334 funa50
!(pred1
, funb50local
)();
1340 void mainlocal(int x
)
1342 printf("mainlocal: j = %d, x = %d\n", j
, x
);
1346 funb50
!(mainlocal
)();
1349 /*******************************************/
1351 void funa51(alias pred1
, alias pred2
)()
1357 void funb51(alias pred1
)()
1359 void funb51local(int x
)
1361 printf("funb51local: k = %d, x = %d\n", k
, x
);
1365 funa51
!(funb51local
, pred1
)();
1371 void mainlocal(int x
)
1373 printf("mainlocal: j = %d, x = %d\n", j
, x
);
1377 funb51
!(mainlocal
)();
1380 /*******************************************/
1388 printf( "this = %p, index = %d\n", this, index
);
1390 assert(this == c52
);
1402 { // Access to the outer-super-field triggers the bug
1415 printf("c = %p\n", c
);
1420 /*******************************************/
1427 int bar() { return x
+ i
+ 1; }
1431 assert(s
.bar() == 11);
1439 /*******************************************/
1444 int fun(int i
) { return x
+ i
; }
1448 int bar(int i
) { return fun(i
); }
1464 assert(a
.bar(2) == 42);
1467 assert(b
.bar(3) == 43);
1470 assert(c
.bar(4) == 44);
1472 /*******************************************/
1478 int inner(int delegate(ref int) dg
) {
1483 int a
= localvar
* localvar
; // This modifies the EAX register
1485 foreach (entry
; &inner
)
1490 /*******************************************/
1499 auto bar() nothrow pure {
1503 auto baz() @property pure @safe {
1507 auto zoo()() @property pure @safe nothrow {
1512 /*******************************************/
1514 alias void delegate() dg_t
;
1516 void Y(dg_t
delegate (dg_t
) y
)
1518 struct F
{ void delegate(F
) f
; }
1521 { // generates error
1522 (dg_t
delegate(F
) a
){return a(F((F b
){return y(a(b
))();})); }
1523 ((F b
){return (){return b
.f(b
);};});
1527 auto abc(dg_t
delegate(F
) a
)
1529 return a(F((F b
){return y(a(b
))();}));
1532 abc((F b
){return (){return b
.f(b
);};});
1540 void bar() { self(); }
1547 /*******************************************/
1550 struct S4612a(alias x
)
1552 void* func() { void* p
= &x
; return p
; }
1555 struct S4612b(alias x
)
1558 void* func() { void* p
= &x
; return p
; }
1565 auto sa
= S4612a
!a();
1566 assert(sa
.func() == &a
);
1568 auto sb
= S4612b
!(a
)();
1569 assert(sb
.func() == &a
);
1572 /*******************************************/
1574 struct S4841(alias pred
)
1588 /*******************************************/
1609 /*******************************************/
1618 printf("&x = %p in main()\n", &x
);
1623 printf("&x = %p in S1.boom()\n", &x
);
1625 //x = 42; // makes the struct nested
1634 printf("&x = %p in S2.this()\n", &x
);
1645 uint unused1
, unused2
= 0;
1648 auto f7965(alias fun
)()
1653 this(S7965 _s
) { s
= _s
; } // required for the problem
1654 void g() { assert(fun(s
.str) == "xa"); }
1657 return Result(S7965("a"));
1663 f7965
!(a
=> s
~= a
)().g();
1667 /*******************************************/
1670 mixin template Print8188(b
...)
1681 mixin Print8188
!(x
, y
);
1689 assert(a
.doprint() == 10);
1692 /*******************************************/
1695 struct S5082
{ float x
; }
1697 struct Map5082a(alias fun
)
1699 typeof({ return fun(int.init
); }()) cache
;
1702 struct Map5082b(alias fun
)
1704 typeof({ return fun(int.init
); }()) cache
;
1706 S5082
front(int i
) { return fun(i
); }
1711 auto temp
= S5082(1);
1712 auto func
= (int v
){ return temp
; };
1713 auto map1
= Map5082a
!func();
1714 auto map2
= Map5082b
!func();
1715 assert(map2
.front(1) == temp
);
1719 /*******************************************/
1728 static assert(is(typeof(baz
) == int));
1732 /*******************************************/
1735 template map8339a(fun
...)
1737 auto map8339a(Range
)(Range r
) {
1739 this(double[] input
) {}
1744 template map8339b(fun
...)
1746 auto map8339b(Range
)(Range r
) {
1748 this(double[] input
) { fun
[0](input
.length
); }
1753 template map8339c(fun
...)
1755 auto map8339c(Range
)(Range r
) {
1756 static struct Result
{
1757 this(double[] input
) {}
1762 template map8339d(fun
...)
1764 auto map8339d(Range
)(Range r
) {
1765 static struct Result
{
1766 this(double[] input
) { fun
[0](input
.length
); }
1771 void copy8339(T
)(T x
)
1780 // Result has context pointer, so cannot copy
1781 static assert (!is(typeof({ copy8339(map8339a
!(a
=>a
)(x
)); })));
1782 static assert (!is(typeof({ copy8339(map8339a
!(a
=>n
)(x
)); })));
1785 static assert (!is(typeof({ copy8339(map8339b
!(a
=>a
)(x
)); })));
1786 static assert (!is(typeof({ copy8339(map8339b
!(a
=>n
)(x
)); })));
1788 // fun is never instantiated
1789 copy8339(map8339c
!(a
=>a
)(x
));
1790 copy8339(map8339c
!(a
=>n
)(x
));
1792 // static nested struct doesn't have contest pointer
1793 copy8339(map8339d
!(a
=>a
)(x
));
1794 //copy8339(map8339d!(a=>n)(x)); // too strict case
1798 template filter8339(alias pred
)
1800 auto filter8339(R
)(R r
) {
1803 this(R r
) { range
= r
; }
1804 auto front() { return pred(0); }
1811 static makefilter() { int n
; return filter8339
!(a
=>n
)([]); }
1813 auto r1
= makefilter();
1814 filter8339
!(a
=>a
)(r1
);
1820 class C1
{ C
!C1 x
; }
1823 struct Pair
{ C1 t
; C2 u
; void func(){} }
1827 /*******************************************/
1830 void check8704(T
, int num
)()
1832 static if (num
== 1) T t0
;
1833 static if (num
== 2) T t1
= T();
1834 static if (num
== 3) T t2
= T(1);
1845 static assert(!is(typeof(check8704
!(S
, 1)())));
1846 static assert(!is(typeof(check8704
!(S
, 2)())));
1847 static assert(!is(typeof(check8704
!(S
, 3)())));
1849 static assert(!__traits(compiles
, check8704
!(S
, 1)()));
1850 static assert(!__traits(compiles
, check8704
!(S
, 2)()));
1851 static assert(!__traits(compiles
, check8704
!(S
, 3)()));
1854 /*******************************************/
1861 struct S
// is nested struct
1863 void foo() { val
= 1; } // access to val through the hidden frame pointer
1866 S s1b
= S(); s1b
.foo();
1867 S
[1] s2a
; s2a
[0].foo();
1868 S
[1] s2b
= S(); s2b
[0].foo();
1870 static struct U
{ S s
; }
1872 U u1b
= U(); u1b
.s
.foo();
1873 U u1c
= U(s1a
); u1c
.s
.foo();
1874 U
[1] u2a
; u2a
[0].s
.foo();
1875 U
[1] u2b
= U(); u2b
[0].s
.foo();
1876 U
[1] u2c
= U(s1a
); u2c
[0].s
.foo();
1877 static struct V
{ S
[1] s
; }
1878 V v1a
; v1a
.s
[0].foo();
1879 V v1b
= V(); v1b
.s
[0].foo();
1880 V v1c
= V(s1a
); v1c
.s
[0].foo();
1881 V
[1] v2a
; v2a
[0].s
[0].foo();
1882 V
[1] v2b
= V(); v2b
[0].s
[0].foo();
1883 V
[1] v2c
= V(s1a
); v2c
[0].s
[0].foo();
1885 static struct W
{ S s
; this(S s
){ this.s
= s
; } }
1887 W w1b
= W(); w1b
.s
.foo();
1888 W w1c
= W(s1a
); w1c
.s
.foo();
1889 W
[1] w2a
; w2a
[0].s
.foo();
1890 W
[1] w2b
= W(); w2b
[0].s
.foo();
1891 W
[1] w2c
= W(s1a
); w2c
[0].s
.foo();
1892 static struct X
{ S
[1] s
; this(S s
){ this.s
[] = s
; } }
1893 X x1a
; x1a
.s
[0].foo();
1894 X x1b
= X(); x1b
.s
[0].foo();
1895 X x1c
= X(s1a
); x1c
.s
[0].foo();
1896 X
[1] x2a
; x2a
[0].s
[0].foo();
1897 X
[1] x2b
= X(); x2b
[0].s
[0].foo();
1898 X
[1] x2c
= X(s1a
); x2c
[0].s
[0].foo();
1900 // Both declarations, Y and Z should raise errors,
1901 // because their ctors don't initialize their field 's'.
1902 static assert(!__traits(compiles
, {
1903 static struct Y
{ S s
; this(S
){} }
1905 /+ Y y1a; //y1a.s.foo();
1906 Y y1b = Y(); y1b.s.foo();
1907 Y y1c = Y(s1a);//y1c.s.foo();
1908 Y[1] y2a; //y2a[0].s.foo();
1909 Y[1] y2b = Y(); y2b[0].s.foo();
1910 Y[1] y2c = Y(s1a);//y2c[0].s.foo(); +/
1911 static assert(!__traits(compiles
, {
1912 static struct Z
{ S
[1] s
; this(S
){} }
1914 /+ Z z1a; //z1a.s[0].foo();
1915 Z z1b = Z(); z1b.s[0].foo();
1916 Z z1c = Z(s1a);//z1c.s[0].foo();
1917 Z[1] z2a; //z1a.s[0].foo();
1918 Z[1] z2b = Z(); z1b.s[0].foo();
1919 Z[1] z2c = Z(s1a);//z1c.s[0].foo(); // +/
1922 struct Tuple8923(int v
, T
...)
1925 static if (v
== 1) this(T args
) { } // should be an error
1926 static if (v
== 2) this(T args
) { field
= args
; }
1927 static if (v
== 3) this(U
...)(U args
) { } // should be an error
1928 static if (v
== 4) this(U
...)(U args
) { field
= args
; }
1934 struct S
{ void foo() { val
= 1; } }
1936 static assert(!__traits(compiles
, Tuple8923
!(1, S
)(S()) ));
1937 static assert(!__traits(compiles
, Tuple8923
!(3, S
)(S()) ));
1939 auto tup2
= Tuple8923
!(2, S
)(S());
1940 tup2
.field
[0].foo(); // correctly initialized
1942 auto tup4
= Tuple8923
!(4, S
)(S());
1943 tup4
.field
[0].foo(); // correctly initialized
1950 struct S
// is nested struct
1952 void foo() { val
= 1; } // access to val through the hidden frame pointer
1955 S s1b
= S(); s1b
.foo();
1956 S
[1] s2a
; s2a
[0].foo();
1957 S
[1] s2b
= S(); s2b
[0].foo();
1959 // U,V,W,X are nested struct, but should work same as non-nested.
1960 // 1: bare struct object. 2: static array of structs.
1961 // a: default construction.
1962 // b: construction by literal syntax which has no argument.
1963 // c: construction by literal syntax which has one or more arguments.
1968 void foo() { val
= 2; }
1970 U u1a
; u1a
.foo(); u1a
.s
.foo();
1971 U u1b
= U(); u1b
.foo(); u1b
.s
.foo();
1972 U u1c
= U(s1a
); u1c
.foo(); u1c
.s
.foo();
1973 U
[1] u2a
; u2a
[0].foo(); u2a
[0].s
.foo();
1974 U
[1] u2b
= U(); u2b
[0].foo(); u2b
[0].s
.foo();
1975 U
[1] u2c
= U(s1a
); u2c
[0].foo(); u2c
[0].s
.foo();
1980 void foo() { val
= 2; }
1982 V v1a
; v1a
.foo(); v1a
.s
[0].foo();
1983 V v1b
= V(); v1b
.foo(); v1b
.s
[0].foo();
1984 V v1c
= V(s1a
); v1c
.foo(); v1c
.s
[0].foo();
1985 V
[1] v2a
; v2a
[0].foo(); v2a
[0].s
[0].foo();
1986 V
[1] v2b
= V(); v2b
[0].foo(); v2b
[0].s
[0].foo();
1987 V
[1] v2c
= V(s1a
); v2c
[0].foo(); v2c
[0].s
[0].foo();
1992 this(S s
) { this.s
= s
; }
1993 void foo() { val
= 2; }
1995 W w1a
; w1a
.foo(); w1a
.s
.foo();
1996 W w1b
= W(); w1b
.foo(); w1b
.s
.foo();
1997 W w1c
= W(s1a
); w1c
.foo(); w1c
.s
.foo();
1998 W
[1] w2a
; w2a
[0].foo(); w2a
[0].s
.foo();
1999 W
[1] w2b
= W(); w2b
[0].foo(); w2b
[0].s
.foo();
2000 W
[1] w2c
= W(s1a
); w2c
[0].foo(); w2c
[0].s
.foo();
2005 this(S s
) { this.s
[] = s
; }
2006 void foo() { val
= 2; }
2008 X x1a
; x1a
.foo(); x1a
.s
[0].foo();
2009 X x1b
= X(); x1b
.foo(); x1b
.s
[0].foo();
2010 X x1c
= X(s1a
); x1c
.foo(); x1c
.s
[0].foo();
2011 X
[1] x2a
; x2a
[0].foo(); x2a
[0].s
[0].foo();
2012 X
[1] x2b
= X(); x2b
[0].foo(); x2b
[0].s
[0].foo();
2013 X
[1] x2c
= X(s1a
); x2c
[0].foo(); x2c
[0].s
[0].foo();
2015 // Both declarations, Y and Z should raise errors,
2016 // because their ctors don't initialize their field 's'.
2017 static assert(!__traits(compiles
, {
2018 struct Y1
{ S s
; this(S
){} void foo() { val
= 2; } }
2020 static assert(!__traits(compiles
, {
2021 struct Y2
{ S s
; this(T
)(S
){} void foo() { val
= 2; } }
2022 auto y2
= Y2
!S(S()); // instantiate ctor
2025 static assert(!__traits(compiles
, {
2026 struct Z1
{ S
[1] s
; this(S
){} void foo() { val
= 2; } }
2028 static assert(!__traits(compiles
, {
2029 struct Z2
{ S
[1] s
; this(T
)(S
){} void foo() { val
= 2; } }
2030 auto z2
= Z2
!S(S()); // instantiate ctor
2034 /*******************************************/
2041 int n1
; // Comment to pass all asserts
2042 int n2
; // Uncomment to fail assert on line 19
2043 int f() { return i
; }
2050 assert(ss1
.ns
!= NS
.init
);
2056 assert(ss2
.ns1
!= NS
.init
); // line 19
2057 assert(ss2
.ns2
!= NS
.init
);
2065 assert(ss3
.ns
!= NS
.init
);
2073 assert(ss4
.ns1
!= NS
.init
); // fails
2074 assert(ss4
.ns2
!= NS
.init
); // fails
2077 /*******************************************/
2086 int[3] a
; // Uncomment to fail assert on line 20 and pass on line 23
2087 int f() { return i
; }
2090 assert(ns
!= NS
.init
);
2092 assert(ns
== NS
.init
);
2094 static struct SS
{ NS ns
; }
2095 assert(SS
.init
.ns
== NS
.init
); // fails
2096 assert(SS
.init
.ns
!= NS()); // fails
2099 assert(s
.ns
!= NS
.init
); // line 20
2100 assert(s
!= SS
.init
); // fails
2102 assert(s
.ns
== NS
.init
); // line 23, fails
2103 assert(s
== SS
.init
);
2106 /*******************************************/
2113 void f(T
)(auto ref T t
)
2115 static assert(!__traits(isRef
, t
));
2118 f(S
.init
); // ok, rvalue
2119 f(S()); // ok, rvalue
2124 int j
= 0; void f() { ++i
; }
2127 f(Nested()); // ok, rvalue
2128 f(Nested
.init
); // fails, lvalue
2130 assert(Nested
.init
.j
== 0);
2131 //(ref n) { n.j = 5; }(Nested.init);
2132 assert(Nested
.init
.j
== 0); // fails, j is 5
2140 void foo() { x
= 1; }
2144 assert(s1
!= S
.init
); // OK
2145 assert(s2
!= S
.init
); // OK
2146 assert(S() != S
.init
); // NG -> OK
2149 /*******************************************/
2161 assert(i
== 0); // postblit not called
2163 assert(i
== 0); // postblit not called
2172 assert(this.tupleof
[$-1] != null); // fails
2178 assert(j
== 0); // fails, j = 1, postblit called
2180 assert(j
== 0); // fails, j = 2, postblit called
2183 /*******************************************/
2186 auto fun8863(T)(T* ret) { *ret = T(); }
2206 /*******************************************/
2211 int[20] abc
; // smash stack
2214 struct MapResult8774(alias fun
)
2216 void delegate() front()
2224 int sliceSize
= 100;
2226 void delegate() foo( int i
)
2228 void delegate() closedPartialSum()
2233 printf("%d\n", sliceSize
);
2234 assert(sliceSize
== 100);
2238 return closedPartialSum();
2241 auto threads
= MapResult8774
!foo();
2243 auto dg
= threads
.front();
2246 printf("calling dg()\n");
2250 /*******************************************/
2252 int Bug8832(alias X
)()
2257 int delegate() foo8832()
2267 return delegate int() { return Bug8832
!(nested_func
); };
2279 /*******************************************/
2289 pragma(msg
, S
.init
.tupleof
[$-1]);
2292 /*******************************************/
2299 @safe int x() { return i
; }
2303 /*******************************************/
2311 struct Y10495(alias f
)
2318 X10495 s
= X10495
.init
;
2326 /*******************************************/
2329 auto map11385(alias fun
, R
)(R range
)
2331 return MapResult11385
!(fun
, R
)(range
);
2333 struct MapResult11385(alias fun
, R
)
2336 auto front() { return fun(range
[0]); }
2340 //import std.algorithm;
2341 static auto fun1(T
)(T a
) { return a
* 2; }
2342 auto fun2(T
)(T a
) { return a
* 2; }
2343 [1].map11385
!(a
=>fun1(a
)); // OK
2344 [1].map11385
!(a
=>fun2(a
)); // NG:XXX is a nested function and cannot be accessed from XXX
2347 /*******************************************/
2349 void xmap(alias g
)(int t
)
2354 enum foo11297
= function (int x
)
2356 //int bar(int y) { return x; } xmap!bar(7);
2360 void xreduce(alias f
)()
2370 /*******************************************/
2373 struct Lambda11886(alias fun
)
2375 auto opCall(A
...)(A args
) { return fun(args
); }
2380 Lambda11886
!(x
=> x
+ n
) f
;
2381 assert(f(1) == 11); // Line 9
2385 auto foo(T
)(T t
) { return t
* n
; }
2387 static assert(NS
.tupleof
.length
== 1);
2388 static assert(NS
.sizeof
== (void*).sizeof
);
2390 assert(ns
.foo(2) == 20);
2393 /*******************************************/
2401 this(int aa
) { a
= aa
; }
2406 static assert(is(typeof(foo
) == delegate));
2412 /*******************************************/
2415 template Mix12981(T
)
2419 alias typeof(this.outer
) x
;
2425 mixin Mix12981
!(int);
2427 static assert(is(A
.x
== B12981
));
2430 /*******************************************/
2433 struct Foo13861(alias f
)
2439 return Bar(); // OK <- Segfault
2446 Foo13861
!(n
=> n
) a
;
2449 /*******************************************/
2472 // hidden fields correctly set
2473 assert(this.tupleof
[$-1] !is null);
2474 assert(inner
.tupleof
[$-1] !is null);
2478 Outer
[1] arr1
= [Outer(0)];
2479 assert(outer
== 0); // no postblit called on arr1 construction
2481 assert(outer
== 42); // inner is copied successfully
2484 /*******************************************/
2487 void foo14846(Dg
)(scope Dg code
)
2489 static assert(is(Dg
== delegate));
2499 this(int n
) { x
= n
; }
2504 foo14846({ S s
= S(); });
2505 foo14846({ S s
= S(1); });
2506 foo14846({ S
[3] s
; });
2508 foo14846({ S
* p
= new S(); });
2509 foo14846({ S
* p
= new S(1); });
2510 foo14846({ S
[] a
= [S()]; });
2511 foo14846({ S
[] a
= [S(1)]; });
2514 /*******************************************/
2522 in {} body // necessary to reproduce the crash
2533 // Frame of test2 function, allocated on heap.
2534 assert(foo
.tupleof
[$-1] !is null);
2536 //printf("&foo = %p\n", &foo); // stack
2537 //printf("&this = %p\n", &this); // stack?
2538 //printf("foo.vthis = %p\n", foo.tupleof[$-1]); // stack...!?
2539 //assert(cast(void*)&this !is *cast(void**)&foo.tupleof[$-1], "bad");
2540 // BUG: currently foo.vthis set to the address of 'this' variable on the stack.
2541 // It's should be stomped to null, because Foo.vthis is never be used.
2544 map
[foo
] = 1; // OK <- crash
2555 this(int k
) { closVar
= k
; }
2556 // Make val a closure variable.
2564 // Frame of test2 function, allocated on heap.
2565 assert(foo
.tupleof
[$-1] !is null);
2573 alias App
= App15422
!int;
2576 auto x
= app1
.test1(1);
2577 auto y
= app1
.test1(1);
2578 static assert(is(typeof(x
) == typeof(y
)));
2580 // int (bitwise comparison)
2583 assert(*cast(void**)&x
.tupleof
[$-1] is *cast(void**)&y
.tupleof
[$-1]);
2585 // bitwise equality (needOpEquals() and needToHash() returns false)
2589 //assert(*cast(void**)&x.tupleof[$-1] is null);
2590 //assert(*cast(void**)&y.tupleof[$-1] is null);
2591 auto getZ() { auto z
= app1
.test1(1); return z
; }
2594 //assert(x.tupleof[$-1] is z.tupleof[$-1]); // should pass
2595 //assert(x == z); // should pass
2597 x
= y
; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2601 auto x
= app1
.test2(1);
2602 auto y
= app2
.test2(1);
2603 static assert(is(typeof(x
) == typeof(y
)));
2605 // int (bitwise comparison)
2608 // closure envirionments
2609 assert(*cast(void**)&x
.tupleof
[$-1] !is *cast(void**)&y
.tupleof
[$-1]);
2611 // Changed to bitwise equality (needOpEquals() and needToHash() returns false)
2612 assert(x
!= y
); // OK <- crash
2614 x
= y
; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2620 alias App
= App15422
!string
;
2623 auto x
= app1
.test1("a".idup
);
2624 auto y
= app1
.test1("a".idup
);
2625 static assert(is(typeof(x
) == typeof(y
)));
2627 // string (element-wise comparison)
2630 assert(*cast(void**)&x
.tupleof
[$-1] is *cast(void**)&y
.tupleof
[$-1]);
2632 // memberwise equality (needToHash() returns true)
2634 // Lowered to: x.a == y.a && x.tupleof[$-1] is y.tupleof[$-1]
2637 //assert(*cast(void**)&x.tupleof[$-1] is null);
2638 //assert(*cast(void**)&y.tupleof[$-1] is null);
2639 auto getZ() { auto z
= app1
.test1("a".idup
); return z
; }
2642 //assert(x.tupleof[$-1] is z.tupleof[$-1]); // should pass
2643 //assert(x == z); // should pass
2645 x
= y
; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2649 auto x
= app1
.test2("a".idup
);
2650 auto y
= app2
.test2("a".idup
);
2651 static assert(is(typeof(x
) == typeof(y
)));
2653 // string (element-wise comparison)
2656 // closure envirionments
2657 assert(*cast(void**)&x
.tupleof
[$-1] !is *cast(void**)&y
.tupleof
[$-1]);
2659 // Changed to memberwise equality (needToHash() returns true)
2660 // Lowered to: x.a == y.a && x.tupleof[$-1] is y.tupleof[$-1]
2661 assert(x
!= y
); // OK <- crash
2663 x
= y
; // OK, x.tupleof[$-1] = y.tupleof[$-1] is a blit copy.
2667 /***************************************************/
2670 template map15757(fun
...)
2672 auto map15757(R
)(R r
)
2674 return MapResult15757
!(fun
, R
)(r
);
2678 struct MapResult15757(alias fun
, R
)
2688 void wrap15757(R
)(R r
)
2702 void test15757() @safe
2704 [1,2,3].map15757
!(x
=> x
*x
).wrap15757
;
2707 /***************************************************/
2804 printf("Success\n");