| 1 |
/* |
| 2 |
TEST_OUTPUT: |
| 3 |
--- |
| 4 |
int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe |
| 5 |
int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe |
| 6 |
int |
| 7 |
int |
| 8 |
int[] |
| 9 |
int delegate() pure nothrow @nogc @safe function() pure nothrow @safe |
| 10 |
--- |
| 11 |
|
| 12 |
RUN_OUTPUT: |
| 13 |
--- |
| 14 |
Success |
| 15 |
--- |
| 16 |
*/ |
| 17 |
import core.vararg; |
| 18 |
|
| 19 |
extern (C) int printf(const char*, ...); |
| 20 |
|
| 21 |
/***************************************************/ |
| 22 |
// lambda syntax check |
| 23 |
|
| 24 |
auto una(alias dg)(int n) |
| 25 |
{ |
| 26 |
return dg(n); |
| 27 |
} |
| 28 |
auto bin(alias dg)(int n, int m) |
| 29 |
{ |
| 30 |
return dg(n, m); |
| 31 |
} |
| 32 |
void test1() |
| 33 |
{ |
| 34 |
assert(una!( a => a*2 )(2) == 4); |
| 35 |
assert(una!( ( a) => a*2 )(2) == 4); |
| 36 |
assert(una!( (int a) => a*2 )(2) == 4); |
| 37 |
assert(una!( ( a){ return a*2; } )(2) == 4); |
| 38 |
assert(una!( function ( a){ return a*2; } )(2) == 4); |
| 39 |
assert(una!( function int( a){ return a*2; } )(2) == 4); |
| 40 |
assert(una!( function (int a){ return a*2; } )(2) == 4); |
| 41 |
assert(una!( function int(int a){ return a*2; } )(2) == 4); |
| 42 |
assert(una!( delegate ( a){ return a*2; } )(2) == 4); |
| 43 |
assert(una!( delegate int( a){ return a*2; } )(2) == 4); |
| 44 |
assert(una!( delegate (int a){ return a*2; } )(2) == 4); |
| 45 |
assert(una!( delegate int(int a){ return a*2; } )(2) == 4); |
| 46 |
|
| 47 |
// partial parameter specialization syntax |
| 48 |
assert(bin!( ( a, b) => a*2+b )(2,1) == 5); |
| 49 |
assert(bin!( (int a, b) => a*2+b )(2,1) == 5); |
| 50 |
assert(bin!( ( a, int b) => a*2+b )(2,1) == 5); |
| 51 |
assert(bin!( (int a, int b) => a*2+b )(2,1) == 5); |
| 52 |
assert(bin!( ( a, b){ return a*2+b; } )(2,1) == 5); |
| 53 |
assert(bin!( (int a, b){ return a*2+b; } )(2,1) == 5); |
| 54 |
assert(bin!( ( a, int b){ return a*2+b; } )(2,1) == 5); |
| 55 |
assert(bin!( (int a, int b){ return a*2+b; } )(2,1) == 5); |
| 56 |
assert(bin!( function ( a, b){ return a*2+b; } )(2,1) == 5); |
| 57 |
assert(bin!( function (int a, b){ return a*2+b; } )(2,1) == 5); |
| 58 |
assert(bin!( function ( a, int b){ return a*2+b; } )(2,1) == 5); |
| 59 |
assert(bin!( function (int a, int b){ return a*2+b; } )(2,1) == 5); |
| 60 |
assert(bin!( function int( a, b){ return a*2+b; } )(2,1) == 5); |
| 61 |
assert(bin!( function int(int a, b){ return a*2+b; } )(2,1) == 5); |
| 62 |
assert(bin!( function int( a, int b){ return a*2+b; } )(2,1) == 5); |
| 63 |
assert(bin!( function int(int a, int b){ return a*2+b; } )(2,1) == 5); |
| 64 |
assert(bin!( delegate ( a, b){ return a*2+b; } )(2,1) == 5); |
| 65 |
assert(bin!( delegate (int a, b){ return a*2+b; } )(2,1) == 5); |
| 66 |
assert(bin!( delegate ( a, int b){ return a*2+b; } )(2,1) == 5); |
| 67 |
assert(bin!( delegate (int a, int b){ return a*2+b; } )(2,1) == 5); |
| 68 |
assert(bin!( delegate int( a, b){ return a*2+b; } )(2,1) == 5); |
| 69 |
assert(bin!( delegate int(int a, b){ return a*2+b; } )(2,1) == 5); |
| 70 |
assert(bin!( delegate int( a, int b){ return a*2+b; } )(2,1) == 5); |
| 71 |
assert(bin!( delegate int(int a, int b){ return a*2+b; } )(2,1) == 5); |
| 72 |
} |
| 73 |
|
| 74 |
/***************************************************/ |
| 75 |
// on initializer |
| 76 |
|
| 77 |
void test2() |
| 78 |
{ |
| 79 |
// explicit typed binding ignite parameter types inference |
| 80 |
int function(int) fn1 = a => a*2; assert(fn1(2) == 4); |
| 81 |
int function(int) fn2 = ( a){ return a*2; }; assert(fn2(2) == 4); |
| 82 |
int function(int) fn3 = function ( a){ return a*2; }; assert(fn3(2) == 4); |
| 83 |
int function(int) fn4 = function int( a){ return a*2; }; assert(fn4(2) == 4); |
| 84 |
int function(int) fn5 = function (int a){ return a*2; }; assert(fn5(2) == 4); |
| 85 |
int function(int) fn6 = function int(int a){ return a*2; }; assert(fn6(2) == 4); |
| 86 |
int delegate(int) dg1 = a => a*2; assert(dg1(2) == 4); |
| 87 |
int delegate(int) dg2 = ( a){ return a*2; }; assert(dg2(2) == 4); |
| 88 |
int delegate(int) dg3 = delegate ( a){ return a*2; }; assert(dg3(2) == 4); |
| 89 |
int delegate(int) dg4 = delegate int( a){ return a*2; }; assert(dg4(2) == 4); |
| 90 |
int delegate(int) dg5 = delegate (int a){ return a*2; }; assert(dg5(2) == 4); |
| 91 |
int delegate(int) dg6 = delegate int(int a){ return a*2; }; assert(dg6(2) == 4); |
| 92 |
|
| 93 |
// function/delegate mismatching always raises an error |
| 94 |
static assert(!__traits(compiles, { int function(int) xfg3 = delegate ( a){ return a*2; }; })); |
| 95 |
static assert(!__traits(compiles, { int function(int) xfg4 = delegate int( a){ return a*2; }; })); |
| 96 |
static assert(!__traits(compiles, { int function(int) xfg5 = delegate (int a){ return a*2; }; })); |
| 97 |
static assert(!__traits(compiles, { int function(int) xfg6 = delegate int(int a){ return a*2; }; })); |
| 98 |
static assert(!__traits(compiles, { int delegate(int) xdn3 = function ( a){ return a*2; }; })); |
| 99 |
static assert(!__traits(compiles, { int delegate(int) xdn4 = function int( a){ return a*2; }; })); |
| 100 |
static assert(!__traits(compiles, { int delegate(int) xdn5 = function (int a){ return a*2; }; })); |
| 101 |
static assert(!__traits(compiles, { int delegate(int) xdn6 = function int(int a){ return a*2; }; })); |
| 102 |
|
| 103 |
// auto binding requires explicit parameter types at least |
| 104 |
static assert(!__traits(compiles, { auto afn1 = a => a*2; })); |
| 105 |
static assert(!__traits(compiles, { auto afn2 = ( a){ return a*2; }; })); |
| 106 |
static assert(!__traits(compiles, { auto afn3 = function ( a){ return a*2; }; })); |
| 107 |
static assert(!__traits(compiles, { auto afn4 = function int( a){ return a*2; }; })); |
| 108 |
static assert(!__traits(compiles, { auto adg3 = delegate ( a){ return a*2; }; })); |
| 109 |
static assert(!__traits(compiles, { auto adg4 = delegate int( a){ return a*2; }; })); |
| 110 |
auto afn5 = function (int a){ return a*2; }; assert(afn5(2) == 4); |
| 111 |
auto afn6 = function int(int a){ return a*2; }; assert(afn6(2) == 4); |
| 112 |
auto adg5 = delegate (int a){ return a*2; }; assert(adg5(2) == 4); |
| 113 |
auto adg6 = delegate int(int a){ return a*2; }; assert(adg6(2) == 4); |
| 114 |
|
| 115 |
// partial specialized lambda |
| 116 |
string delegate(int, string) dg = |
| 117 |
(n, string s){ |
| 118 |
string r = ""; |
| 119 |
foreach (_; 0..n) r~=s; |
| 120 |
return r; |
| 121 |
}; |
| 122 |
assert(dg(2, "str") == "strstr"); |
| 123 |
} |
| 124 |
|
| 125 |
/***************************************************/ |
| 126 |
// on return statement |
| 127 |
|
| 128 |
void test3() |
| 129 |
{ |
| 130 |
// inference matching system is same as on initializer |
| 131 |
int delegate(int) mul(int x) |
| 132 |
{ |
| 133 |
return a => a * x; |
| 134 |
} |
| 135 |
assert(mul(5)(2) == 10); |
| 136 |
} |
| 137 |
|
| 138 |
/***************************************************/ |
| 139 |
// on function arguments |
| 140 |
|
| 141 |
auto foo4(int delegate(int) dg) { return dg(10); } |
| 142 |
auto foo4(int delegate(int, int) dg) { return dg(10, 20); } |
| 143 |
|
| 144 |
void nbar4fp(void function(int) fp) { } |
| 145 |
void nbar4dg(void delegate(int) dg) { } |
| 146 |
void tbar4fp(T,R)(R function(T) dg) { static assert(is(typeof(dg) == void function(int))); } |
| 147 |
void tbar4dg(T,R)(R delegate(T) dg) { static assert(is(typeof(dg) == void delegate(int))); } |
| 148 |
|
| 149 |
auto nbaz4(void function() fp) { return 1; } |
| 150 |
auto nbaz4(void delegate() dg) { return 2; } |
| 151 |
auto tbaz4(R)(R function() dg) { static assert(is(R == void)); return 1; } |
| 152 |
auto tbaz4(R)(R delegate() dg) { static assert(is(R == void)); return 2; } |
| 153 |
|
| 154 |
auto thoo4(T)(T lambda){ return lambda; } |
| 155 |
|
| 156 |
void tfun4a()(int function(int) a){} |
| 157 |
void tfun4b(T)(T function(T) a){} |
| 158 |
void tfun4c(T)(T f){} |
| 159 |
|
| 160 |
void test4() |
| 161 |
{ |
| 162 |
int v; |
| 163 |
static void sfoo() {} |
| 164 |
void nfoo() {} |
| 165 |
|
| 166 |
// parameter type inference + overload resolution |
| 167 |
assert(foo4((a) => a * 2) == 20); |
| 168 |
assert(foo4((a,b) => a * 2 + b) == 40); |
| 169 |
|
| 170 |
// function/delegate inference |
| 171 |
nbar4fp((int x){ }); |
| 172 |
nbar4dg((int x){ }); |
| 173 |
tbar4fp((int x){ }); |
| 174 |
tbar4dg((int x){ }); |
| 175 |
|
| 176 |
// function/delegate inference + overload resolution |
| 177 |
assert(nbaz4({ }) == 1); |
| 178 |
assert(nbaz4({ v = 1; }) == 2); |
| 179 |
assert(nbaz4({ sfoo(); }) == 1); // https://issues.dlang.org/show_bug.cgi?id=8836 |
| 180 |
assert(nbaz4({ nfoo(); }) == 2); |
| 181 |
|
| 182 |
assert(tbaz4({ }) == 1); |
| 183 |
assert(tbaz4({ v = 1; }) == 2); |
| 184 |
assert(tbaz4({ sfoo(); }) == 1); |
| 185 |
assert(tbaz4({ nfoo(); }) == 2); |
| 186 |
|
| 187 |
// template function deduction |
| 188 |
static assert(is(typeof(thoo4({ })) : void function())); |
| 189 |
static assert(is(typeof(thoo4({ v = 1; })) : void delegate())); |
| 190 |
|
| 191 |
tfun4a(a => a); |
| 192 |
static assert(!__traits(compiles, { tfun4b(a => a); })); |
| 193 |
static assert(!__traits(compiles, { tfun4c(a => a); })); |
| 194 |
} |
| 195 |
|
| 196 |
void fsvarg4(int function(int)[] a...){} |
| 197 |
void fcvarg4(int dummy, ...){} |
| 198 |
|
| 199 |
void tsvarg4a()(int function(int)[] a...){} |
| 200 |
void tsvarg4b(T)(T function(T)[] a...){} |
| 201 |
void tsvarg4c(T)(T [] a...){} |
| 202 |
void tcvarg4()(int dummy, ...){} |
| 203 |
|
| 204 |
void test4v() |
| 205 |
{ |
| 206 |
fsvarg4(function(int a){ return a; }); // OK |
| 207 |
fsvarg4(a => a); // OK |
| 208 |
|
| 209 |
fcvarg4(0, function(int a){ return a; }); // OK |
| 210 |
static assert(!__traits(compiles, { fcvarg4(0, a => a); })); |
| 211 |
|
| 212 |
tsvarg4a(function(int a){ return a; }); // OK |
| 213 |
tsvarg4b(function(int a){ return a; }); // OK |
| 214 |
tsvarg4c(function(int a){ return a; }); // OK |
| 215 |
tsvarg4a(a => a); |
| 216 |
static assert(!__traits(compiles, { tsvarg4b(a => a); })); |
| 217 |
static assert(!__traits(compiles, { tsvarg4c(a => a); })); |
| 218 |
|
| 219 |
tcvarg4(0, function(int a){ return a; }); // OK |
| 220 |
static assert(!__traits(compiles, { tcvarg4(0, a => a); })); |
| 221 |
} |
| 222 |
|
| 223 |
// A lambda in function default argument should be deduced to delegate, by the |
| 224 |
// preparation inferType call in TypeFunction.semantic. |
| 225 |
void test4_findRoot(scope bool delegate(real lo, real hi) tolerance = (real a, real b) => false) |
| 226 |
{} |
| 227 |
|
| 228 |
/***************************************************/ |
| 229 |
// on CallExp::e1 |
| 230 |
|
| 231 |
void test5() |
| 232 |
{ |
| 233 |
assert((a => a*2)(10) == 20); |
| 234 |
assert(( a, s){ return s~s; }(10, "str") == "strstr"); |
| 235 |
assert((int a, s){ return s~s; }(10, "str") == "strstr"); |
| 236 |
assert(( a, string s){ return s~s; }(10, "str") == "strstr"); |
| 237 |
assert((int a, string s){ return s~s; }(10, "str") == "strstr"); |
| 238 |
} |
| 239 |
|
| 240 |
/***************************************************/ |
| 241 |
// escape check to nested function symbols |
| 242 |
|
| 243 |
void checkNestedRef(alias dg)(bool isnested) |
| 244 |
{ |
| 245 |
static if (is(typeof(dg) == delegate)) |
| 246 |
enum isNested = true; |
| 247 |
else static if ((is(typeof(dg) PF == F*, F) && is(F == function))) |
| 248 |
enum isNested = false; |
| 249 |
else |
| 250 |
static assert(0); |
| 251 |
|
| 252 |
assert(isnested == isNested); |
| 253 |
dg(); |
| 254 |
} |
| 255 |
|
| 256 |
void freeFunc(){} |
| 257 |
|
| 258 |
void test6() |
| 259 |
{ |
| 260 |
static void localFunc(){} |
| 261 |
void nestedLocalFunc(){} |
| 262 |
|
| 263 |
checkNestedRef!({ })(false); |
| 264 |
|
| 265 |
checkNestedRef!({ freeFunc(); })(false); |
| 266 |
checkNestedRef!({ localFunc(); })(false); |
| 267 |
checkNestedRef!({ nestedLocalFunc(); })(true); |
| 268 |
checkNestedRef!({ void inner(){} inner(); })(false); |
| 269 |
|
| 270 |
checkNestedRef!({ auto f = &freeFunc; })(false); |
| 271 |
checkNestedRef!({ auto f = &localFunc; })(false); |
| 272 |
checkNestedRef!({ auto f = &nestedLocalFunc; })(true); |
| 273 |
checkNestedRef!({ void inner(){} auto f = &inner; })(false); |
| 274 |
} |
| 275 |
|
| 276 |
/***************************************************/ |
| 277 |
// on AssignExp::e2 |
| 278 |
|
| 279 |
void test7() |
| 280 |
{ |
| 281 |
int function(int) fp; |
| 282 |
fp = a => a; |
| 283 |
fp = (int a) => a; |
| 284 |
fp = function(int a) => a; |
| 285 |
fp = function int(int a) => a; |
| 286 |
static assert(!__traits(compiles, { fp = delegate(int a) => a; })); |
| 287 |
static assert(!__traits(compiles, { fp = delegate int(int a) => a; })); |
| 288 |
|
| 289 |
int delegate(int) dg; |
| 290 |
dg = a => a; |
| 291 |
dg = (int a) => a; |
| 292 |
dg = delegate(int a) => a; |
| 293 |
dg = delegate int(int a) => a; |
| 294 |
static assert(!__traits(compiles, { dg = function(int a) => a; })); |
| 295 |
static assert(!__traits(compiles, { dg = function int(int a) => a; })); |
| 296 |
} |
| 297 |
|
| 298 |
/***************************************************/ |
| 299 |
// on StructLiteralExp::elements |
| 300 |
|
| 301 |
void test8() |
| 302 |
{ |
| 303 |
struct S |
| 304 |
{ |
| 305 |
int function(int) fp; |
| 306 |
} |
| 307 |
auto s1 = S(a => a); |
| 308 |
static assert(!__traits(compiles, { auto s2 = S((a, b) => a); })); |
| 309 |
} |
| 310 |
|
| 311 |
/***************************************************/ |
| 312 |
// on concat operation |
| 313 |
|
| 314 |
void test9() |
| 315 |
{ |
| 316 |
int function(int)[] a2; |
| 317 |
a2 ~= x => x; |
| 318 |
} |
| 319 |
|
| 320 |
/***************************************************/ |
| 321 |
// on associative array key |
| 322 |
|
| 323 |
void test10() |
| 324 |
{ |
| 325 |
int[int function()] aa; |
| 326 |
assert(!aa.remove(() => 1)); |
| 327 |
|
| 328 |
int[int function(int)] aa2; |
| 329 |
assert(!aa2.remove(x => 1)); |
| 330 |
} |
| 331 |
|
| 332 |
/***************************************************/ |
| 333 |
// on common type deduction |
| 334 |
|
| 335 |
void test11() |
| 336 |
{ |
| 337 |
auto a1 = [x => x, (int x) => x * 2]; |
| 338 |
static assert(is(typeof(a1[0]) == int function(int) pure @safe nothrow @nogc)); |
| 339 |
assert(a1[0](10) == 10); |
| 340 |
assert(a1[1](10) == 20); |
| 341 |
|
| 342 |
//int n = 10; |
| 343 |
//auto a2 = [x => n, (int x) => x * 2]; |
| 344 |
//static assert(is(typeof(a2[0]) == int delegate(int) @safe nothrow)); |
| 345 |
//assert(a2[0](99) == 10); |
| 346 |
//assert(a2[1](10) == 20); |
| 347 |
|
| 348 |
int function(int) fp = true ? (x => x) : (x => x*2); |
| 349 |
assert(fp(10) == 10); |
| 350 |
|
| 351 |
int m = 10; |
| 352 |
int delegate(int) dg = true ? (x => x) : (x => m*2); |
| 353 |
assert(dg(10) == 10); |
| 354 |
} |
| 355 |
|
| 356 |
/***************************************************/ |
| 357 |
// https://issues.dlang.org/show_bug.cgi?id=3235 |
| 358 |
|
| 359 |
void test3235() |
| 360 |
{ |
| 361 |
// from TDPL |
| 362 |
auto f = (int i) {}; |
| 363 |
static if (is(typeof(f) _ == F*, F) && is(F == function)) |
| 364 |
{} else static assert(0); |
| 365 |
} |
| 366 |
|
| 367 |
/***************************************************/ |
| 368 |
// https://issues.dlang.org/show_bug.cgi?id=6714 |
| 369 |
|
| 370 |
void foo6714x(int function (int, int) a){} |
| 371 |
void bar6714x(int delegate (int, int) a){} |
| 372 |
|
| 373 |
int bar6714y(double delegate(int, int) a){ return 1; } |
| 374 |
int bar6714y( int delegate(int, int) a){ return 2; } |
| 375 |
|
| 376 |
void test6714() |
| 377 |
{ |
| 378 |
foo6714x((a, b) { return a + b; }); |
| 379 |
bar6714x((a, b) { return a + b; }); |
| 380 |
|
| 381 |
assert(bar6714y((a, b){ return 1.0; }) == 1); |
| 382 |
assert(bar6714y((a, b){ return 1.0f; }) == 1); |
| 383 |
assert(bar6714y((a, b){ return a; }) == 2); |
| 384 |
} |
| 385 |
|
| 386 |
/***************************************************/ |
| 387 |
// https://issues.dlang.org/show_bug.cgi?id=7207 |
| 388 |
// on CastExp |
| 389 |
|
| 390 |
void test7202() |
| 391 |
{ |
| 392 |
auto dg = cast(int function(int))(a => a); |
| 393 |
assert(dg(10) == 10); |
| 394 |
} |
| 395 |
|
| 396 |
/***************************************************/ |
| 397 |
// https://issues.dlang.org/show_bug.cgi?id=7288 |
| 398 |
|
| 399 |
void test7288() |
| 400 |
{ |
| 401 |
// 7288 -> OK |
| 402 |
auto foo() |
| 403 |
{ |
| 404 |
int x; |
| 405 |
return () { return () => x; }; |
| 406 |
} |
| 407 |
pragma(msg, typeof(&foo)); |
| 408 |
alias int delegate() pure nothrow @nogc @safe delegate() pure nothrow @nogc @safe delegate() pure nothrow @safe Dg; |
| 409 |
pragma(msg, Dg); |
| 410 |
static assert(is(typeof(&foo) == Dg)); // should pass |
| 411 |
} |
| 412 |
|
| 413 |
/***************************************************/ |
| 414 |
// https://issues.dlang.org/show_bug.cgi?id=7499 |
| 415 |
|
| 416 |
void test7499() |
| 417 |
{ |
| 418 |
int function(int)[] a1 = [ x => x ]; // 7499 |
| 419 |
int function(int)[][] a2 = [[x => x]]; // +a |
| 420 |
assert(a1[0] (10) == 10); |
| 421 |
assert(a2[0][0](10) == 10); |
| 422 |
} |
| 423 |
|
| 424 |
/***************************************************/ |
| 425 |
// https://issues.dlang.org/show_bug.cgi?id=7500 |
| 426 |
|
| 427 |
void test7500() |
| 428 |
{ |
| 429 |
alias immutable bool function(int[]) Foo; |
| 430 |
Foo f = a => true; |
| 431 |
} |
| 432 |
|
| 433 |
/***************************************************/ |
| 434 |
// https://issues.dlang.org/show_bug.cgi?id=7525 |
| 435 |
|
| 436 |
void test7525() |
| 437 |
{ |
| 438 |
{ |
| 439 |
char[] delegate() a = { return null; }; |
| 440 |
int delegate() b = { return 1U; }; |
| 441 |
uint delegate() c = { return 1; }; |
| 442 |
float delegate() d = { return 1.0; }; |
| 443 |
double delegate() e = { return 1.0f; }; |
| 444 |
} |
| 445 |
|
| 446 |
{ |
| 447 |
char[] delegate(int) a = (x){ return null; }; |
| 448 |
int delegate(int) b = (x){ return 1U; }; |
| 449 |
uint delegate(int) c = (x){ return 1; }; |
| 450 |
float delegate(int) d = (x){ return 1.0; }; |
| 451 |
double delegate(int) e = (x){ return 1.0f; }; |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/***************************************************/ |
| 456 |
// https://issues.dlang.org/show_bug.cgi?id=7582 |
| 457 |
|
| 458 |
void test7582() |
| 459 |
{ |
| 460 |
void delegate(int) foo; |
| 461 |
void delegate(int) foo2; |
| 462 |
foo = (a) { |
| 463 |
foo2 = (b) { }; |
| 464 |
}; |
| 465 |
} |
| 466 |
|
| 467 |
/***************************************************/ |
| 468 |
// https://issues.dlang.org/show_bug.cgi?id=7649 |
| 469 |
|
| 470 |
void test7649() |
| 471 |
{ |
| 472 |
void foo(int function(int) fp = x => 1) |
| 473 |
{ |
| 474 |
assert(fp(1) == 1); |
| 475 |
} |
| 476 |
foo(); |
| 477 |
} |
| 478 |
|
| 479 |
/***************************************************/ |
| 480 |
// https://issues.dlang.org/show_bug.cgi?id=7650 |
| 481 |
|
| 482 |
void test7650() |
| 483 |
{ |
| 484 |
int[int function(int)] aa1 = [x=>x:1, x=>x*2:2]; |
| 485 |
foreach (k, v; aa1) { |
| 486 |
if (v == 1) assert(k(10) == 10); |
| 487 |
if (v == 2) assert(k(10) == 20); |
| 488 |
} |
| 489 |
|
| 490 |
int function(int)[int] aa2 = [1:x=>x, 2:x=>x*2]; |
| 491 |
assert(aa2[1](10) == 10); |
| 492 |
assert(aa2[2](10) == 20); |
| 493 |
|
| 494 |
int n = 10; |
| 495 |
int[int delegate(int)] aa3 = [x=>n+x:1, x=>n+x*2:2]; |
| 496 |
foreach (k, v; aa3) { |
| 497 |
if (v == 1) assert(k(10) == 20); |
| 498 |
if (v == 2) assert(k(10) == 30); |
| 499 |
} |
| 500 |
|
| 501 |
int delegate(int)[int] aa4 = [1:x=>n+x, 2:x=>n+x*2]; |
| 502 |
assert(aa4[1](10) == 20); |
| 503 |
assert(aa4[2](10) == 30); |
| 504 |
} |
| 505 |
|
| 506 |
/***************************************************/ |
| 507 |
// https://issues.dlang.org/show_bug.cgi?id=7705 |
| 508 |
|
| 509 |
void test7705() |
| 510 |
{ |
| 511 |
void foo1(void delegate(ref int ) dg){ int x=10; dg(x); } |
| 512 |
foo1((ref x){ pragma(msg, typeof(x)); assert(x == 10); }); |
| 513 |
|
| 514 |
void foo2(void delegate(int, ...) dg){ dg(20, 3.14); } |
| 515 |
foo2((x,...){ pragma(msg, typeof(x)); assert(x == 20); }); |
| 516 |
|
| 517 |
void foo3(void delegate(int[]...) dg){ dg(1, 2, 3); } |
| 518 |
foo3((x ...){ pragma(msg, typeof(x)); assert(x == [1,2,3]); }); |
| 519 |
} |
| 520 |
|
| 521 |
/***************************************************/ |
| 522 |
// https://issues.dlang.org/show_bug.cgi?id=7713 |
| 523 |
|
| 524 |
void foo7713(T)(T delegate(in Object) dlg) |
| 525 |
{} |
| 526 |
void test7713() |
| 527 |
{ |
| 528 |
foo7713( (in obj) { return 15; } ); // line 6 |
| 529 |
} |
| 530 |
|
| 531 |
/***************************************************/ |
| 532 |
// https://issues.dlang.org/show_bug.cgi?id=7743 |
| 533 |
|
| 534 |
auto foo7743a() |
| 535 |
{ |
| 536 |
int x = 10; |
| 537 |
return () nothrow { |
| 538 |
return x; |
| 539 |
}; |
| 540 |
} |
| 541 |
auto foo7743b() |
| 542 |
{ |
| 543 |
int x = 10; |
| 544 |
return () nothrow => x; |
| 545 |
} |
| 546 |
void test7743() |
| 547 |
{ |
| 548 |
pragma(msg, typeof(&foo7743a)); |
| 549 |
static assert(is(typeof(&foo7743a) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe)); |
| 550 |
assert(foo7743a()() == 10); |
| 551 |
|
| 552 |
static assert(is(typeof(&foo7743b) == int delegate() pure nothrow @nogc @safe function() pure nothrow @safe)); |
| 553 |
assert(foo7743b()() == 10); |
| 554 |
} |
| 555 |
|
| 556 |
/***************************************************/ |
| 557 |
// https://issues.dlang.org/show_bug.cgi?id=7761 |
| 558 |
|
| 559 |
enum dg7761 = (int a) pure => 2 * a; |
| 560 |
|
| 561 |
void test7761() |
| 562 |
{ |
| 563 |
static assert(is(typeof(dg7761) == int function(int) pure @safe nothrow @nogc)); |
| 564 |
assert(dg7761(10) == 20); |
| 565 |
} |
| 566 |
|
| 567 |
/***************************************************/ |
| 568 |
// https://issues.dlang.org/show_bug.cgi?id=7941 |
| 569 |
|
| 570 |
void test7941() |
| 571 |
{ |
| 572 |
static assert(!__traits(compiles, { enum int c = function(){}; })); |
| 573 |
} |
| 574 |
|
| 575 |
/***************************************************/ |
| 576 |
// https://issues.dlang.org/show_bug.cgi?id=8005 |
| 577 |
|
| 578 |
void test8005() |
| 579 |
{ |
| 580 |
auto n = (a, int n = 2){ return n; }(1); |
| 581 |
assert(n == 2); |
| 582 |
} |
| 583 |
|
| 584 |
/***************************************************/ |
| 585 |
// test8198 |
| 586 |
|
| 587 |
void test8198() |
| 588 |
{ |
| 589 |
T delegate(T) zero(T)(T delegate(T) f) |
| 590 |
{ |
| 591 |
return x => x; |
| 592 |
} |
| 593 |
|
| 594 |
T delegate(T) delegate(T delegate(T)) succ(T)(T delegate(T) delegate(T delegate(T)) n) |
| 595 |
{ |
| 596 |
return f => x => f(n(f)(x)); |
| 597 |
} |
| 598 |
|
| 599 |
uint delegate(uint) delegate(uint delegate(uint)) n = &zero!uint; |
| 600 |
foreach (i; 0..10) |
| 601 |
{ |
| 602 |
assert(n(x => x + 1)(0) == i); |
| 603 |
n = succ(n); |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
/***************************************************/ |
| 608 |
// https://issues.dlang.org/show_bug.cgi?id=8226 |
| 609 |
|
| 610 |
immutable f8226 = (int x) => x * 2; |
| 611 |
|
| 612 |
void test8226() |
| 613 |
{ |
| 614 |
assert(f8226(10) == 20); |
| 615 |
} |
| 616 |
|
| 617 |
/***************************************************/ |
| 618 |
// https://issues.dlang.org/show_bug.cgi?id=8241 |
| 619 |
|
| 620 |
auto exec8241a(alias a = function(x) => x, T...)(T as) |
| 621 |
{ |
| 622 |
return a(as); |
| 623 |
} |
| 624 |
|
| 625 |
auto exec8241b(alias a = (x) => x, T...)(T as) |
| 626 |
{ |
| 627 |
return a(as); |
| 628 |
} |
| 629 |
|
| 630 |
void test8241() |
| 631 |
{ |
| 632 |
exec8241a(2); |
| 633 |
exec8241b(2); |
| 634 |
} |
| 635 |
|
| 636 |
/***************************************************/ |
| 637 |
// https://issues.dlang.org/show_bug.cgi?id=8242 |
| 638 |
|
| 639 |
template exec8242(alias a, T...) |
| 640 |
{ |
| 641 |
auto func8242(T as) |
| 642 |
{ |
| 643 |
return a(as); |
| 644 |
} |
| 645 |
} |
| 646 |
|
| 647 |
mixin exec8242!(x => x, int); |
| 648 |
mixin exec8242!((string x) => x, string); |
| 649 |
|
| 650 |
void test8242() |
| 651 |
{ |
| 652 |
func8242(1); |
| 653 |
func8242(""); |
| 654 |
} |
| 655 |
|
| 656 |
/***************************************************/ |
| 657 |
// https://issues.dlang.org/show_bug.cgi?id=8315 |
| 658 |
|
| 659 |
void test8315() |
| 660 |
{ |
| 661 |
bool b; |
| 662 |
foo8315!(a => b)(); |
| 663 |
} |
| 664 |
|
| 665 |
void foo8315(alias pred)() |
| 666 |
if (is(typeof(pred(1)) == bool)) |
| 667 |
{} |
| 668 |
|
| 669 |
/***************************************************/ |
| 670 |
// https://issues.dlang.org/show_bug.cgi?id=8397 |
| 671 |
|
| 672 |
void test8397() |
| 673 |
{ |
| 674 |
void function(int) f; |
| 675 |
static assert(!is(typeof({ |
| 676 |
f = function(string x) {}; |
| 677 |
}))); |
| 678 |
} |
| 679 |
|
| 680 |
/***************************************************/ |
| 681 |
// https://issues.dlang.org/show_bug.cgi?id=8496 |
| 682 |
|
| 683 |
void test8496() |
| 684 |
{ |
| 685 |
alias extern (C) void function() Func; |
| 686 |
|
| 687 |
Func fp = (){}; |
| 688 |
|
| 689 |
fp = (){}; |
| 690 |
} |
| 691 |
|
| 692 |
/***************************************************/ |
| 693 |
// https://issues.dlang.org/show_bug.cgi?id=8575 |
| 694 |
|
| 695 |
template tfunc8575(func...) |
| 696 |
{ |
| 697 |
auto tfunc8575(U)(U u) { return func[0](u); } |
| 698 |
} |
| 699 |
auto bar8575(T)(T t) |
| 700 |
{ |
| 701 |
return tfunc8575!(a => a)(t); |
| 702 |
} |
| 703 |
void foo8575a() { assert(bar8575(uint.init + 1) == +1); } |
| 704 |
void foo8575b() { assert(bar8575( int.init - 1) == -1); } |
| 705 |
|
| 706 |
void test8575() |
| 707 |
{ |
| 708 |
foo8575a(); |
| 709 |
foo8575b(); |
| 710 |
} |
| 711 |
|
| 712 |
/***************************************************/ |
| 713 |
// https://issues.dlang.org/show_bug.cgi?id=9153 |
| 714 |
|
| 715 |
void writeln9153(string s){} |
| 716 |
|
| 717 |
void test9153() |
| 718 |
{ |
| 719 |
auto tbl1 = [ |
| 720 |
(string x) { writeln9153(x); }, |
| 721 |
(string x) { x ~= 'a'; }, |
| 722 |
]; |
| 723 |
auto tbl2 = [ |
| 724 |
(string x) { x ~= 'a'; }, |
| 725 |
(string x) { writeln9153(x); }, |
| 726 |
]; |
| 727 |
} |
| 728 |
|
| 729 |
/***************************************************/ |
| 730 |
// https://issues.dlang.org/show_bug.cgi?id=9393 |
| 731 |
|
| 732 |
template ifThrown9393a(E) |
| 733 |
{ |
| 734 |
void ifThrown9393a(T)(scope T delegate(E) errHandler) |
| 735 |
{ |
| 736 |
} |
| 737 |
} |
| 738 |
void ifThrown9393b(E, T)(scope T delegate(E) errHandler) |
| 739 |
{ |
| 740 |
} |
| 741 |
|
| 742 |
void foo9393(T)(void delegate(T) dg){ dg(T.init); } |
| 743 |
void foo9393()(void delegate(int) dg){ foo9393!int(dg); } |
| 744 |
|
| 745 |
void test9393() |
| 746 |
{ |
| 747 |
ifThrown9393a!Exception(e => 10); |
| 748 |
ifThrown9393b!Exception(e => 10); |
| 749 |
|
| 750 |
foo9393((x){ assert(x == int.init); }); |
| 751 |
} |
| 752 |
|
| 753 |
/***************************************************/ |
| 754 |
// https://issues.dlang.org/show_bug.cgi?id=9415 |
| 755 |
|
| 756 |
void test9415() |
| 757 |
{ |
| 758 |
int z; |
| 759 |
typeof((int a){return z;}) dg; |
| 760 |
dg = (int a){return z;}; |
| 761 |
} |
| 762 |
|
| 763 |
/***************************************************/ |
| 764 |
// https://issues.dlang.org/show_bug.cgi?id=9628 |
| 765 |
|
| 766 |
template TypeTuple9628(TL...) { alias TypeTuple9628 = TL; } |
| 767 |
void map9628(alias func)() { func(0); } |
| 768 |
|
| 769 |
void test9628() |
| 770 |
{ |
| 771 |
auto items = [[10, 20], [30]]; |
| 772 |
size_t[] res; |
| 773 |
|
| 774 |
res = null; |
| 775 |
foreach (_; 0 .. 2) |
| 776 |
{ |
| 777 |
foreach (sub; items) |
| 778 |
{ |
| 779 |
map9628!(( i){ res ~= sub.length; }); |
| 780 |
map9628!((size_t i){ res ~= sub.length; }); |
| 781 |
} |
| 782 |
} |
| 783 |
assert(res == [2,2,1,1, 2,2,1,1]); |
| 784 |
|
| 785 |
res = null; |
| 786 |
foreach (_; TypeTuple9628!(0, 1)) |
| 787 |
{ |
| 788 |
foreach (sub; items) |
| 789 |
{ |
| 790 |
map9628!(( i){ res ~= sub.length; }); |
| 791 |
map9628!((size_t i){ res ~= sub.length; }); |
| 792 |
} |
| 793 |
} |
| 794 |
assert(res == [2,2,1,1, 2,2,1,1]); |
| 795 |
} |
| 796 |
|
| 797 |
/***************************************************/ |
| 798 |
// https://issues.dlang.org/show_bug.cgi?id=9928 |
| 799 |
|
| 800 |
void test9928() |
| 801 |
{ |
| 802 |
void* smth = (int x) { return x; }; |
| 803 |
} |
| 804 |
|
| 805 |
/***************************************************/ |
| 806 |
// https://issues.dlang.org/show_bug.cgi?id=10133 |
| 807 |
|
| 808 |
ptrdiff_t countUntil10133(alias pred, R)(R haystack) |
| 809 |
{ |
| 810 |
typeof(return) i; |
| 811 |
|
| 812 |
alias T = dchar; |
| 813 |
|
| 814 |
foreach (T elem; haystack) |
| 815 |
{ |
| 816 |
if (pred(elem)) return i; |
| 817 |
++i; |
| 818 |
} |
| 819 |
|
| 820 |
return -1; |
| 821 |
} |
| 822 |
|
| 823 |
bool func10133(string s)() if (countUntil10133!(x => x == 'x')(s) == 1) |
| 824 |
{ |
| 825 |
return true; |
| 826 |
} |
| 827 |
|
| 828 |
bool func10133a(string s)() if (countUntil10133!(x => s == "x")(s) != -1) |
| 829 |
{ |
| 830 |
return true; |
| 831 |
} |
| 832 |
bool func10133b(string s)() if (countUntil10133!(x => s == "x")(s) != -1) |
| 833 |
{ |
| 834 |
return true; |
| 835 |
} |
| 836 |
|
| 837 |
void test10133() |
| 838 |
{ |
| 839 |
func10133!("ax")(); |
| 840 |
|
| 841 |
func10133a!("x")(); |
| 842 |
static assert(!is(typeof(func10133a!("ax")()))); |
| 843 |
static assert(!is(typeof(func10133b!("ax")()))); |
| 844 |
func10133b!("x")(); |
| 845 |
} |
| 846 |
|
| 847 |
/***************************************************/ |
| 848 |
// https://issues.dlang.org/show_bug.cgi?id=10219 |
| 849 |
|
| 850 |
void test10219() |
| 851 |
{ |
| 852 |
interface I { } |
| 853 |
class C : I { } |
| 854 |
|
| 855 |
void test_dg(I delegate(C) dg) |
| 856 |
{ |
| 857 |
C c = new C; |
| 858 |
void* cptr = cast(void*) c; |
| 859 |
void* iptr = cast(void*) cast(I) c; |
| 860 |
void* xptr = cast(void*) dg(c); |
| 861 |
assert(cptr != iptr); |
| 862 |
assert(cptr != xptr); // should pass |
| 863 |
assert(iptr == xptr); // should pass |
| 864 |
} |
| 865 |
|
| 866 |
C delegate(C c) dg = delegate C(C c) { return c; }; |
| 867 |
static assert(!__traits(compiles, { test_dg(dg); })); |
| 868 |
static assert(!__traits(compiles, { test_dg(delegate C(C c) { return c; }); })); |
| 869 |
static assert(!__traits(compiles, { I delegate(C) dg2 = dg; })); |
| 870 |
|
| 871 |
// creates I delegate(C) |
| 872 |
test_dg(c => c); |
| 873 |
test_dg(delegate(C c) => c); |
| 874 |
|
| 875 |
void test_fp(I function(C) fp) |
| 876 |
{ |
| 877 |
C c = new C; |
| 878 |
void* cptr = cast(void*) c; |
| 879 |
void* iptr = cast(void*) cast(I) c; |
| 880 |
void* xptr = cast(void*) fp(c); |
| 881 |
assert(cptr != iptr); |
| 882 |
assert(cptr != xptr); // should pass |
| 883 |
assert(iptr == xptr); // should pass |
| 884 |
} |
| 885 |
|
| 886 |
C function(C c) fp = function C(C c) { return c; }; |
| 887 |
static assert(!__traits(compiles, { test_fp(fp); })); |
| 888 |
static assert(!__traits(compiles, { test_fp(function C(C c) { return c; }); })); |
| 889 |
static assert(!__traits(compiles, { I function(C) fp2 = fp; })); |
| 890 |
|
| 891 |
// creates I function(C) |
| 892 |
test_fp(c => c); |
| 893 |
test_fp(function(C c) => c); |
| 894 |
} |
| 895 |
|
| 896 |
/***************************************************/ |
| 897 |
// https://issues.dlang.org/show_bug.cgi?id=10288 |
| 898 |
|
| 899 |
T foo10288(T)(T x) |
| 900 |
{ |
| 901 |
void lambda() @trusted nothrow { x += 10; } |
| 902 |
lambda(); |
| 903 |
return x; |
| 904 |
} |
| 905 |
|
| 906 |
T bar10288(T)(T x) |
| 907 |
{ |
| 908 |
() @trusted { x += 10; } (); |
| 909 |
return x; |
| 910 |
} |
| 911 |
|
| 912 |
T baz10288(T)(T arg) |
| 913 |
{ |
| 914 |
static int g = 10; |
| 915 |
() @trusted { x += g; } (); |
| 916 |
return x; |
| 917 |
} |
| 918 |
|
| 919 |
void test10288() @safe pure nothrow |
| 920 |
{ |
| 921 |
assert(foo10288(10) == 20); // OK |
| 922 |
assert(bar10288(10) == 20); // OK <- NG |
| 923 |
static assert(!__traits(compiles, baz10288(10))); |
| 924 |
} |
| 925 |
|
| 926 |
/***************************************************/ |
| 927 |
// https://issues.dlang.org/show_bug.cgi?id=10666 |
| 928 |
|
| 929 |
struct S10666 |
| 930 |
{ |
| 931 |
int val; |
| 932 |
~this() {} |
| 933 |
} |
| 934 |
|
| 935 |
void foo10666(S10666 s1) |
| 936 |
{ |
| 937 |
S10666 s2; |
| 938 |
|
| 939 |
/* Even if closureVars(s1 and s2) are accessed by directly called lambdas, |
| 940 |
* they won't escape the scope of this function. |
| 941 |
*/ |
| 942 |
auto x1 = (){ return s1.val; }(); // OK |
| 943 |
auto x2 = (){ return s2.val; }(); // OK |
| 944 |
} |
| 945 |
|
| 946 |
/***************************************************/ |
| 947 |
// https://issues.dlang.org/show_bug.cgi?id=11081 |
| 948 |
|
| 949 |
T ifThrown11081(E : Throwable, T)(T delegate(E) errorHandler) |
| 950 |
{ |
| 951 |
return errorHandler(); |
| 952 |
} |
| 953 |
|
| 954 |
void test11081() |
| 955 |
{ |
| 956 |
static if (__traits(compiles, ifThrown11081!Exception(e => 0))) |
| 957 |
{ |
| 958 |
} |
| 959 |
static if (__traits(compiles, ifThrown11081!Exception(e => 0))) |
| 960 |
{ |
| 961 |
} |
| 962 |
} |
| 963 |
|
| 964 |
/***************************************************/ |
| 965 |
// https://issues.dlang.org/show_bug.cgi?id=11220 |
| 966 |
|
| 967 |
int parsePrimaryExp11220(int x) |
| 968 |
{ |
| 969 |
parseAmbig11220!( (parsed){ x += 1; } )(); |
| 970 |
return 1; |
| 971 |
} |
| 972 |
|
| 973 |
typeof(handler(1)) parseAmbig11220(alias handler)() |
| 974 |
{ |
| 975 |
return handler(parsePrimaryExp11220(1)); |
| 976 |
} |
| 977 |
|
| 978 |
/***************************************************/ |
| 979 |
// https://issues.dlang.org/show_bug.cgi?id=11230 |
| 980 |
|
| 981 |
template map11230(fun...) |
| 982 |
{ |
| 983 |
auto map11230(Range)(Range r) |
| 984 |
{ |
| 985 |
return MapResult11230!(fun, Range)(r); |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
struct MapResult11230(alias fun, R) |
| 990 |
{ |
| 991 |
R _input; |
| 992 |
this(R input) { _input = input; } |
| 993 |
} |
| 994 |
|
| 995 |
class A11230 { A11230[] as; } |
| 996 |
class B11230 { A11230[] as; } |
| 997 |
class C11230 : A11230 {} |
| 998 |
|
| 999 |
C11230 visit11230(A11230 a) |
| 1000 |
{ |
| 1001 |
a.as.map11230!(a => visit11230); |
| 1002 |
return null; |
| 1003 |
} |
| 1004 |
C11230 visit11230(B11230 b) |
| 1005 |
{ |
| 1006 |
b.as.map11230!(a => visit11230); |
| 1007 |
return null; |
| 1008 |
} |
| 1009 |
C11230 visit11230() |
| 1010 |
{ |
| 1011 |
return null; |
| 1012 |
} |
| 1013 |
|
| 1014 |
/***************************************************/ |
| 1015 |
// https://issues.dlang.org/show_bug.cgi?id=10336 |
| 1016 |
|
| 1017 |
struct S10336 |
| 1018 |
{ |
| 1019 |
template opDispatch(string name) |
| 1020 |
{ |
| 1021 |
enum opDispatch = function(int x) { |
| 1022 |
return x; |
| 1023 |
}; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
void test10336() |
| 1028 |
{ |
| 1029 |
S10336 s; |
| 1030 |
assert(s.hello(12) == 12); |
| 1031 |
} |
| 1032 |
|
| 1033 |
/***************************************************/ |
| 1034 |
// https://issues.dlang.org/show_bug.cgi?id=10928 |
| 1035 |
|
| 1036 |
struct D10928 |
| 1037 |
{ |
| 1038 |
int x; |
| 1039 |
~this() @nogc {} |
| 1040 |
} |
| 1041 |
|
| 1042 |
void f10928a(D10928 bar) |
| 1043 |
{ |
| 1044 |
(){ bar.x++; }(); |
| 1045 |
} |
| 1046 |
void f10928b(D10928 bar) @nogc |
| 1047 |
{ |
| 1048 |
(){ bar.x++; }(); |
| 1049 |
} |
| 1050 |
|
| 1051 |
void test10928() |
| 1052 |
{ |
| 1053 |
f10928a(D10928.init); |
| 1054 |
f10928b(D10928.init); |
| 1055 |
} |
| 1056 |
|
| 1057 |
/***************************************************/ |
| 1058 |
// https://issues.dlang.org/show_bug.cgi?id=11661 |
| 1059 |
|
| 1060 |
void test11661() |
| 1061 |
{ |
| 1062 |
void delegate() dg = {}; // OK |
| 1063 |
void function() fp = {}; // OK <- NG |
| 1064 |
} |
| 1065 |
|
| 1066 |
/***************************************************/ |
| 1067 |
// https://issues.dlang.org/show_bug.cgi?id=11774 |
| 1068 |
|
| 1069 |
void f11774(T, R)(R delegate(T[]) dg) |
| 1070 |
{ |
| 1071 |
T[] src; |
| 1072 |
dg(src); |
| 1073 |
} |
| 1074 |
|
| 1075 |
void test11774() |
| 1076 |
{ |
| 1077 |
int[] delegate(int[]) dg = (int[] a) => a; |
| 1078 |
f11774!int(dg); |
| 1079 |
f11774!Object(a => a); |
| 1080 |
f11774!int(dg); |
| 1081 |
} |
| 1082 |
|
| 1083 |
/***************************************************/ |
| 1084 |
// https://issues.dlang.org/show_bug.cgi?id=12421 |
| 1085 |
|
| 1086 |
void test12421() |
| 1087 |
{ |
| 1088 |
void test(string decl, bool polymorphic = true)() |
| 1089 |
{ |
| 1090 |
// parse AliasDeclaration in Statement |
| 1091 |
mixin("alias f = " ~ decl ~ ";"); |
| 1092 |
assert(f(1) == 1); |
| 1093 |
static if (polymorphic) |
| 1094 |
assert(f("s") == "s"); |
| 1095 |
|
| 1096 |
// parse AliasDeclaration in DeclDefs |
| 1097 |
mixin("template X() { alias g = " ~ decl ~ "; }"); |
| 1098 |
alias g = X!().g; |
| 1099 |
assert(g(1) == 1); |
| 1100 |
static if (polymorphic) |
| 1101 |
assert(g("s") == "s"); |
| 1102 |
} |
| 1103 |
|
| 1104 |
test!(q{ x => x }); |
| 1105 |
test!(q{ ( x) => x }); |
| 1106 |
test!(q{ (int x) => x }, false); |
| 1107 |
|
| 1108 |
test!(q{ ( x){ return x; } }); |
| 1109 |
test!(q{ (int x){ return x; } }, false); |
| 1110 |
|
| 1111 |
test!(q{ function ( x) => x }); |
| 1112 |
test!(q{ function (int x) => x }, false); |
| 1113 |
test!(q{ function int ( x) => x }, false); |
| 1114 |
test!(q{ function int (int x) => x }, false); |
| 1115 |
|
| 1116 |
test!(q{ delegate ( x) => x }); |
| 1117 |
test!(q{ delegate (int x) => x }, false); |
| 1118 |
test!(q{ delegate int ( x) => x }, false); |
| 1119 |
test!(q{ delegate int (int x) => x }, false); |
| 1120 |
|
| 1121 |
test!(q{ function ( x){ return x; } }); |
| 1122 |
test!(q{ function (int x){ return x; } }, false); |
| 1123 |
test!(q{ function int ( x){ return x; } }, false); |
| 1124 |
test!(q{ function int (int x){ return x; } }, false); |
| 1125 |
|
| 1126 |
test!(q{ delegate ( x){ return x; } }); |
| 1127 |
test!(q{ delegate (int x){ return x; } }, false); |
| 1128 |
test!(q{ delegate int ( x){ return x; } }, false); |
| 1129 |
test!(q{ delegate int (int x){ return x; } }, false); |
| 1130 |
|
| 1131 |
// This is problematic case, and should be disallowed in the future. |
| 1132 |
alias f = x => y; |
| 1133 |
int y = 10; |
| 1134 |
assert(f(1) == 10); |
| 1135 |
} |
| 1136 |
|
| 1137 |
/***************************************************/ |
| 1138 |
// https://issues.dlang.org/show_bug.cgi?id=12508 |
| 1139 |
|
| 1140 |
interface A12508(T) |
| 1141 |
{ |
| 1142 |
T getT(); |
| 1143 |
} |
| 1144 |
|
| 1145 |
class C12508 : A12508!double |
| 1146 |
{ |
| 1147 |
double getT() { return 1; } |
| 1148 |
} |
| 1149 |
|
| 1150 |
void f12508(A12508!double delegate() dg) |
| 1151 |
{ |
| 1152 |
auto a = dg(); |
| 1153 |
assert(a !is null); |
| 1154 |
assert(a.getT() == 1.0); // fails! |
| 1155 |
} |
| 1156 |
|
| 1157 |
void t12508(T)(A12508!T delegate() dg) |
| 1158 |
{ |
| 1159 |
auto a = dg(); |
| 1160 |
assert(a !is null); |
| 1161 |
assert(a.getT() == 1.0); // fails! |
| 1162 |
} |
| 1163 |
|
| 1164 |
ref alias Dg12508 = A12508!double delegate(); |
| 1165 |
void t12508(T)(Dg12508 dg) {} |
| 1166 |
|
| 1167 |
void test12508() |
| 1168 |
{ |
| 1169 |
f12508({ return new C12508(); }); |
| 1170 |
t12508({ return new C12508(); }); |
| 1171 |
static assert(!__traits(compiles, x12508({ return new C12508(); }))); |
| 1172 |
} |
| 1173 |
|
| 1174 |
/***************************************************/ |
| 1175 |
// https://issues.dlang.org/show_bug.cgi?id=13879 |
| 1176 |
|
| 1177 |
void test13879() |
| 1178 |
{ |
| 1179 |
bool function(int)[2] funcs1 = (int x) => true; // OK |
| 1180 |
assert(funcs1[0] is funcs1[1]); |
| 1181 |
funcs1[0] = x => true; // OK |
| 1182 |
bool function(int)[2] funcs2 = x => true; // OK <- Error |
| 1183 |
assert(funcs2[0] is funcs2[1]); |
| 1184 |
} |
| 1185 |
|
| 1186 |
/***************************************************/ |
| 1187 |
// https://issues.dlang.org/show_bug.cgi?id=14745 |
| 1188 |
|
| 1189 |
void test14745() |
| 1190 |
{ |
| 1191 |
// qualified nested functions |
| 1192 |
auto foo1() pure immutable { return 0; } |
| 1193 |
auto foo2() pure const { return 0; } |
| 1194 |
|
| 1195 |
// qualified lambdas (== implicitly marked as delegate literals) |
| 1196 |
auto lambda1 = () pure immutable { return 0; }; |
| 1197 |
auto lambda2 = () pure const { return 0; }; |
| 1198 |
static assert(is(typeof(lambda1) : typeof(&foo1))); |
| 1199 |
static assert(is(typeof(lambda2) : typeof(&foo2))); |
| 1200 |
|
| 1201 |
// qualified delegate literals |
| 1202 |
auto dg1 = delegate () pure immutable { return 0; }; |
| 1203 |
auto dg2 = delegate () pure const { return 0; }; |
| 1204 |
static assert(is(typeof(dg1) : typeof(&foo1))); |
| 1205 |
static assert(is(typeof(dg2) : typeof(&foo2))); |
| 1206 |
} |
| 1207 |
|
| 1208 |
/***************************************************/ |
| 1209 |
// https://issues.dlang.org/show_bug.cgi?id=15794 |
| 1210 |
|
| 1211 |
struct Foo15794 |
| 1212 |
{ |
| 1213 |
static void fun(Holder)() |
| 1214 |
{ |
| 1215 |
int i = Holder.fn(); |
| 1216 |
} |
| 1217 |
} |
| 1218 |
|
| 1219 |
struct Holder15794(alias Fn) |
| 1220 |
{ |
| 1221 |
alias fn = Fn; |
| 1222 |
} |
| 1223 |
|
| 1224 |
void gun15794(alias fn, U...)() |
| 1225 |
{ |
| 1226 |
Foo15794.fun!(Holder15794!fn)(); |
| 1227 |
} |
| 1228 |
|
| 1229 |
void test15794() |
| 1230 |
{ |
| 1231 |
gun15794!(() => 0)(); // Line 26 |
| 1232 |
} |
| 1233 |
|
| 1234 |
/***************************************************/ |
| 1235 |
// https://issues.dlang.org/show_bug.cgi?id=16271 |
| 1236 |
|
| 1237 |
ref auto funa16271(alias dg, T)(ref T a) |
| 1238 |
{ |
| 1239 |
return dg(a); |
| 1240 |
} |
| 1241 |
|
| 1242 |
ref auto func16271(alias dg)() |
| 1243 |
{ |
| 1244 |
return dg(); |
| 1245 |
} |
| 1246 |
|
| 1247 |
void assign16271(T)(ref T a, T b) |
| 1248 |
{ |
| 1249 |
alias fun = ref (ref a) => a; |
| 1250 |
fun(a) = b; |
| 1251 |
} |
| 1252 |
|
| 1253 |
void test16271() |
| 1254 |
{ |
| 1255 |
int x; |
| 1256 |
(delegate ref => x)() = -1; assert(x == -1); |
| 1257 |
(ref () => x )() = 1; assert(x == 1); |
| 1258 |
func16271!(ref () => x) = 2; assert(x == 2); |
| 1259 |
assign16271(x, 3); assert(x == 3); |
| 1260 |
|
| 1261 |
alias alx = func16271!(ref () => x); |
| 1262 |
alx = 4; assert(x == 4); |
| 1263 |
|
| 1264 |
alias alf = ref (ref a) => a; |
| 1265 |
auto auf = ref (ref int a) => a; |
| 1266 |
alf(x) = 5; assert(x == 5); |
| 1267 |
auf(x) = 6; assert(x == 6); |
| 1268 |
|
| 1269 |
assert((funa16271!( ref (ref a) => a)(x) += 1) == 7 ); |
| 1270 |
assert((funa16271!(function ref (ref a) => a)(x) += 1) == 8 ); |
| 1271 |
assert((funa16271!(function ref int(ref a) => a)(x) += 1) == 9 ); |
| 1272 |
assert((funa16271!(delegate ref (ref a) => a)(x) += 1) == 10); |
| 1273 |
assert((funa16271!(delegate ref int(ref a) => a)(x) += 1) == 11); |
| 1274 |
assert(x == 11); |
| 1275 |
|
| 1276 |
alias aldc = ref () @trusted @nogc { return x; }; |
| 1277 |
auto audc = ref () @safe nothrow { return x; }; |
| 1278 |
alias alfuc = function ref (ref x) @trusted { return x; }; |
| 1279 |
alias aldec = delegate ref () @trusted { return x; }; |
| 1280 |
aldc() = 12; assert(x == 12); |
| 1281 |
audc() = 13; assert(x == 13); |
| 1282 |
alfuc(x) = 14; assert(x == 14); |
| 1283 |
aldec() = 15; assert(x == 15); |
| 1284 |
|
| 1285 |
template T() |
| 1286 |
{ |
| 1287 |
int x; |
| 1288 |
alias alf = ref () => x; |
| 1289 |
auto auf = ref () => x; |
| 1290 |
} |
| 1291 |
T!().alf() = 1; assert(T!().x == 1); |
| 1292 |
T!().auf() = 2; assert(T!().x == 2); |
| 1293 |
} |
| 1294 |
|
| 1295 |
// https://issues.dlang.org/show_bug.cgi?id=24525 |
| 1296 |
void test24525() |
| 1297 |
{ |
| 1298 |
int a; |
| 1299 |
auto ref () {return a;}() = 1; |
| 1300 |
assert(a == 1); |
| 1301 |
|
| 1302 |
ref () {return a;}() = 2; |
| 1303 |
assert(a == 2); |
| 1304 |
} |
| 1305 |
|
| 1306 |
/***************************************************/ |
| 1307 |
|
| 1308 |
int main() |
| 1309 |
{ |
| 1310 |
test1(); |
| 1311 |
test2(); |
| 1312 |
test3(); |
| 1313 |
test4(); |
| 1314 |
test4v(); |
| 1315 |
test5(); |
| 1316 |
test6(); |
| 1317 |
test7(); |
| 1318 |
test8(); |
| 1319 |
test9(); |
| 1320 |
test10(); |
| 1321 |
test11(); |
| 1322 |
test3235(); |
| 1323 |
test6714(); |
| 1324 |
test7202(); |
| 1325 |
test7288(); |
| 1326 |
test7499(); |
| 1327 |
test7500(); |
| 1328 |
test7525(); |
| 1329 |
test7582(); |
| 1330 |
test7649(); |
| 1331 |
test7650(); |
| 1332 |
test7705(); |
| 1333 |
test7713(); |
| 1334 |
test7743(); |
| 1335 |
test7761(); |
| 1336 |
test7941(); |
| 1337 |
test8005(); |
| 1338 |
test8198(); |
| 1339 |
test8226(); |
| 1340 |
test8241(); |
| 1341 |
test8242(); |
| 1342 |
test8315(); |
| 1343 |
test8397(); |
| 1344 |
test8496(); |
| 1345 |
test8575(); |
| 1346 |
test9153(); |
| 1347 |
test9393(); |
| 1348 |
test9415(); |
| 1349 |
test9628(); |
| 1350 |
test9928(); |
| 1351 |
test10133(); |
| 1352 |
test10219(); |
| 1353 |
test10288(); |
| 1354 |
test10336(); |
| 1355 |
test10928(); |
| 1356 |
test11661(); |
| 1357 |
test11774(); |
| 1358 |
test12421(); |
| 1359 |
test12508(); |
| 1360 |
test13879(); |
| 1361 |
test14745(); |
| 1362 |
test15794(); |
| 1363 |
test16271(); |
| 1364 |
test24525(); |
| 1365 |
|
| 1366 |
printf("Success\n"); |
| 1367 |
return 0; |
| 1368 |
} |