| 1 |
//https://issues.dlang.org/show_bug.cgi?id=22291 |
| 2 |
|
| 3 |
alias AliasSeq(T...) = T; |
| 4 |
void noParameters() |
| 5 |
{ |
| 6 |
static assert(typeof(__traits(parameters)).length == 0); |
| 7 |
} |
| 8 |
void noArgs() |
| 9 |
{ |
| 10 |
//Arguments are not valid, this should not compile |
| 11 |
static assert(!__traits(compiles, __traits(parameters, 456))); |
| 12 |
} |
| 13 |
shared static this() |
| 14 |
{ |
| 15 |
static assert(typeof(__traits(parameters)).length == 0); |
| 16 |
} |
| 17 |
int echoPlusOne(int x) |
| 18 |
{ |
| 19 |
__traits(parameters)[0] += 1; |
| 20 |
return x; |
| 21 |
} |
| 22 |
static assert(echoPlusOne(1) == 2); |
| 23 |
|
| 24 |
void nesting(double d, int i) |
| 25 |
{ |
| 26 |
alias EXP = AliasSeq!(d, i); |
| 27 |
|
| 28 |
if (d) |
| 29 |
{ |
| 30 |
static assert(__traits(isSame, __traits(parameters), EXP)); |
| 31 |
|
| 32 |
while (d) |
| 33 |
{ |
| 34 |
static assert(__traits(isSame, __traits(parameters), EXP)); |
| 35 |
switch (i) |
| 36 |
{ |
| 37 |
static assert(__traits(isSame, __traits(parameters), EXP)); |
| 38 |
case 1: |
| 39 |
static assert(__traits(isSame, __traits(parameters), EXP)); |
| 40 |
break; |
| 41 |
|
| 42 |
default: |
| 43 |
static assert(__traits(isSame, __traits(parameters), EXP)); |
| 44 |
break; |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
class Tree { |
| 51 |
int opApply(int delegate(size_t, Tree) dg) { |
| 52 |
if (dg(0, this)) return 1; |
| 53 |
return 0; |
| 54 |
} |
| 55 |
} |
| 56 |
void useOpApply(Tree top, int x) |
| 57 |
{ |
| 58 |
foreach(idx; 0..5) |
| 59 |
{ |
| 60 |
static assert(is(typeof(__traits(parameters)) == AliasSeq!(Tree, int))); |
| 61 |
} |
| 62 |
foreach(idx, elem; top) |
| 63 |
{ |
| 64 |
static assert(is(typeof(__traits(parameters)) == AliasSeq!(Tree, int))); |
| 65 |
} |
| 66 |
|
| 67 |
foreach(idx, elem; top) |
| 68 |
{ |
| 69 |
foreach (idx2, elem2; elem) |
| 70 |
static assert(is(typeof(__traits(parameters)) == AliasSeq!(Tree, int))); |
| 71 |
} |
| 72 |
|
| 73 |
foreach(idx, elem; top) |
| 74 |
{ |
| 75 |
static void foo(char[] text) |
| 76 |
{ |
| 77 |
foreach (const char c; text) |
| 78 |
static assert(is(typeof(__traits(parameters)) == AliasSeq!(char[]))); |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
class Test |
| 83 |
{ |
| 84 |
static assert(!__traits(compiles, __traits(parameters))); |
| 85 |
void handle(int x) |
| 86 |
{ |
| 87 |
static assert(typeof(__traits(parameters)).length == 1); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
int add(int x, int y) |
| 92 |
{ |
| 93 |
return x + y; |
| 94 |
} |
| 95 |
|
| 96 |
auto forwardToAdd(int x, int y) |
| 97 |
{ |
| 98 |
return add(__traits(parameters)); |
| 99 |
} |
| 100 |
static assert(forwardToAdd(2, 3) == 5); |
| 101 |
struct TestConstructor |
| 102 |
{ |
| 103 |
int x; |
| 104 |
string y; |
| 105 |
//This parameter will not have a name but it's (tuple) members |
| 106 |
//will |
| 107 |
this(typeof(this.tupleof)) |
| 108 |
{ |
| 109 |
this.tupleof = __traits(parameters); |
| 110 |
} |
| 111 |
} |
| 112 |
bool test(int x, string y) |
| 113 |
{ |
| 114 |
auto s = TestConstructor(2, "pi"); |
| 115 |
return s.x == x && s.y == y; |
| 116 |
} |
| 117 |
static assert(test(2, "pi")); |
| 118 |
int testNested(int x) |
| 119 |
{ |
| 120 |
static assert(typeof(__traits(parameters)).length == 1); |
| 121 |
int add(int x, int y) |
| 122 |
{ |
| 123 |
static assert(typeof(__traits(parameters)).length == 2); |
| 124 |
return x + y; |
| 125 |
} |
| 126 |
return add(x + 2, x + 3); |
| 127 |
} |
| 128 |
void testPack(Pack...)(Pack x) |
| 129 |
{ |
| 130 |
static assert(is(typeof(__traits(parameters)) == typeof(AliasSeq!(x)))); |
| 131 |
} |
| 132 |
|
| 133 |
ref int forwardTest(return ref int x) |
| 134 |
{ |
| 135 |
static assert(__traits(isRef, x) == __traits(isRef, __traits(parameters)[0])); |
| 136 |
return x; |
| 137 |
} |
| 138 |
|
| 139 |
int testRefness(int x, ref int monkey) |
| 140 |
{ |
| 141 |
{ |
| 142 |
//monkey = x; |
| 143 |
__traits(parameters)[1] = __traits(parameters)[0]; |
| 144 |
} |
| 145 |
return x; |
| 146 |
} |
| 147 |
int refTest() |
| 148 |
{ |
| 149 |
int x; |
| 150 |
testRefness(45, x); |
| 151 |
return x; |
| 152 |
} |
| 153 |
auto packLength(Pack...)(Pack x) |
| 154 |
{ |
| 155 |
return typeof(__traits(parameters)).length; |
| 156 |
} |
| 157 |
static assert(packLength(2, 3) == 2); |
| 158 |
alias lambda = (x) => typeof(__traits(parameters)).stringof; |
| 159 |
static assert(lambda(1) == "(int)"); |
| 160 |
static assert(refTest() == 45); |
| 161 |
|
| 162 |
T testTemplate(T)(scope T input) |
| 163 |
{ |
| 164 |
void chimpInASuit(float set) |
| 165 |
{ |
| 166 |
static assert(is(typeof(__traits(parameters)) == AliasSeq!(float))); |
| 167 |
} |
| 168 |
{ |
| 169 |
__traits(parameters) = AliasSeq!(T.max); |
| 170 |
} |
| 171 |
__traits(parameters) = AliasSeq!(T.init); |
| 172 |
return input; |
| 173 |
} |
| 174 |
|
| 175 |
static assert(testTemplate!long(420) == 0); |
| 176 |
|
| 177 |
void qualifiers(immutable int a, const bool b) |
| 178 |
{ |
| 179 |
static assert(is(typeof(__traits(parameters)) == AliasSeq!(immutable int, const bool))); |
| 180 |
} |
| 181 |
|
| 182 |
int makeAggregate(int a, bool b) |
| 183 |
{ |
| 184 |
struct S |
| 185 |
{ |
| 186 |
typeof(__traits(parameters)) members; |
| 187 |
} |
| 188 |
|
| 189 |
S s = S(__traits(parameters)); |
| 190 |
assert(s.members[0] == a); |
| 191 |
assert(s.members[1] == b); |
| 192 |
return 1; |
| 193 |
} |
| 194 |
|
| 195 |
static assert(makeAggregate(5, true)); |
| 196 |
|
| 197 |
int makeAlias(int a, bool b) |
| 198 |
{ |
| 199 |
alias Params = __traits(parameters); |
| 200 |
assert(Params[0] == 3); |
| 201 |
assert(Params[1] == true); |
| 202 |
return 1; |
| 203 |
} |
| 204 |
|
| 205 |
static assert(makeAlias(3, true)); |
| 206 |
|
| 207 |
|
| 208 |
mixin template nestedCheckParameters(int unique) |
| 209 |
{ |
| 210 |
alias NestedNames = __traits(parameters); |
| 211 |
version (Fixed) |
| 212 |
alias Types = typeof(Names); |
| 213 |
} |
| 214 |
|
| 215 |
mixin template checkParameters(int unique) |
| 216 |
{ |
| 217 |
mixin nestedCheckParameters!unique; |
| 218 |
|
| 219 |
alias Names = __traits(parameters); |
| 220 |
alias Types = typeof(Names); |
| 221 |
} |
| 222 |
|
| 223 |
int makeAggregateMixin(immutable int a, const bool b) |
| 224 |
{ |
| 225 |
mixin checkParameters!0; |
| 226 |
|
| 227 |
struct S |
| 228 |
{ |
| 229 |
mixin checkParameters!1; |
| 230 |
typeof(Names) members; |
| 231 |
} |
| 232 |
|
| 233 |
S s = S(Names); |
| 234 |
assert(s.members[0] == a); |
| 235 |
assert(s.members[1] == b); |
| 236 |
return 1; |
| 237 |
} |