| 1 |
/* |
| 2 |
RUN_OUTPUT: |
| 3 |
--- |
| 4 |
Success |
| 5 |
--- |
| 6 |
*/ |
| 7 |
extern (C) int printf(const(char*) fmt, ...); |
| 8 |
|
| 9 |
static int foo(); |
| 10 |
|
| 11 |
/**********************************/ |
| 12 |
|
| 13 |
auto red()() |
| 14 |
{ |
| 15 |
return foo(); |
| 16 |
} |
| 17 |
|
| 18 |
void test13350() |
| 19 |
{ |
| 20 |
int[] data; |
| 21 |
assert(is(typeof(red()))); |
| 22 |
} |
| 23 |
|
| 24 |
/**********************************/ |
| 25 |
|
| 26 |
struct A1(T) { void f1() {} } |
| 27 |
struct B1(T) { void f2() {} } |
| 28 |
|
| 29 |
A1!(B1!int) func1()() { return typeof(return).init; } |
| 30 |
|
| 31 |
void test1() |
| 32 |
{ |
| 33 |
static if (is(typeof(func1()) R : X!(Y), alias X, Y)) |
| 34 |
{ |
| 35 |
R.init.f1(); |
| 36 |
Y.init.f2(); |
| 37 |
} |
| 38 |
else |
| 39 |
static assert(0); |
| 40 |
} |
| 41 |
|
| 42 |
/**********************************/ |
| 43 |
|
| 44 |
struct A2(T) { void f1() { foo(); } } |
| 45 |
struct B2(T) { void f2() { foo(); } } |
| 46 |
|
| 47 |
A2!(B2!int) func2()() { return typeof(return).init; } |
| 48 |
|
| 49 |
void test2() |
| 50 |
{ |
| 51 |
static if (is(typeof(func2()))) |
| 52 |
{ |
| 53 |
} |
| 54 |
else |
| 55 |
static assert(0); |
| 56 |
} |
| 57 |
|
| 58 |
/**********************************/ |
| 59 |
|
| 60 |
template A3() { void foo() { B3!().bar(); } } |
| 61 |
template B3() { void bar() {} } |
| 62 |
|
| 63 |
void test3() |
| 64 |
{ |
| 65 |
// A3!() and B3!() are marked as 'speculative' |
| 66 |
static assert(is(typeof(A3!().foo()))); |
| 67 |
|
| 68 |
// A3!() is unspeculative, but B3!() isn't. |
| 69 |
A3!().foo(); |
| 70 |
|
| 71 |
// in codegen phase, B3!() will generate its members, because |
| 72 |
// the tinst chain contains unspeculative instance A3!(). |
| 73 |
} |
| 74 |
|
| 75 |
/**********************************/ |
| 76 |
|
| 77 |
struct S4(T) |
| 78 |
{ |
| 79 |
string toString() const { return "instantiated"; } |
| 80 |
} |
| 81 |
|
| 82 |
void test4() |
| 83 |
{ |
| 84 |
// inside typeof is not speculative context |
| 85 |
alias X = typeof(S4!int()); |
| 86 |
assert(typeid(X).xtoString !is null); |
| 87 |
} |
| 88 |
|
| 89 |
/**********************************/ |
| 90 |
|
| 91 |
struct S5(T) |
| 92 |
{ |
| 93 |
string toString() const { return "instantiated"; } |
| 94 |
} |
| 95 |
|
| 96 |
void test5() |
| 97 |
{ |
| 98 |
enum x = S5!int(); |
| 99 |
assert(x.toString() == "instantiated"); |
| 100 |
} |
| 101 |
|
| 102 |
/**********************************/ |
| 103 |
|
| 104 |
int foo6()() { return 0; } |
| 105 |
|
| 106 |
template A6() { alias f = foo6!(); } |
| 107 |
void testa6()() if (is(typeof(A6!().f))) {} |
| 108 |
|
| 109 |
template B6() { alias f = foo6!(); } |
| 110 |
void testb6()() if (is(typeof(B6!().f))) {} |
| 111 |
|
| 112 |
template C6() { void f() { B6!().f(); } } |
| 113 |
|
| 114 |
void test6() |
| 115 |
{ |
| 116 |
testa6(); |
| 117 |
// foo6!() is speculatively instantiated from A6!() [TemplateInstance a] |
| 118 |
// -> foo6!() is instantiated in A6!(), so it should be inserted to the members of this module. |
| 119 |
|
| 120 |
testb6(); |
| 121 |
// foo6!() is speculatively instantiated from B6!() [TemplateInstance b], |
| 122 |
// but the tinst of cached [TemplateInstance a] is not changed. |
| 123 |
// -> insert [b] to the tnext chain of [a] |
| 124 |
|
| 125 |
C6!().f(); |
| 126 |
// foo6!() is used through C6!(), so it should be linked to the final executable. |
| 127 |
// but its first instance does not link to any non-speculative instances. |
| 128 |
// -> look for tnext chain and determine its codegen is really necessary. |
| 129 |
} |
| 130 |
|
| 131 |
/**********************************/ |
| 132 |
|
| 133 |
int main() |
| 134 |
{ |
| 135 |
test13350(); |
| 136 |
test1(); |
| 137 |
test2(); |
| 138 |
test3(); |
| 139 |
test4(); |
| 140 |
test5(); |
| 141 |
test6(); |
| 142 |
|
| 143 |
printf("Success\n"); |
| 144 |
return 0; |
| 145 |
} |