| 1 |
|
| 2 |
/***************************************************/ |
| 3 |
// https://issues.dlang.org/show_bug.cgi?id=6265 |
| 4 |
|
| 5 |
pure nothrow @safe int h6265() { |
| 6 |
return 1; |
| 7 |
} |
| 8 |
int f6265a(alias g)() { |
| 9 |
return g(); |
| 10 |
} |
| 11 |
pure nothrow @safe int i6265a() { |
| 12 |
return f6265a!h6265(); |
| 13 |
} |
| 14 |
|
| 15 |
int f6265b()() { |
| 16 |
return h6265(); |
| 17 |
} |
| 18 |
pure nothrow @safe int i6265b() { |
| 19 |
return f6265b(); |
| 20 |
} |
| 21 |
|
| 22 |
pure nothrow @safe int i6265c() { |
| 23 |
return { |
| 24 |
return h6265(); |
| 25 |
}(); |
| 26 |
} |
| 27 |
|
| 28 |
/***************************************************/ |
| 29 |
// Make sure a function is not infered as pure if it isn't. |
| 30 |
|
| 31 |
int fNPa() { |
| 32 |
return 1; |
| 33 |
} |
| 34 |
int gNPa()() { |
| 35 |
return fNPa(); |
| 36 |
} |
| 37 |
static assert( __traits(compiles, function int () { return gNPa(); })); |
| 38 |
static assert(!__traits(compiles, function int () pure { return gNPa(); })); |
| 39 |
static assert(!__traits(compiles, function int () nothrow { return gNPa(); })); |
| 40 |
static assert(!__traits(compiles, function int () @safe { return gNPa(); })); |
| 41 |
|
| 42 |
/***************************************************/ |
| 43 |
// Need to ensure the comment in Expression::checkPurity is not violated. |
| 44 |
|
| 45 |
void fECPa() { |
| 46 |
void g()() { |
| 47 |
void h() { |
| 48 |
} |
| 49 |
h(); |
| 50 |
} |
| 51 |
static assert( is(typeof(&g!()) == void delegate() pure nothrow @nogc @safe)); |
| 52 |
static assert(!is(typeof(&g!()) == void delegate())); |
| 53 |
} |
| 54 |
|
| 55 |
void fECPb() { |
| 56 |
void g()() { |
| 57 |
void h() { |
| 58 |
} |
| 59 |
fECPb(); |
| 60 |
} |
| 61 |
static assert(!is(typeof(&g!()) == void delegate() pure)); |
| 62 |
static assert( is(typeof(&g!()) == void delegate())); |
| 63 |
} |
| 64 |
|
| 65 |
/***************************************************/ |
| 66 |
// https://issues.dlang.org/show_bug.cgi?id=5635 |
| 67 |
|
| 68 |
pure bool foo5635(R = int)(string x) |
| 69 |
{ |
| 70 |
bool result = false; |
| 71 |
foreach (dchar d; x) |
| 72 |
result = true; |
| 73 |
return result; |
| 74 |
} |
| 75 |
|
| 76 |
void test5635() |
| 77 |
{ |
| 78 |
foo5635("hi"); |
| 79 |
} |
| 80 |
|
| 81 |
/***************************************************/ |
| 82 |
// https://issues.dlang.org/show_bug.cgi?id=5936 |
| 83 |
|
| 84 |
auto bug5936c(R)(R i) @safe pure nothrow { |
| 85 |
return true; |
| 86 |
} |
| 87 |
static assert( bug5936c(0) ); |
| 88 |
|
| 89 |
/***************************************************/ |
| 90 |
// https://issues.dlang.org/show_bug.cgi?id=6351 |
| 91 |
|
| 92 |
void bug6351(alias dg)() |
| 93 |
{ |
| 94 |
dg(); |
| 95 |
} |
| 96 |
|
| 97 |
void test6351() |
| 98 |
{ |
| 99 |
void delegate(int[] a...) deleg6351 = (int[] a...){}; |
| 100 |
alias bug6351!(deleg6351) baz6531; |
| 101 |
} |
| 102 |
|
| 103 |
/***************************************************/ |
| 104 |
// https://issues.dlang.org/show_bug.cgi?id=6359 |
| 105 |
|
| 106 |
void impure6359() nothrow @safe @nogc {} |
| 107 |
void throwable6359() pure @safe @nogc {} |
| 108 |
void system6359() pure nothrow @nogc {} |
| 109 |
void gcable6359() pure nothrow @safe {} |
| 110 |
|
| 111 |
int global6359; |
| 112 |
|
| 113 |
void f6359() pure nothrow @safe @nogc |
| 114 |
{ |
| 115 |
static assert(!__traits(compiles, impure6359())); |
| 116 |
static assert(!__traits(compiles, throwable6359())); |
| 117 |
static assert(!__traits(compiles, system6359())); |
| 118 |
static assert(!__traits(compiles, gcable6359())); |
| 119 |
static assert(!__traits(compiles, global6359++)); |
| 120 |
|
| 121 |
static assert(!__traits(compiles, { impure6359(); }())); |
| 122 |
static assert(!__traits(compiles, { throwable6359(); }())); |
| 123 |
static assert(!__traits(compiles, { system6359(); }())); |
| 124 |
static assert(!__traits(compiles, { gcable6359(); }())); |
| 125 |
static assert(!__traits(compiles, { global6359++; }())); |
| 126 |
} |
| 127 |
|
| 128 |
void g6359()() pure nothrow @safe @nogc |
| 129 |
{ |
| 130 |
static assert(!__traits(compiles, impure6359())); |
| 131 |
static assert(!__traits(compiles, throwable6359())); |
| 132 |
static assert(!__traits(compiles, system6359())); |
| 133 |
static assert(!__traits(compiles, gcable6359())); |
| 134 |
static assert(!__traits(compiles, global6359++)); |
| 135 |
|
| 136 |
static assert(!__traits(compiles, { impure6359(); }())); |
| 137 |
static assert(!__traits(compiles, { throwable6359(); }())); |
| 138 |
static assert(!__traits(compiles, { system6359(); }())); |
| 139 |
static assert(!__traits(compiles, { gcable6359(); }())); |
| 140 |
static assert(!__traits(compiles, { global6359++; }())); |
| 141 |
} |
| 142 |
|
| 143 |
// attribute inference is not affected by the expressions inside __traits(compiles) |
| 144 |
void h6359()() |
| 145 |
{ |
| 146 |
static assert( __traits(compiles, impure6359())); |
| 147 |
static assert( __traits(compiles, throwable6359())); |
| 148 |
static assert( __traits(compiles, system6359())); |
| 149 |
static assert( __traits(compiles, gcable6359())); |
| 150 |
static assert( __traits(compiles, global6359++)); |
| 151 |
|
| 152 |
static assert( __traits(compiles, { impure6359(); }())); |
| 153 |
static assert( __traits(compiles, { throwable6359(); }())); |
| 154 |
static assert( __traits(compiles, { system6359(); }())); |
| 155 |
static assert( __traits(compiles, { gcable6359(); }())); |
| 156 |
static assert( __traits(compiles, { global6359++; }())); |
| 157 |
} |
| 158 |
|
| 159 |
void test6359() pure nothrow @safe @nogc |
| 160 |
{ |
| 161 |
f6359(); |
| 162 |
g6359(); |
| 163 |
h6359(); |
| 164 |
} |
| 165 |
|
| 166 |
/***************************************************/ |
| 167 |
// https://issues.dlang.org/show_bug.cgi?id=7017 |
| 168 |
|
| 169 |
template map7017(fun...) if (fun.length >= 1) |
| 170 |
{ |
| 171 |
auto map7017() |
| 172 |
{ |
| 173 |
struct Result { |
| 174 |
this(int dummy){} // impure member function -> inferred to pure by fixing https://issues.dlang.org/show_bug.cgi?id=10329 |
| 175 |
} |
| 176 |
return Result(0); // impure call -> inferred to pure by fixing https://issues.dlang.org/show_bug.cgi?id=10329 |
| 177 |
} |
| 178 |
} |
| 179 |
|
| 180 |
int foo7017(immutable int x) pure nothrow { return 1; } |
| 181 |
|
| 182 |
void test7017a() pure |
| 183 |
{ |
| 184 |
int bar7017(immutable int x) pure nothrow { return 1; } |
| 185 |
|
| 186 |
static assert(__traits(compiles, map7017!((){})())); |
| 187 |
static assert(__traits(compiles, map7017!q{ 1 }())); |
| 188 |
static assert(__traits(compiles, map7017!foo7017())); |
| 189 |
static assert(__traits(compiles, map7017!bar7017())); |
| 190 |
} |
| 191 |
|
| 192 |
/***************************************************/ |
| 193 |
// https://issues.dlang.org/show_bug.cgi?id=7017 (little simpler cases) |
| 194 |
|
| 195 |
auto map7017a(alias fun)() { return fun(); } // depends on purity of fun |
| 196 |
auto map7017b(alias fun)() { return; } // always pure |
| 197 |
auto map7017c(alias fun)() { return yyy7017(); } // always impure |
| 198 |
|
| 199 |
int xxx7017() pure { return 1; } |
| 200 |
int yyy7017() { return 1; } |
| 201 |
|
| 202 |
void test7017b() pure |
| 203 |
{ |
| 204 |
static assert( __traits(compiles, map7017a!xxx7017() )); |
| 205 |
static assert(!__traits(compiles, map7017a!yyy7017() )); |
| 206 |
|
| 207 |
static assert( __traits(compiles, map7017b!xxx7017() )); |
| 208 |
static assert( __traits(compiles, map7017b!yyy7017() )); |
| 209 |
|
| 210 |
static assert(!__traits(compiles, map7017c!xxx7017() )); |
| 211 |
static assert(!__traits(compiles, map7017c!yyy7017() )); |
| 212 |
} |
| 213 |
|
| 214 |
/***************************************************/ |
| 215 |
// Test case from std.process |
| 216 |
|
| 217 |
auto escapeArgumentImpl(alias allocator)() |
| 218 |
{ |
| 219 |
return allocator(); |
| 220 |
} |
| 221 |
|
| 222 |
auto escapeShellArgument(alias allocator)() |
| 223 |
{ |
| 224 |
return escapeArgumentImpl!allocator(); |
| 225 |
} |
| 226 |
|
| 227 |
pure string escapeShellArguments() |
| 228 |
{ |
| 229 |
char[] allocator() |
| 230 |
{ |
| 231 |
return new char[1]; |
| 232 |
} |
| 233 |
|
| 234 |
/* Both escape!allocator and escapeImpl!allocator are impure, |
| 235 |
* but they are nested template function that instantiated here. |
| 236 |
* Then calling them from here doesn't break purity. |
| 237 |
*/ |
| 238 |
return escapeShellArgument!allocator(); |
| 239 |
} |
| 240 |
|
| 241 |
/***************************************************/ |
| 242 |
// https://issues.dlang.org/show_bug.cgi?id=8234 |
| 243 |
|
| 244 |
void test8234() |
| 245 |
{ |
| 246 |
immutable int x = 0; |
| 247 |
|
| 248 |
alias FP = typeof({ enum e = x; return e; }); |
| 249 |
static assert(is(FP : int function())); |
| 250 |
|
| 251 |
auto fp = { enum e = x; return e; }; |
| 252 |
static assert(is(typeof(fp) : int function())); |
| 253 |
|
| 254 |
alias DG = typeof({ auto e = x; return e; }); |
| 255 |
static assert(is(DG : int delegate())); |
| 256 |
|
| 257 |
auto dg = { auto e = x; return e; }; |
| 258 |
static assert(is(typeof(dg) : int delegate())); |
| 259 |
} |
| 260 |
|
| 261 |
/***************************************************/ |
| 262 |
// https://issues.dlang.org/show_bug.cgi?id=8504 |
| 263 |
|
| 264 |
import core.demangle : demangle; |
| 265 |
|
| 266 |
void foo8504()() |
| 267 |
{ |
| 268 |
static assert(typeof(foo8504!()).stringof == "void()"); |
| 269 |
static assert(typeof(foo8504!()).mangleof == "FZv"); |
| 270 |
// static assert(demangle(foo8504!().mangleof) == "void testInference.foo8504!().foo8504()"); |
| 271 |
} |
| 272 |
|
| 273 |
auto toDelegate8504a(F)(auto ref F fp) { return fp; } |
| 274 |
F toDelegate8504b(F)(auto ref F fp) { return fp; } |
| 275 |
|
| 276 |
extern(C) void testC8504() {} |
| 277 |
|
| 278 |
void test8504() |
| 279 |
{ |
| 280 |
static assert(typeof(foo8504!()).stringof == "pure nothrow @nogc @safe void()"); |
| 281 |
static assert(typeof(foo8504!()).mangleof == "FNaNbNiNfZv"); |
| 282 |
static assert(demangle(foo8504!().mangleof) == "pure nothrow @nogc @safe void testInference.foo8504!().foo8504()"); |
| 283 |
|
| 284 |
auto fp1 = toDelegate8504a(&testC8504); |
| 285 |
auto fp2 = toDelegate8504b(&testC8504); |
| 286 |
static assert(is(typeof(fp1) == typeof(fp2))); |
| 287 |
static assert(typeof(fp1).stringof == "extern (C) void function()"); |
| 288 |
static assert(typeof(fp2).stringof == "extern (C) void function()"); |
| 289 |
static assert(typeof(fp1).mangleof == "PUZv"); |
| 290 |
static assert(typeof(fp2).mangleof == "PUZv"); |
| 291 |
} |
| 292 |
|
| 293 |
/***************************************************/ |
| 294 |
// https://issues.dlang.org/show_bug.cgi?id=8751 |
| 295 |
|
| 296 |
alias bool delegate(in int) pure Bar8751; |
| 297 |
Bar8751 foo8751a(immutable int x) pure |
| 298 |
{ |
| 299 |
return y => x > y; // OK |
| 300 |
} |
| 301 |
Bar8751 foo8751b(const int x) pure |
| 302 |
{ |
| 303 |
return y => x > y; // error -> OK |
| 304 |
} |
| 305 |
|
| 306 |
/***************************************************/ |
| 307 |
// https://issues.dlang.org/show_bug.cgi?id=8793 |
| 308 |
|
| 309 |
alias bool delegate(in int) pure Dg8793; |
| 310 |
alias bool function(in int) pure Fp8793; |
| 311 |
|
| 312 |
Dg8793 foo8793fp1(immutable Fp8793 f) pure { return x => (*f)(x); } // OK |
| 313 |
Dg8793 foo8793fp2( const Fp8793 f) pure { return x => (*f)(x); } // OK |
| 314 |
|
| 315 |
Dg8793 foo8793dg1(immutable Dg8793 f) pure { return x => f(x); } // OK |
| 316 |
Dg8793 foo8793dg2( const Dg8793 f) pure { return x => f(x); } // OK <- error |
| 317 |
|
| 318 |
Dg8793 foo8793pfp1(immutable Fp8793* f) pure { return x => (*f)(x); } // OK |
| 319 |
Dg8793 foo8793pdg1(immutable Dg8793* f) pure { return x => (*f)(x); } // OK |
| 320 |
|
| 321 |
Dg8793 foo8793pfp2(const Fp8793* f) pure { return x => (*f)(x); } // OK <- error |
| 322 |
Dg8793 foo8793pdg2(const Dg8793* f) pure { return x => (*f)(x); } // OK <- error |
| 323 |
|
| 324 |
// general case for the hasPointer type |
| 325 |
Dg8793 foo8793ptr1(immutable int* p) pure { return x => *p == x; } // OK |
| 326 |
|
| 327 |
Dg8793 foo8793ptr2(const int* p) pure { return x => *p == x; } // OK <- error |
| 328 |
|
| 329 |
/***************************************************/ |
| 330 |
// https://issues.dlang.org/show_bug.cgi?id=9072 |
| 331 |
|
| 332 |
struct A9072(T) |
| 333 |
{ |
| 334 |
this(U)(U x) {} |
| 335 |
~this() {} |
| 336 |
} |
| 337 |
void test9072() |
| 338 |
{ |
| 339 |
A9072!int a = A9072!short(); |
| 340 |
} |
| 341 |
|
| 342 |
/***************************************************/ |
| 343 |
// https://issues.dlang.org/show_bug.cgi?id=5933 |
| 344 |
// https://issues.dlang.org/show_bug.cgi?id=8504 |
| 345 |
// Template attribute inferrence doesn't work |
| 346 |
|
| 347 |
int foo5933()(int a) { return a*a; } |
| 348 |
struct S5933 |
| 349 |
{ |
| 350 |
double foo()(double a) { return a * a; } |
| 351 |
} |
| 352 |
// outside function |
| 353 |
static assert(typeof(foo5933!()).stringof == "pure nothrow @nogc @safe int(int a)"); |
| 354 |
static assert(typeof(S5933.init.foo!()).stringof == "pure nothrow @nogc @safe double(double a)"); |
| 355 |
|
| 356 |
void test5933() |
| 357 |
{ |
| 358 |
// inside function |
| 359 |
static assert(typeof(foo5933!()).stringof == "pure nothrow @nogc @safe int(int a)"); |
| 360 |
static assert(typeof(S5933.init.foo!()).stringof == "pure nothrow @nogc @safe double(double a)"); |
| 361 |
} |
| 362 |
|
| 363 |
/***************************************************/ |
| 364 |
// https://issues.dlang.org/show_bug.cgi?id=9148 |
| 365 |
|
| 366 |
void test9148a() pure |
| 367 |
{ |
| 368 |
static int g; |
| 369 |
int x; |
| 370 |
|
| 371 |
void foo1() /+pure+/ |
| 372 |
{ |
| 373 |
static assert(!__traits(compiles, g++)); |
| 374 |
x++; |
| 375 |
} |
| 376 |
void foo2() pure |
| 377 |
{ |
| 378 |
static assert(!__traits(compiles, g++)); |
| 379 |
x++; |
| 380 |
} |
| 381 |
foo1(); |
| 382 |
static assert(is(typeof(&foo1) == void delegate() pure nothrow @nogc @safe)); |
| 383 |
foo2(); |
| 384 |
static assert(is(typeof(&foo2) == void delegate() pure nothrow @nogc @safe)); |
| 385 |
|
| 386 |
void bar1() immutable /+pure+/ |
| 387 |
{ |
| 388 |
static assert(!__traits(compiles, g++)); |
| 389 |
static assert(!__traits(compiles, x++)); |
| 390 |
} |
| 391 |
void bar2() immutable pure |
| 392 |
{ |
| 393 |
static assert(!__traits(compiles, g++)); |
| 394 |
static assert(!__traits(compiles, x++)); |
| 395 |
} |
| 396 |
bar1(); |
| 397 |
static assert(is(typeof(&bar1) == void delegate() pure immutable nothrow @nogc @safe)); |
| 398 |
bar2(); |
| 399 |
static assert(is(typeof(&bar2) == void delegate() pure immutable nothrow @nogc @safe)); |
| 400 |
|
| 401 |
struct S |
| 402 |
{ |
| 403 |
void foo1() /+pure+/ |
| 404 |
{ |
| 405 |
static assert(!__traits(compiles, g++)); |
| 406 |
x++; |
| 407 |
} |
| 408 |
void foo2() pure |
| 409 |
{ |
| 410 |
static assert(!__traits(compiles, g++)); |
| 411 |
x++; |
| 412 |
} |
| 413 |
void bar1() immutable /+pure+/ |
| 414 |
{ |
| 415 |
static assert(!__traits(compiles, g++)); |
| 416 |
static assert(!__traits(compiles, x++)); |
| 417 |
} |
| 418 |
void bar2() immutable pure |
| 419 |
{ |
| 420 |
static assert(!__traits(compiles, g++)); |
| 421 |
static assert(!__traits(compiles, x++)); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
S sm; |
| 426 |
sm.foo1(); |
| 427 |
static assert(is(typeof(&sm.foo1) == void delegate() pure)); |
| 428 |
sm.foo2(); |
| 429 |
static assert(is(typeof(&sm.foo2) == void delegate() pure)); |
| 430 |
|
| 431 |
immutable S si; |
| 432 |
si.bar1(); |
| 433 |
static assert(is(typeof(&si.bar1) == void delegate() pure immutable)); |
| 434 |
si.bar2(); |
| 435 |
static assert(is(typeof(&si.bar2) == void delegate() pure immutable)); |
| 436 |
} |
| 437 |
|
| 438 |
// ---- |
| 439 |
// inheritance of pure and @safe |
| 440 |
|
| 441 |
void test9148b() pure nothrow @nogc @safe |
| 442 |
{ |
| 443 |
void nf() {} |
| 444 |
static assert(is(typeof(&nf) == void delegate() pure nothrow @nogc @safe)); |
| 445 |
|
| 446 |
struct NS |
| 447 |
{ |
| 448 |
void mf() {} |
| 449 |
static void sf() {} |
| 450 |
} |
| 451 |
NS ns; |
| 452 |
static assert(is(typeof(&ns.mf) == void delegate() pure nothrow @nogc @safe)); |
| 453 |
static assert(is(typeof(&NS.sf) == void function() pure nothrow @nogc @safe)); |
| 454 |
|
| 455 |
static void sf() {} |
| 456 |
static assert(is(typeof(&sf) == void function() pure nothrow @nogc @safe)); |
| 457 |
|
| 458 |
static struct SS |
| 459 |
{ |
| 460 |
void mf() {} |
| 461 |
static void sf() {} |
| 462 |
} |
| 463 |
SS ss; |
| 464 |
static assert(is(typeof(&ss.mf) == void delegate() pure nothrow @nogc @safe)); |
| 465 |
static assert(is(typeof(&SS.sf) == void function() pure nothrow @nogc @safe)); |
| 466 |
} |
| 467 |
|
| 468 |
void impureSystem9148b() {} |
| 469 |
void func9148b()() |
| 470 |
{ |
| 471 |
void bar() // do not inherit PUREfwdref |
| 472 |
{ |
| 473 |
static assert(is(typeof(&bar) == void delegate())); |
| 474 |
impureSystem9148b(); |
| 475 |
} |
| 476 |
static assert(is(typeof(&bar) == void delegate())); |
| 477 |
} |
| 478 |
static assert(is(typeof(&func9148b!()) == void function() pure nothrow @nogc @safe)); |
| 479 |
|
| 480 |
// ---- |
| 481 |
// from fail_compilation/fail283.d |
| 482 |
|
| 483 |
pure int double_sqr9148c(int x) |
| 484 |
{ |
| 485 |
int y = x; |
| 486 |
void do_sqr() pure { y *= y; } |
| 487 |
do_sqr(); |
| 488 |
return y; |
| 489 |
} |
| 490 |
|
| 491 |
void test9148c() |
| 492 |
{ |
| 493 |
assert(double_sqr9148c(10) == 100); |
| 494 |
} |
| 495 |
|
| 496 |
// ---- |
| 497 |
// from fail_compilation/fail348.d |
| 498 |
|
| 499 |
void test9148d() pure |
| 500 |
{ |
| 501 |
void g() // implicitly marked as 'pure' |
| 502 |
{ |
| 503 |
void h() pure |
| 504 |
{ |
| 505 |
// i() and j() are implicitly marked as 'pure' |
| 506 |
void i() { } |
| 507 |
void j() { i(); g(); } // can call i() and g() |
| 508 |
} |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
void test9148e() |
| 513 |
{ |
| 514 |
int x; |
| 515 |
static assert(is(typeof((int a){ return a + x; }) == int delegate(int) pure nothrow @nogc @safe)); |
| 516 |
|
| 517 |
auto dg = (int a){ return a + x; }; |
| 518 |
static assert(is(typeof(dg) == int delegate(int) pure nothrow @nogc @safe)); |
| 519 |
} |
| 520 |
|
| 521 |
/***************************************************/ |
| 522 |
// https://issues.dlang.org/show_bug.cgi?id=12912 |
| 523 |
|
| 524 |
struct S12912(alias fun) |
| 525 |
{ |
| 526 |
void f() { fun(); } |
| 527 |
} |
| 528 |
|
| 529 |
class C12912 |
| 530 |
{ |
| 531 |
int n; |
| 532 |
|
| 533 |
void f() pure |
| 534 |
{ |
| 535 |
S12912!(() => n) s; |
| 536 |
// Here lambda should be inferred to weak purity. |
| 537 |
|
| 538 |
s.f(); |
| 539 |
// And this call will be a pure member function call. |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
/***************************************************/ |
| 544 |
// https://issues.dlang.org/show_bug.cgi?id=10002 |
| 545 |
|
| 546 |
void impure10002() {} |
| 547 |
void remove10002(alias pred, bool impure = false, Range)(Range range) |
| 548 |
{ |
| 549 |
pred(range[0]); |
| 550 |
static if (impure) impure10002(); |
| 551 |
} |
| 552 |
class Node10002 |
| 553 |
{ |
| 554 |
Node10002 parent; |
| 555 |
Node10002[] children; |
| 556 |
|
| 557 |
void foo() pure |
| 558 |
{ |
| 559 |
parent.children.remove10002!(n => n is parent)(); |
| 560 |
remove10002!(n => n is parent)(parent.children); |
| 561 |
static assert(!__traits(compiles, parent.children.remove10002x!(n => n is parent, true)())); |
| 562 |
static assert(!__traits(compiles, remove10002x!(n => n is parent, true)(parent.children))); |
| 563 |
|
| 564 |
Node10002 p; |
| 565 |
p.children.remove10002!(n => n is p)(); |
| 566 |
remove10002!(n => n is p)(p.children); |
| 567 |
static assert(!__traits(compiles, p.children.remove10002x!(n => n is p, true)())); |
| 568 |
static assert(!__traits(compiles, remove10002x!(n => n is p, true)(p.children))); |
| 569 |
} |
| 570 |
} |
| 571 |
|
| 572 |
/***************************************************/ |
| 573 |
// https://issues.dlang.org/show_bug.cgi?id=10148 |
| 574 |
|
| 575 |
void fa10148() {} // fa is @system |
| 576 |
|
| 577 |
auto fb10148(T)() |
| 578 |
{ |
| 579 |
struct A(S) |
| 580 |
{ |
| 581 |
// [4] Parent function fb is already inferred to @safe, then |
| 582 |
// fc is forcely marked @safe on default until 2.052. |
| 583 |
// But fc should keep attribute inference ability |
| 584 |
// by overriding the inherited @safe-ty from its parent. |
| 585 |
void fc(T2)() |
| 586 |
{ |
| 587 |
// [5] During semantic3 process, fc is not @safe on default. |
| 588 |
static assert(is(typeof(&fc) == void delegate())); |
| 589 |
fa10148(); |
| 590 |
} |
| 591 |
// [1] this is now inferred to @safe by implementing https://issues.dlang.org/show_bug.cgi?id=7511 |
| 592 |
this(S a) {} |
| 593 |
} |
| 594 |
|
| 595 |
// [2] A!int(0) is now calling @safe function, then fb!T also be inferred to @safe |
| 596 |
return A!int(0); |
| 597 |
} |
| 598 |
|
| 599 |
void test10148() |
| 600 |
{ |
| 601 |
fb10148!int.fc!int; // [0] instantiate fb |
| 602 |
// [3] instantiate fc |
| 603 |
|
| 604 |
// [6] After semantic3 done, fc!int is deduced to @system. |
| 605 |
static assert(is(typeof(&fb10148!int.fc!int) == void delegate() @system)); |
| 606 |
} |
| 607 |
|
| 608 |
/***************************************************/ |
| 609 |
// https://issues.dlang.org/show_bug.cgi?id=10289 |
| 610 |
|
| 611 |
void test10289() |
| 612 |
{ |
| 613 |
void foo(E)() |
| 614 |
{ |
| 615 |
throw new E(""); |
| 616 |
} |
| 617 |
void bar(E1, E2)() |
| 618 |
{ |
| 619 |
throw new E1(""); |
| 620 |
throw new E2(""); |
| 621 |
} |
| 622 |
void baz(E1, E2)(bool cond) |
| 623 |
{ |
| 624 |
if (cond) |
| 625 |
throw new E1(""); |
| 626 |
else |
| 627 |
throw new E2(""); |
| 628 |
} |
| 629 |
|
| 630 |
import core.exception; |
| 631 |
static class MyException : Exception |
| 632 |
{ |
| 633 |
this(string) @safe pure nothrow { super(""); } |
| 634 |
} |
| 635 |
|
| 636 |
static assert( __traits(compiles, () nothrow { foo!Error(); })); |
| 637 |
static assert( __traits(compiles, () nothrow { foo!AssertError(); })); |
| 638 |
|
| 639 |
static assert(!__traits(compiles, () nothrow { foo!Exception(); })); |
| 640 |
static assert(!__traits(compiles, () nothrow { foo!MyException(); })); |
| 641 |
|
| 642 |
static assert( __traits(compiles, () nothrow { bar!(Error, Exception)(); })); |
| 643 |
static assert(!__traits(compiles, () nothrow { bar!(Exception, Error)(); })); |
| 644 |
|
| 645 |
static assert(!__traits(compiles, () nothrow { baz!(Error, Exception)(); })); |
| 646 |
static assert(!__traits(compiles, () nothrow { baz!(Exception, Error)(); })); |
| 647 |
} |
| 648 |
|
| 649 |
/***************************************************/ |
| 650 |
// https://issues.dlang.org/show_bug.cgi?id=10296 |
| 651 |
|
| 652 |
void foo10296()() |
| 653 |
{ |
| 654 |
int[3] a; |
| 655 |
|
| 656 |
void bar()() { a[1] = 2; } |
| 657 |
bar(); |
| 658 |
static assert(typeof(bar!()).stringof == "pure nothrow @nogc @safe void()"); // nothrow @safe void() |
| 659 |
} |
| 660 |
pure void test10296() |
| 661 |
{ |
| 662 |
foo10296(); |
| 663 |
} |
| 664 |
|
| 665 |
/***************************************************/ |
| 666 |
// https://issues.dlang.org/show_bug.cgi?id=12025 |
| 667 |
|
| 668 |
struct Foo12025 |
| 669 |
{ |
| 670 |
int[5] bar; |
| 671 |
} |
| 672 |
|
| 673 |
void test12025a() pure |
| 674 |
{ |
| 675 |
enum n1 = typeof(Foo12025.bar).length; // OK |
| 676 |
enum n2 = Foo12025.bar .length; // OK <- error |
| 677 |
|
| 678 |
auto x1 = typeof(Foo12025.bar).length; // OK |
| 679 |
auto x2 = Foo12025.bar .length; // OK <- error |
| 680 |
} |
| 681 |
|
| 682 |
void test12025b() pure |
| 683 |
{ |
| 684 |
static int[5] bar; |
| 685 |
|
| 686 |
enum n1 = typeof(bar).length; // OK |
| 687 |
enum n2 = bar .length; // OK <- error |
| 688 |
|
| 689 |
auto x1 = typeof(bar).length; // OK |
| 690 |
auto x2 = bar .length; // OK <- error |
| 691 |
} |
| 692 |
|
| 693 |
/***************************************************/ |
| 694 |
// https://issues.dlang.org/show_bug.cgi?id=12542 |
| 695 |
|
| 696 |
int logOf12542(T)(T n) |
| 697 |
{ |
| 698 |
if (n) |
| 699 |
return 1 + logOf12542(n/2); |
| 700 |
return 0; |
| 701 |
} |
| 702 |
|
| 703 |
void test12542() @safe nothrow pure |
| 704 |
{ |
| 705 |
int log = logOf12542(9); |
| 706 |
} |
| 707 |
|
| 708 |
/***************************************************/ |
| 709 |
// https://issues.dlang.org/show_bug.cgi?id=12704 |
| 710 |
|
| 711 |
void foo12704() @system; |
| 712 |
alias FP12704 = typeof(function() { foo12704(); }); |
| 713 |
static assert(is(FP12704 == void function() @system)); |
| 714 |
|
| 715 |
/***************************************************/ |
| 716 |
// https://issues.dlang.org/show_bug.cgi?id=12970 |
| 717 |
|
| 718 |
@system { @safe void f12970a() {} } |
| 719 |
@system { void f12970b() @safe {} } |
| 720 |
static assert(is(typeof(&f12970a) == void function() @safe)); |
| 721 |
static assert(is(typeof(&f12970b) == void function() @safe)); |
| 722 |
|
| 723 |
@system { @trusted void f12970c() {} } |
| 724 |
@system { void f12970d() @trusted {} } |
| 725 |
static assert(is(typeof(&f12970c) == void function() @trusted)); |
| 726 |
static assert(is(typeof(&f12970d) == void function() @trusted)); |
| 727 |
|
| 728 |
@safe { @system void f12970e() {} } |
| 729 |
@safe { void f12970f() @system {} } |
| 730 |
static assert(is(typeof(&f12970e) == void function() @system)); |
| 731 |
static assert(is(typeof(&f12970f) == void function() @system)); |
| 732 |
|
| 733 |
@safe { @trusted void f12970g() {} } |
| 734 |
@safe { void f12970h() @trusted {} } |
| 735 |
static assert(is(typeof(&f12970g) == void function() @trusted)); |
| 736 |
static assert(is(typeof(&f12970h) == void function() @trusted)); |
| 737 |
|
| 738 |
@trusted { @safe void f12970i() {} } |
| 739 |
@trusted { void f12970j() @safe {} } |
| 740 |
static assert(is(typeof(&f12970i) == void function() @safe)); |
| 741 |
static assert(is(typeof(&f12970j) == void function() @safe)); |
| 742 |
|
| 743 |
@trusted { @system void f12970k() {} } |
| 744 |
@trusted { void f12970l() @system {} } |
| 745 |
static assert(is(typeof(&f12970k) == void function() @system)); |
| 746 |
static assert(is(typeof(&f12970l) == void function() @system)); |
| 747 |
|
| 748 |
/***************************************************/ |
| 749 |
// Parsing prefix STC_FUNCATTR for variable declaration |
| 750 |
|
| 751 |
__gshared immutable pure nothrow @property @nogc @safe void function() prefix_qualified_fp1; |
| 752 |
__gshared{immutable{pure{nothrow{@property{@nogc{@safe{void function() prefix_qualified_fp2;}}}}}}} |
| 753 |
static assert(typeof(prefix_qualified_fp1).stringof == typeof(prefix_qualified_fp2).stringof); |
| 754 |
static assert(typeof(prefix_qualified_fp1).stringof |
| 755 |
== "immutable(void function() pure nothrow @nogc @property @safe)"); |
| 756 |
|
| 757 |
const pure nothrow @property @nogc @safe void function()[] prefix_qualified_fp_array1; |
| 758 |
const{pure{nothrow{@property{@nogc{@safe{void function()[] prefix_qualified_fp_array2;}}}}}} |
| 759 |
static assert(typeof(prefix_qualified_fp_array1).stringof == typeof(prefix_qualified_fp_array2).stringof); |
| 760 |
static assert(typeof(prefix_qualified_fp_array1).stringof |
| 761 |
== "const(void function() pure nothrow @nogc @property @safe[])"); |
| 762 |
|
| 763 |
/***************************************************/ |
| 764 |
// Parsing prefix, intermediate, or postfix @safe for alias declaration |
| 765 |
|
| 766 |
@safe alias void function() AliasDecl_FP1; |
| 767 |
alias @safe void function() AliasDecl_FP2; // is not @safe |
| 768 |
alias void function() @safe AliasDecl_FP3; |
| 769 |
static assert(AliasDecl_FP1.stringof == "void function() @safe"); |
| 770 |
static assert(AliasDecl_FP2.stringof == "void function()"); |
| 771 |
static assert(AliasDecl_FP3.stringof == "void function() @safe"); |
| 772 |
|
| 773 |
/***************************************************/ |
| 774 |
// https://issues.dlang.org/show_bug.cgi?id=13217 |
| 775 |
|
| 776 |
void writeln13217(string) {} |
| 777 |
|
| 778 |
nothrow void a13217(T)(T x) |
| 779 |
{ |
| 780 |
try |
| 781 |
{ |
| 782 |
() { writeln13217("a"); } (); |
| 783 |
} |
| 784 |
catch (Exception e) {} |
| 785 |
} |
| 786 |
|
| 787 |
void test13217() |
| 788 |
{ |
| 789 |
a13217(1); |
| 790 |
} |
| 791 |
|
| 792 |
/***************************************************/ |
| 793 |
// https://issues.dlang.org/show_bug.cgi?id=13840 |
| 794 |
|
| 795 |
struct Foo13840 |
| 796 |
{ |
| 797 |
int opApply(int delegate(int)) |
| 798 |
{ |
| 799 |
return 0; |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
void func13840() |
| 804 |
{ |
| 805 |
} |
| 806 |
|
| 807 |
void test13840() nothrow |
| 808 |
{ |
| 809 |
try |
| 810 |
{ |
| 811 |
foreach (i; Foo13840()) // generated delegate is throwable |
| 812 |
{ |
| 813 |
func13840(); // throwable function call |
| 814 |
} |
| 815 |
} |
| 816 |
catch(Throwable) |
| 817 |
{} |
| 818 |
} |
| 819 |
|
| 820 |
/***************************************************/ |
| 821 |
// https://github.com/dlang/dmd/pull/20685 |
| 822 |
|
| 823 |
struct T1 |
| 824 |
{ |
| 825 |
int a; |
| 826 |
inout this(ref inout T1 t) @nogc nothrow pure { a = t.a; } |
| 827 |
} |
| 828 |
|
| 829 |
struct S1 |
| 830 |
{ |
| 831 |
T1 t; // generate copy constructor, infer @nogc nothrow pure |
| 832 |
} |
| 833 |
|
| 834 |
void test1() @nogc nothrow pure |
| 835 |
{ |
| 836 |
S1 s; |
| 837 |
S1 t = s; |
| 838 |
} |