| 1 |
/* |
| 2 |
EXTRA_FILES: imports/test15777a.d imports/test15777b.d |
| 3 |
TEST_OUTPUT: |
| 4 |
--- |
| 5 |
int |
| 6 |
double |
| 7 |
foobar7406(T) |
| 8 |
test7406() |
| 9 |
int |
| 10 |
foobar7406(T) |
| 11 |
int |
| 12 |
test7406() |
| 13 |
--- |
| 14 |
*/ |
| 15 |
|
| 16 |
extern(C) int printf(const char* fmt, ...); |
| 17 |
|
| 18 |
alias AliasSeq(X...) = X; |
| 19 |
|
| 20 |
/***************************************/ |
| 21 |
|
| 22 |
void test1() |
| 23 |
{ |
| 24 |
char[] a; |
| 25 |
|
| 26 |
int foo() |
| 27 |
{ |
| 28 |
printf("foo\n"); |
| 29 |
a ~= "foo"; |
| 30 |
return 10; |
| 31 |
} |
| 32 |
|
| 33 |
foreach (i; 0 .. foo()) |
| 34 |
{ |
| 35 |
printf("%d\n", i); |
| 36 |
a ~= cast(char)('0' + i); |
| 37 |
} |
| 38 |
assert(a == "foo0123456789"); |
| 39 |
|
| 40 |
foreach_reverse (i; 0 .. foo()) |
| 41 |
{ |
| 42 |
printf("%d\n", i); |
| 43 |
a ~= cast(char)('0' + i); |
| 44 |
} |
| 45 |
assert(a == "foo0123456789foo9876543210"); |
| 46 |
} |
| 47 |
|
| 48 |
/***************************************/ |
| 49 |
// https://issues.dlang.org/show_bug.cgi?id=2411 |
| 50 |
|
| 51 |
struct S2411 |
| 52 |
{ |
| 53 |
int n; |
| 54 |
string s; |
| 55 |
} |
| 56 |
|
| 57 |
void test2411() |
| 58 |
{ |
| 59 |
S2411 s; |
| 60 |
assert(s.n == 0); |
| 61 |
assert(s.s == ""); |
| 62 |
foreach (i, ref e; s.tupleof) |
| 63 |
{ |
| 64 |
static if (i == 0) |
| 65 |
e = 10; |
| 66 |
static if (i == 1) |
| 67 |
e = "str"; |
| 68 |
} |
| 69 |
assert(s.n == 10); |
| 70 |
assert(s.s == "str"); |
| 71 |
} |
| 72 |
|
| 73 |
/***************************************/ |
| 74 |
// https://issues.dlang.org/show_bug.cgi?id=2442 |
| 75 |
|
| 76 |
template canForeach(T, E) |
| 77 |
{ |
| 78 |
enum canForeach = __traits(compiles, |
| 79 |
{ |
| 80 |
foreach(a; new T) |
| 81 |
{ |
| 82 |
static assert(is(typeof(a) == E)); |
| 83 |
} |
| 84 |
}); |
| 85 |
} |
| 86 |
|
| 87 |
void test2442() |
| 88 |
{ |
| 89 |
struct S1 |
| 90 |
{ |
| 91 |
int opApply(int delegate(ref const(int) v) dg) const { return 0; } |
| 92 |
int opApply(int delegate(ref int v) dg) { return 0; } |
| 93 |
} |
| 94 |
S1 ms1; |
| 95 |
const S1 cs1; |
| 96 |
foreach (x; ms1) { static assert(is(typeof(x) == int)); } |
| 97 |
foreach (x; cs1) { static assert(is(typeof(x) == const int)); } |
| 98 |
|
| 99 |
struct S2 |
| 100 |
{ |
| 101 |
int opApply(int delegate(ref int v) dg) { return 0; } |
| 102 |
int opApply(int delegate(ref long v) dg) { return 0; } |
| 103 |
} |
| 104 |
S2 ms2; |
| 105 |
static assert(!__traits(compiles, { foreach ( x; ms2) {} })); // ambiguous |
| 106 |
static assert( __traits(compiles, { foreach (int x; ms2) {} })); |
| 107 |
|
| 108 |
struct S3 |
| 109 |
{ |
| 110 |
int opApply(int delegate(ref int v) dg) const { return 0; } |
| 111 |
int opApply(int delegate(ref int v) dg) shared const { return 0; } |
| 112 |
} |
| 113 |
immutable S3 ms3; |
| 114 |
static assert(!__traits(compiles, { foreach (int x; ms3) {} })); // ambiguous |
| 115 |
|
| 116 |
// from https://github.com/dlang/dmd/pull/120 |
| 117 |
static class C |
| 118 |
{ |
| 119 |
int opApply(int delegate(ref int v) dg) { return 0; } |
| 120 |
int opApply(int delegate(ref const int v) dg) const { return 0; } |
| 121 |
int opApply(int delegate(ref immutable int v) dg) immutable { return 0; } |
| 122 |
int opApply(int delegate(ref shared int v) dg) shared { return 0; } |
| 123 |
int opApply(int delegate(ref shared const int v) dg) shared const { return 0; } |
| 124 |
} |
| 125 |
static class D |
| 126 |
{ |
| 127 |
int opApply(int delegate(ref int v) dg) const { return 0; } |
| 128 |
} |
| 129 |
static class E |
| 130 |
{ |
| 131 |
int opApply(int delegate(ref int v) dg) shared const { return 0; } |
| 132 |
} |
| 133 |
|
| 134 |
static assert( canForeach!( C , int )); |
| 135 |
static assert( canForeach!( const(C) , const(int) )); |
| 136 |
static assert( canForeach!( immutable(C) , immutable(int) )); |
| 137 |
static assert( canForeach!( shared(C) , shared(int) )); |
| 138 |
static assert( canForeach!(shared(const(C)), shared(const(int)))); |
| 139 |
|
| 140 |
static assert( canForeach!( D , int)); |
| 141 |
static assert( canForeach!( const(D) , int)); |
| 142 |
static assert( canForeach!( immutable(D) , int)); |
| 143 |
static assert(!canForeach!( shared(D) , int)); |
| 144 |
static assert(!canForeach!(shared(const(D)), int)); |
| 145 |
|
| 146 |
static assert(!canForeach!( E , int)); |
| 147 |
static assert(!canForeach!( const(E) , int)); |
| 148 |
static assert( canForeach!( immutable(E) , int)); |
| 149 |
static assert( canForeach!( shared(E) , int)); |
| 150 |
static assert( canForeach!(shared(const(E)), int)); |
| 151 |
} |
| 152 |
|
| 153 |
/***************************************/ |
| 154 |
// https://issues.dlang.org/show_bug.cgi?id=2443 |
| 155 |
|
| 156 |
struct S2443 |
| 157 |
{ |
| 158 |
int[] arr; |
| 159 |
int opApply(int delegate(size_t i, ref int v) dg) |
| 160 |
{ |
| 161 |
int result = 0; |
| 162 |
foreach (i, ref x; arr) |
| 163 |
{ |
| 164 |
if ((result = dg(i, x)) != 0) |
| 165 |
break; |
| 166 |
} |
| 167 |
return result; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
void test2443() |
| 172 |
{ |
| 173 |
S2443 s; |
| 174 |
foreach (i, ref v; s) {} |
| 175 |
foreach (i, v; s) {} |
| 176 |
static assert(!__traits(compiles, { foreach (ref i, ref v; s) {} })); |
| 177 |
static assert(!__traits(compiles, { foreach (ref i, v; s) {} })); |
| 178 |
} |
| 179 |
|
| 180 |
/***************************************/ |
| 181 |
// https://issues.dlang.org/show_bug.cgi?id=3187 |
| 182 |
|
| 183 |
class Collection |
| 184 |
{ |
| 185 |
int opApply(int delegate(ref Object) a) |
| 186 |
{ |
| 187 |
return 0; |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
Object testForeach(Collection level1, Collection level2) |
| 192 |
{ |
| 193 |
foreach (first; level1) { |
| 194 |
foreach (second; level2) |
| 195 |
return second; |
| 196 |
} |
| 197 |
return null; |
| 198 |
} |
| 199 |
|
| 200 |
void test3187() |
| 201 |
{ |
| 202 |
testForeach(new Collection, new Collection); |
| 203 |
} |
| 204 |
|
| 205 |
/***************************************/ |
| 206 |
// https://issues.dlang.org/show_bug.cgi?id=4090 |
| 207 |
|
| 208 |
void test4090a() |
| 209 |
{ |
| 210 |
double[10] arr = 1; |
| 211 |
double tot = 0; |
| 212 |
|
| 213 |
static assert(!__traits(compiles, { |
| 214 |
foreach (immutable ref x; arr) {} |
| 215 |
})); |
| 216 |
foreach (const ref x; arr) |
| 217 |
{ |
| 218 |
static assert(is(typeof(x) == const double)); |
| 219 |
tot += x; |
| 220 |
} |
| 221 |
foreach (immutable x; arr) |
| 222 |
{ |
| 223 |
static assert(is(typeof(x) == immutable double)); |
| 224 |
tot += x; |
| 225 |
} |
| 226 |
assert(tot == 1*10 + 1*10); |
| 227 |
} |
| 228 |
|
| 229 |
void test4090b() |
| 230 |
{ |
| 231 |
int tot = 0; |
| 232 |
|
| 233 |
static assert(!__traits(compiles, { |
| 234 |
foreach (immutable ref x; 1..11) {} |
| 235 |
})); |
| 236 |
foreach (const ref x; 1..11) |
| 237 |
{ |
| 238 |
static assert(is(typeof(x) == const int)); |
| 239 |
tot += x; |
| 240 |
} |
| 241 |
foreach (immutable x; 1..11) |
| 242 |
{ |
| 243 |
static assert(is(typeof(x) == immutable int)); |
| 244 |
tot += x; |
| 245 |
} |
| 246 |
assert(tot == 55 + 55); |
| 247 |
} |
| 248 |
|
| 249 |
/***************************************/ |
| 250 |
// https://issues.dlang.org/show_bug.cgi?id=5605 |
| 251 |
|
| 252 |
struct MyRange |
| 253 |
{ |
| 254 |
int theOnlyOne; |
| 255 |
|
| 256 |
@property bool empty() const |
| 257 |
{ |
| 258 |
return true; |
| 259 |
} |
| 260 |
|
| 261 |
@property ref int front() return |
| 262 |
{ |
| 263 |
return theOnlyOne; |
| 264 |
} |
| 265 |
|
| 266 |
void popFront() |
| 267 |
{} |
| 268 |
} |
| 269 |
|
| 270 |
struct MyCollection |
| 271 |
{ |
| 272 |
MyRange opSlice() const |
| 273 |
{ |
| 274 |
return MyRange(); |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void test5605() |
| 279 |
{ |
| 280 |
auto coll = MyCollection(); |
| 281 |
|
| 282 |
foreach (i; coll) { // <-- compilation error |
| 283 |
// ... |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
/***************************************/ |
| 288 |
// https://issues.dlang.org/show_bug.cgi?id=7004 |
| 289 |
|
| 290 |
void func7004(A...)(A args) |
| 291 |
{ |
| 292 |
foreach (i, e; args){} // OK |
| 293 |
foreach (uint i, e; args){} // OK |
| 294 |
foreach (size_t i, e; args){} // NG |
| 295 |
} |
| 296 |
void test7004() |
| 297 |
{ |
| 298 |
func7004(1, 3.14); |
| 299 |
} |
| 300 |
|
| 301 |
/***************************************/ |
| 302 |
// https://issues.dlang.org/show_bug.cgi?id=7406 |
| 303 |
|
| 304 |
template TypeTuple7406(T...) |
| 305 |
{ |
| 306 |
alias T TypeTuple7406; |
| 307 |
} |
| 308 |
|
| 309 |
template foobar7406(T) |
| 310 |
{ |
| 311 |
enum foobar = 2; |
| 312 |
} |
| 313 |
|
| 314 |
void test7406() |
| 315 |
{ |
| 316 |
foreach (sym; TypeTuple7406!(int, double)) // OK |
| 317 |
pragma(msg, sym.stringof); |
| 318 |
|
| 319 |
foreach (sym; TypeTuple7406!(foobar7406)) // OK |
| 320 |
pragma(msg, sym.stringof); |
| 321 |
|
| 322 |
foreach (sym; TypeTuple7406!(test7406)) // OK |
| 323 |
pragma(msg, sym.stringof); |
| 324 |
|
| 325 |
foreach (sym; TypeTuple7406!(int, foobar7406)) // Error: type int has no value |
| 326 |
pragma(msg, sym.stringof); |
| 327 |
|
| 328 |
foreach (sym; TypeTuple7406!(int, test7406)) // Error: type int has no value |
| 329 |
pragma(msg, sym.stringof); |
| 330 |
} |
| 331 |
|
| 332 |
/***************************************/ |
| 333 |
// https://issues.dlang.org/show_bug.cgi?id=6659 |
| 334 |
|
| 335 |
void test6659() |
| 336 |
{ |
| 337 |
static struct Iter |
| 338 |
{ |
| 339 |
~this() |
| 340 |
{ |
| 341 |
++_dtor; |
| 342 |
} |
| 343 |
|
| 344 |
bool opCmp(ref const Iter rhs) { return _pos == rhs._pos; } |
| 345 |
void opUnary(string op:"++")() { ++_pos; } |
| 346 |
size_t _pos; |
| 347 |
|
| 348 |
static size_t _dtor; |
| 349 |
} |
| 350 |
|
| 351 |
foreach (ref iter; Iter(0) .. Iter(10)) |
| 352 |
{ |
| 353 |
assert(Iter._dtor == 0); |
| 354 |
} |
| 355 |
assert(Iter._dtor == 2); |
| 356 |
|
| 357 |
Iter._dtor = 0; // reset |
| 358 |
|
| 359 |
for (auto iter = Iter(0), limit = Iter(10); iter != limit; ++iter) |
| 360 |
{ |
| 361 |
assert(Iter._dtor == 0); |
| 362 |
} |
| 363 |
assert(Iter._dtor == 2); |
| 364 |
} |
| 365 |
|
| 366 |
void test6659a() |
| 367 |
{ |
| 368 |
auto value = 0; |
| 369 |
try |
| 370 |
{ |
| 371 |
for ({scope(success) { assert(value == 1); value = 2;} } true; ) |
| 372 |
{ |
| 373 |
value = 1; |
| 374 |
break; |
| 375 |
} |
| 376 |
assert(value == 2); |
| 377 |
} |
| 378 |
catch (Exception e) |
| 379 |
{ |
| 380 |
assert(0); |
| 381 |
} |
| 382 |
assert(value == 2); |
| 383 |
} |
| 384 |
|
| 385 |
void test6659b() |
| 386 |
{ |
| 387 |
auto value = 0; |
| 388 |
try |
| 389 |
{ |
| 390 |
for ({scope(failure) value = 1;} true; ) |
| 391 |
{ |
| 392 |
throw new Exception(""); |
| 393 |
} |
| 394 |
assert(0); |
| 395 |
} |
| 396 |
catch (Exception e) |
| 397 |
{ |
| 398 |
assert(e); |
| 399 |
} |
| 400 |
assert(value == 1); |
| 401 |
} |
| 402 |
|
| 403 |
void test6659c() |
| 404 |
{ |
| 405 |
auto value = 0; |
| 406 |
try |
| 407 |
{ |
| 408 |
for ({scope(exit) value = 1;} true; ) |
| 409 |
{ |
| 410 |
throw new Exception(""); |
| 411 |
} |
| 412 |
assert(0); |
| 413 |
} |
| 414 |
catch (Exception e) |
| 415 |
{ |
| 416 |
assert(e); |
| 417 |
} |
| 418 |
assert(value == 1); |
| 419 |
} |
| 420 |
|
| 421 |
/***************************************/ |
| 422 |
|
| 423 |
// https://issues.dlang.org/show_bug.cgi?id=10221 |
| 424 |
|
| 425 |
void test10221() |
| 426 |
{ |
| 427 |
// All of these should work, but most are too slow. Just check they compile. |
| 428 |
foreach(char i; char.min..char.max+1) {} |
| 429 |
if (0) foreach(wchar i; wchar.min..wchar.max+1) {} |
| 430 |
if (0) foreach(dchar i; dchar.min..dchar.max+1) {} |
| 431 |
foreach(byte i; byte.min..byte.max+1) {} |
| 432 |
foreach(ubyte i; ubyte.min..ubyte.max+1) {} |
| 433 |
if (0) foreach(short i; short.min..short.max+1) {} |
| 434 |
if (0) foreach(ushort i; ushort.min..ushort.max+1) {} |
| 435 |
if (0) foreach(int i; int.min..int.max+1U) {} |
| 436 |
if (0) foreach(uint i; uint.min..uint.max+1L) {} |
| 437 |
if (0) foreach(long i; long.min..long.max+1UL) {} |
| 438 |
|
| 439 |
foreach_reverse(char i; char.min..char.max+1) { assert(i == typeof(i).max); break; } |
| 440 |
foreach_reverse(wchar i; wchar.min..wchar.max+1) { assert(i == typeof(i).max); break; } |
| 441 |
foreach_reverse(dchar i; dchar.min..dchar.max+1) { assert(i == typeof(i).max); break; } |
| 442 |
foreach_reverse(byte i; byte.min..byte.max+1) { assert(i == typeof(i).max); break; } |
| 443 |
foreach_reverse(ubyte i; ubyte.min..ubyte.max+1) { assert(i == typeof(i).max); break; } |
| 444 |
foreach_reverse(short i; short.min..short.max+1) { assert(i == typeof(i).max); break; } |
| 445 |
foreach_reverse(ushort i; ushort.min..ushort.max+1) { assert(i == typeof(i).max); break; } |
| 446 |
foreach_reverse(int i; int.min..int.max+1U) { assert(i == typeof(i).max); break; } |
| 447 |
foreach_reverse(uint i; uint.min..uint.max+1L) { assert(i == typeof(i).max); break; } |
| 448 |
foreach_reverse(long i; long.min..long.max+1UL) { assert(i == typeof(i).max); break; } |
| 449 |
} |
| 450 |
|
| 451 |
/***************************************/ |
| 452 |
// https://issues.dlang.org/show_bug.cgi?id=7814 |
| 453 |
|
| 454 |
struct File7814 |
| 455 |
{ |
| 456 |
~this(){} |
| 457 |
} |
| 458 |
|
| 459 |
struct ByLine7814 |
| 460 |
{ |
| 461 |
File7814 file; |
| 462 |
|
| 463 |
// foreach interface |
| 464 |
@property bool empty() const { return true; } |
| 465 |
@property char[] front() { return null; } |
| 466 |
void popFront(){} |
| 467 |
} |
| 468 |
|
| 469 |
void test7814() |
| 470 |
{ |
| 471 |
int dummy; |
| 472 |
ByLine7814 f; |
| 473 |
foreach (l; f) { |
| 474 |
scope(failure) // 'failure' or 'success' fails, but 'exit' works |
| 475 |
dummy = -1; |
| 476 |
dummy = 0; |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
/***************************************/ |
| 481 |
// https://issues.dlang.org/show_bug.cgi?id=10049 |
| 482 |
|
| 483 |
struct ByLine10049 |
| 484 |
{ |
| 485 |
bool empty() { return true; } |
| 486 |
string front() { return null; } |
| 487 |
void popFront() {} |
| 488 |
|
| 489 |
~this() {} // necessary |
| 490 |
} |
| 491 |
|
| 492 |
void test10049() |
| 493 |
{ |
| 494 |
ByLine10049 r; |
| 495 |
foreach (line; r) |
| 496 |
{ |
| 497 |
doNext: |
| 498 |
{} |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
/******************************************/ |
| 503 |
|
| 504 |
struct T11955(T...) { T field; alias field this; } |
| 505 |
|
| 506 |
alias X11955 = uint; |
| 507 |
|
| 508 |
struct S11955 |
| 509 |
{ |
| 510 |
enum empty = false; |
| 511 |
T11955!(uint, uint) front; |
| 512 |
void popFront() {} |
| 513 |
} |
| 514 |
|
| 515 |
void test11955() |
| 516 |
{ |
| 517 |
foreach(X11955 i, v; S11955()) {} |
| 518 |
} |
| 519 |
|
| 520 |
/******************************************/ |
| 521 |
// https://issues.dlang.org/show_bug.cgi?id=6652 |
| 522 |
|
| 523 |
void test6652() |
| 524 |
{ |
| 525 |
size_t sum; |
| 526 |
foreach (i; 0 .. 10) |
| 527 |
sum += i++; // 0123456789 |
| 528 |
assert(sum == 45); |
| 529 |
|
| 530 |
sum = 0; |
| 531 |
foreach (ref i; 0 .. 10) |
| 532 |
sum += i++; // 02468 |
| 533 |
assert(sum == 20); |
| 534 |
|
| 535 |
sum = 0; |
| 536 |
foreach_reverse (i; 0 .. 10) |
| 537 |
sum += i--; // 9876543210 |
| 538 |
assert(sum == 45); |
| 539 |
|
| 540 |
sum = 0; |
| 541 |
foreach_reverse (ref i; 0 .. 10) |
| 542 |
sum += i--; // 97531 |
| 543 |
assert(sum == 25); |
| 544 |
|
| 545 |
enum ary = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]; |
| 546 |
sum = 0; |
| 547 |
foreach (i, v; ary) |
| 548 |
{ |
| 549 |
assert(i == v); |
| 550 |
sum += i++; // 0123456789 |
| 551 |
} |
| 552 |
assert(sum == 45); |
| 553 |
|
| 554 |
sum = 0; |
| 555 |
foreach (ref i, v; ary) |
| 556 |
{ |
| 557 |
assert(i == v); |
| 558 |
sum += i++; // 02468 |
| 559 |
} |
| 560 |
assert(sum == 20); |
| 561 |
|
| 562 |
sum = 0; |
| 563 |
foreach_reverse (i, v; ary) |
| 564 |
{ |
| 565 |
assert(i == v); |
| 566 |
sum += i--; // 9876543210 |
| 567 |
} |
| 568 |
assert(sum == 45); |
| 569 |
|
| 570 |
sum = 0; |
| 571 |
foreach_reverse (ref i, v; ary) |
| 572 |
{ |
| 573 |
assert(i == v); |
| 574 |
sum += i--; // 97531 |
| 575 |
} |
| 576 |
assert(sum == 25); |
| 577 |
|
| 578 |
static struct Iter |
| 579 |
{ |
| 580 |
~this() |
| 581 |
{ |
| 582 |
++_dtorCount; |
| 583 |
} |
| 584 |
|
| 585 |
bool opCmp(ref const Iter rhs) |
| 586 |
{ |
| 587 |
return _pos == rhs._pos; |
| 588 |
} |
| 589 |
|
| 590 |
void opUnary(string op)() if(op == "++" || op == "--") |
| 591 |
{ |
| 592 |
mixin(op ~ q{_pos;}); |
| 593 |
} |
| 594 |
|
| 595 |
size_t _pos; |
| 596 |
static size_t _dtorCount; |
| 597 |
} |
| 598 |
|
| 599 |
Iter._dtorCount = sum = 0; |
| 600 |
foreach (v; Iter(0) .. Iter(10)) |
| 601 |
sum += v._pos++; // 0123456789 |
| 602 |
assert(sum == 45 && Iter._dtorCount == 12); |
| 603 |
|
| 604 |
Iter._dtorCount = sum = 0; |
| 605 |
foreach (ref v; Iter(0) .. Iter(10)) |
| 606 |
sum += v._pos++; // 02468 |
| 607 |
assert(sum == 20 && Iter._dtorCount == 2); |
| 608 |
|
| 609 |
// additional dtor calls due to unnecessary postdecrements |
| 610 |
Iter._dtorCount = sum = 0; |
| 611 |
foreach_reverse (v; Iter(0) .. Iter(10)) |
| 612 |
sum += v._pos--; // 9876543210 |
| 613 |
assert(sum == 45 && Iter._dtorCount >= 12); |
| 614 |
|
| 615 |
Iter._dtorCount = sum = 0; |
| 616 |
foreach_reverse (ref v; Iter(0) .. Iter(10)) |
| 617 |
sum += v._pos--; // 97531 |
| 618 |
assert(sum == 25 && Iter._dtorCount >= 2); |
| 619 |
} |
| 620 |
|
| 621 |
/***************************************/ |
| 622 |
// https://issues.dlang.org/show_bug.cgi?id=8595 |
| 623 |
|
| 624 |
struct OpApply8595 |
| 625 |
{ |
| 626 |
int opApply(int delegate(ref int) dg) |
| 627 |
{ |
| 628 |
assert(0); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
string test8595() |
| 633 |
{ |
| 634 |
foreach (elem; OpApply8595.init) |
| 635 |
{ |
| 636 |
static assert(is(typeof(return) == string)); |
| 637 |
} |
| 638 |
assert(0); |
| 639 |
} |
| 640 |
|
| 641 |
/***************************************/ |
| 642 |
// https://issues.dlang.org/show_bug.cgi?id=9068 |
| 643 |
|
| 644 |
struct Foo9068 |
| 645 |
{ |
| 646 |
static int[] destroyed; |
| 647 |
int x; |
| 648 |
~this() { destroyed ~= x; } |
| 649 |
} |
| 650 |
|
| 651 |
struct SimpleCounter9068 |
| 652 |
{ |
| 653 |
static int destroyedCount; |
| 654 |
enum int limit = 5; |
| 655 |
int counter; |
| 656 |
~this() { destroyedCount++; } |
| 657 |
|
| 658 |
// Range primitives. |
| 659 |
@property bool empty() const { return counter >= limit; } |
| 660 |
@property int front() { return counter; } |
| 661 |
void popFront() { counter++; } |
| 662 |
} |
| 663 |
|
| 664 |
void test9068() |
| 665 |
{ |
| 666 |
//---------------------------------------- |
| 667 |
// There was never a bug in this case (no range). |
| 668 |
int sum; |
| 669 |
loop_simple: |
| 670 |
foreach (i; [10, 20]) |
| 671 |
{ |
| 672 |
sum += i; |
| 673 |
break loop_simple; |
| 674 |
} |
| 675 |
assert(sum == 10); |
| 676 |
|
| 677 |
//---------------------------------------- |
| 678 |
// There was a bug with loops over ranges. |
| 679 |
int last = -1; |
| 680 |
X: foreach (i; SimpleCounter9068()) |
| 681 |
{ |
| 682 |
switch(i) |
| 683 |
{ |
| 684 |
case 3: |
| 685 |
break X; |
| 686 |
default: |
| 687 |
last = i; |
| 688 |
} |
| 689 |
} |
| 690 |
assert(last == 2); |
| 691 |
assert(SimpleCounter9068.destroyedCount == 1); |
| 692 |
|
| 693 |
//---------------------------------------- |
| 694 |
// Simpler case: the compiler error had nothing to do with the switch. |
| 695 |
last = -1; |
| 696 |
loop_with_range: |
| 697 |
foreach (i; SimpleCounter9068()) |
| 698 |
{ |
| 699 |
last = i; |
| 700 |
break loop_with_range; |
| 701 |
} |
| 702 |
assert(last == 0); |
| 703 |
assert(SimpleCounter9068.destroyedCount == 2); |
| 704 |
|
| 705 |
//---------------------------------------- |
| 706 |
// Test with destructors: the loop is implicitly wrapped into two |
| 707 |
// try/finally clauses. |
| 708 |
loop_with_dtors: |
| 709 |
for (auto x = Foo9068(4), y = Foo9068(5); x.x != 10; ++x.x) |
| 710 |
{ |
| 711 |
if (x.x == 8) |
| 712 |
break loop_with_dtors; |
| 713 |
} |
| 714 |
assert(Foo9068.destroyed == [5, 8]); |
| 715 |
Foo9068.destroyed = null; |
| 716 |
|
| 717 |
//---------------------------------------- |
| 718 |
// Same with an unlabelled break. |
| 719 |
for (auto x = Foo9068(4), y = Foo9068(5); x.x != 10; ++x.x) |
| 720 |
{ |
| 721 |
if (x.x == 7) |
| 722 |
break; |
| 723 |
} |
| 724 |
assert(Foo9068.destroyed == [5, 7]); |
| 725 |
Foo9068.destroyed = null; |
| 726 |
} |
| 727 |
|
| 728 |
/***************************************/ |
| 729 |
// https://issues.dlang.org/show_bug.cgi?id=11885 |
| 730 |
|
| 731 |
struct Foo11885 |
| 732 |
{ |
| 733 |
static int[] destroyed; |
| 734 |
int x; |
| 735 |
~this() { destroyed ~= x; } |
| 736 |
} |
| 737 |
|
| 738 |
struct SimpleCounter11885 |
| 739 |
{ |
| 740 |
static int destroyedCount; |
| 741 |
enum int limit = 5; |
| 742 |
int counter; |
| 743 |
~this() { destroyedCount++; } |
| 744 |
|
| 745 |
// Range primitives. |
| 746 |
@property bool empty() const { return counter >= limit; } |
| 747 |
@property int front() { return counter; } |
| 748 |
void popFront() { counter++; } |
| 749 |
} |
| 750 |
|
| 751 |
void test11885() |
| 752 |
{ |
| 753 |
//---------------------------------------- |
| 754 |
// There was never a bug in this case (no range). |
| 755 |
int sum; |
| 756 |
loop_simple: |
| 757 |
foreach (i; [10, 20]) |
| 758 |
{ |
| 759 |
sum += i; |
| 760 |
continue loop_simple; |
| 761 |
} |
| 762 |
assert(sum == 30); |
| 763 |
|
| 764 |
//---------------------------------------- |
| 765 |
// There was a bug with loops over ranges. |
| 766 |
int last = -1; |
| 767 |
X: foreach (i; SimpleCounter11885()) |
| 768 |
{ |
| 769 |
switch(i) |
| 770 |
{ |
| 771 |
case 3: |
| 772 |
continue X; |
| 773 |
default: |
| 774 |
last = i; |
| 775 |
} |
| 776 |
} |
| 777 |
assert(last == 4); |
| 778 |
assert(SimpleCounter11885.destroyedCount == 1); |
| 779 |
|
| 780 |
//---------------------------------------- |
| 781 |
// Simpler case: the compiler error had nothing to do with the switch. |
| 782 |
last = -1; |
| 783 |
loop_with_range: |
| 784 |
foreach (i; SimpleCounter11885()) |
| 785 |
{ |
| 786 |
last = i; |
| 787 |
continue loop_with_range; |
| 788 |
} |
| 789 |
assert(last == 4); |
| 790 |
assert(SimpleCounter11885.destroyedCount == 2); |
| 791 |
|
| 792 |
//---------------------------------------- |
| 793 |
// Test with destructors: the loop is implicitly wrapped into two |
| 794 |
// try/finally clauses. |
| 795 |
loop_with_dtors: |
| 796 |
for (auto x = Foo11885(4), y = Foo11885(5); x.x != 10; ++x.x) |
| 797 |
{ |
| 798 |
if (x.x == 8) |
| 799 |
continue loop_with_dtors; |
| 800 |
} |
| 801 |
assert(Foo11885.destroyed == [5, 10]); |
| 802 |
Foo11885.destroyed = null; |
| 803 |
|
| 804 |
//---------------------------------------- |
| 805 |
// Same with an unlabelled continue. |
| 806 |
for (auto x = Foo11885(4), y = Foo11885(5); x.x != 10; ++x.x) |
| 807 |
{ |
| 808 |
if (x.x == 7) |
| 809 |
continue; |
| 810 |
} |
| 811 |
assert(Foo11885.destroyed == [5, 10]); |
| 812 |
Foo11885.destroyed = null; |
| 813 |
} |
| 814 |
|
| 815 |
/***************************************/ |
| 816 |
// https://issues.dlang.org/show_bug.cgi?id=10475 |
| 817 |
|
| 818 |
void test10475a() |
| 819 |
{ |
| 820 |
struct DirIterator |
| 821 |
{ |
| 822 |
int _store = 42; |
| 823 |
~this() { assert(0); } |
| 824 |
} |
| 825 |
|
| 826 |
DirIterator dirEntries() |
| 827 |
{ |
| 828 |
throw new Exception(""); |
| 829 |
} |
| 830 |
|
| 831 |
try |
| 832 |
{ |
| 833 |
for (DirIterator c = dirEntries(); true; ) {} |
| 834 |
assert(0); |
| 835 |
} |
| 836 |
catch (Exception e) |
| 837 |
{ |
| 838 |
assert(e.next is null); |
| 839 |
} |
| 840 |
} |
| 841 |
|
| 842 |
void test10475b() |
| 843 |
{ |
| 844 |
uint g; |
| 845 |
struct S |
| 846 |
{ |
| 847 |
uint flag; |
| 848 |
~this() { g |= flag; } |
| 849 |
} |
| 850 |
|
| 851 |
S thrown() |
| 852 |
{ |
| 853 |
throw new Exception(""); |
| 854 |
} |
| 855 |
|
| 856 |
g = 0x0; |
| 857 |
try |
| 858 |
{ |
| 859 |
for (auto x = S(0x1), y = S(0x2), z = thrown(); true; ) {} |
| 860 |
assert(0); |
| 861 |
} |
| 862 |
catch (Exception e) |
| 863 |
{ |
| 864 |
assert(e.next is null); |
| 865 |
} |
| 866 |
assert(g == 0x3); |
| 867 |
|
| 868 |
g = 0x0; |
| 869 |
try |
| 870 |
{ |
| 871 |
for (auto x = S(0x1), y = thrown(), z = S(0x2); true; ) {} |
| 872 |
assert(0); |
| 873 |
} |
| 874 |
catch (Exception e) |
| 875 |
{ |
| 876 |
assert(e.next is null); |
| 877 |
} |
| 878 |
assert(g == 0x1); |
| 879 |
|
| 880 |
g = 0x0; |
| 881 |
try |
| 882 |
{ |
| 883 |
for (auto x = thrown(), y = S(0x1), z = S(0x2); true; ) {} |
| 884 |
assert(0); |
| 885 |
} |
| 886 |
catch (Exception e) |
| 887 |
{ |
| 888 |
assert(e.next is null); |
| 889 |
} |
| 890 |
assert(g == 0x0); |
| 891 |
} |
| 892 |
|
| 893 |
/***************************************/ |
| 894 |
// https://issues.dlang.org/show_bug.cgi?id=11291 |
| 895 |
|
| 896 |
void test11291() |
| 897 |
{ |
| 898 |
struct Tuple(T...) |
| 899 |
{ |
| 900 |
T field; |
| 901 |
alias field this; |
| 902 |
} |
| 903 |
struct zip |
| 904 |
{ |
| 905 |
string[] s1, s2; |
| 906 |
|
| 907 |
bool empty() { return true; } |
| 908 |
auto front() { return Tuple!(string, string)(s1[0], s2[0]); } |
| 909 |
void popFront() {} |
| 910 |
} |
| 911 |
|
| 912 |
foreach (const a, const b; zip(["foo"], ["bar"])) |
| 913 |
{ |
| 914 |
static assert(is(typeof(a) == const string)); |
| 915 |
static assert(is(typeof(b) == const string)); |
| 916 |
|
| 917 |
static assert(!__traits(compiles, a = "something")); |
| 918 |
static assert(!__traits(compiles, b = "something")); |
| 919 |
} |
| 920 |
} |
| 921 |
|
| 922 |
/***************************************/ |
| 923 |
// https://issues.dlang.org/show_bug.cgi?id=12103 |
| 924 |
|
| 925 |
alias TypeTuple12103(TL...) = TL; |
| 926 |
|
| 927 |
alias Id12103(alias a) = a; |
| 928 |
|
| 929 |
void test12103() |
| 930 |
{ |
| 931 |
alias fs1 = TypeTuple12103!(() => 0, () => 1); |
| 932 |
foreach (i, f; fs1) |
| 933 |
{ |
| 934 |
static assert(f() == i); |
| 935 |
static assert(Id12103!f() == i); |
| 936 |
assert(f() == i); |
| 937 |
assert(Id12103!f() == i); |
| 938 |
} |
| 939 |
|
| 940 |
alias fs2 = TypeTuple12103!(x=>x+0, y=>y+1); |
| 941 |
foreach (i, f; fs2) |
| 942 |
{ |
| 943 |
static assert(f(0) == i); |
| 944 |
static assert(Id12103!f(0) == i); |
| 945 |
assert(f(0) == i); |
| 946 |
assert(Id12103!f(0) == i); |
| 947 |
} |
| 948 |
} |
| 949 |
|
| 950 |
/***************************************/ |
| 951 |
// https://issues.dlang.org/show_bug.cgi?id=12739 |
| 952 |
|
| 953 |
struct S12739 |
| 954 |
{ |
| 955 |
nothrow: |
| 956 |
int opApply(int delegate(ref int) nothrow dg) |
| 957 |
{ |
| 958 |
return 0; |
| 959 |
} |
| 960 |
} |
| 961 |
|
| 962 |
void test12739() nothrow |
| 963 |
{ |
| 964 |
S12739 s; |
| 965 |
foreach (e; s) {} |
| 966 |
} |
| 967 |
|
| 968 |
/***************************************/ |
| 969 |
// https://issues.dlang.org/show_bug.cgi?id=12932 |
| 970 |
|
| 971 |
void test12932() @nogc |
| 972 |
{ |
| 973 |
int sum; |
| 974 |
foreach (e; [1,2,3]) |
| 975 |
{ |
| 976 |
sum += e; |
| 977 |
} |
| 978 |
assert(sum == 6); |
| 979 |
} |
| 980 |
|
| 981 |
/***************************************/ |
| 982 |
// https://issues.dlang.org/show_bug.cgi?id=13756 |
| 983 |
|
| 984 |
void test13756() |
| 985 |
{ |
| 986 |
printf("test13756()\n"); |
| 987 |
int[int] org = [1:2], aa; |
| 988 |
|
| 989 |
aa = org.dup; |
| 990 |
foreach (v; aa) |
| 991 |
{ |
| 992 |
static assert(is(typeof(v) == int)); |
| 993 |
v = 20; |
| 994 |
} |
| 995 |
assert(aa == [1:2]); |
| 996 |
|
| 997 |
aa = org.dup; |
| 998 |
foreach (ref v; aa) |
| 999 |
{ |
| 1000 |
static assert(is(typeof(v) == int)); |
| 1001 |
v = 20; |
| 1002 |
} |
| 1003 |
assert(aa == [1:20]); |
| 1004 |
|
| 1005 |
aa = org.dup; |
| 1006 |
foreach (k, v; aa) |
| 1007 |
{ |
| 1008 |
static assert(is(typeof(k) == int)); |
| 1009 |
static assert(is(typeof(v) == int)); |
| 1010 |
k = 10; |
| 1011 |
v = 20; |
| 1012 |
} |
| 1013 |
assert(aa == [1:2]); |
| 1014 |
|
| 1015 |
aa = org.dup; |
| 1016 |
foreach (k, ref v; aa) |
| 1017 |
{ |
| 1018 |
static assert(is(typeof(k) == int)); |
| 1019 |
static assert(is(typeof(v) == int)); |
| 1020 |
k = 10; |
| 1021 |
v = 20; |
| 1022 |
} |
| 1023 |
assert(aa == [1:20]); |
| 1024 |
|
| 1025 |
aa = org.dup; |
| 1026 |
foreach (ref k, v; aa) // NG -> OK |
| 1027 |
{ |
| 1028 |
static assert(is(typeof(k) == const int)); |
| 1029 |
static assert(is(typeof(v) == int)); |
| 1030 |
static assert(!__traits(compiles, k = 10)); |
| 1031 |
v = 20; |
| 1032 |
} |
| 1033 |
assert(aa == [1:2]); |
| 1034 |
|
| 1035 |
aa = org.dup; |
| 1036 |
foreach (ref k, ref v; aa) // NG -> OK |
| 1037 |
{ |
| 1038 |
static assert(is(typeof(k) == const int)); |
| 1039 |
static assert(is(typeof(v) == int)); |
| 1040 |
static assert(!__traits(compiles, k = 10)); |
| 1041 |
v = 20; |
| 1042 |
} |
| 1043 |
assert(aa == [1:20]); |
| 1044 |
|
| 1045 |
foreach (ref const k, v; aa) // NG -> OK, same with 'ref k' |
| 1046 |
{ |
| 1047 |
static assert(is(typeof(k) == const int)); |
| 1048 |
} |
| 1049 |
} |
| 1050 |
|
| 1051 |
/***************************************/ |
| 1052 |
// https://issues.dlang.org/show_bug.cgi?id=14653 |
| 1053 |
|
| 1054 |
static string result14653; |
| 1055 |
|
| 1056 |
class RangeClass14653 |
| 1057 |
{ |
| 1058 |
int a; |
| 1059 |
|
| 1060 |
this(T)(T...) { result14653 ~= "c"; } |
| 1061 |
~this() { result14653 ~= "d"; a = -1; } |
| 1062 |
|
| 1063 |
@property bool empty() { result14653 ~= "e"; return a >= 2; } |
| 1064 |
@property int front() { result14653 ~= "f"; assert(a >= 0); return a; } |
| 1065 |
void popFront() { result14653 ~= "p"; ++a; } |
| 1066 |
} |
| 1067 |
|
| 1068 |
auto scoped14653(T, A...)(A args) |
| 1069 |
{ |
| 1070 |
static struct Scoped(T) |
| 1071 |
{ |
| 1072 |
void[__traits(classInstanceSize, T)] store; |
| 1073 |
T payload() { return cast(T)cast(void*)store.ptr; } |
| 1074 |
alias payload this; |
| 1075 |
|
| 1076 |
~this() |
| 1077 |
{ |
| 1078 |
//.destroy(payload); |
| 1079 |
payload.__dtor(); |
| 1080 |
(cast(byte[])store)[] = 0; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
Scoped!T result = void; |
| 1085 |
|
| 1086 |
//emplace!T(result.store[], args); |
| 1087 |
result.store[] = typeid(T).initializer[]; |
| 1088 |
result.payload.__ctor(args); |
| 1089 |
|
| 1090 |
return result; |
| 1091 |
} |
| 1092 |
|
| 1093 |
void test14653() |
| 1094 |
{ |
| 1095 |
printf("test14653()\n"); |
| 1096 |
foreach (e; scoped14653!RangeClass14653(1)) |
| 1097 |
{ |
| 1098 |
result14653 ~= "b"; |
| 1099 |
} |
| 1100 |
assert(result14653 == "cefbpefbped", result14653); |
| 1101 |
} |
| 1102 |
|
| 1103 |
/***************************************/ |
| 1104 |
// https://issues.dlang.org/show_bug.cgi?id=15777 |
| 1105 |
|
| 1106 |
template funA15777() |
| 1107 |
{ |
| 1108 |
import imports.test15777a; |
| 1109 |
alias funA15777 = fun; |
| 1110 |
} |
| 1111 |
|
| 1112 |
template funB15777() |
| 1113 |
{ |
| 1114 |
import imports.test15777b; |
| 1115 |
alias funB15777 = fun; |
| 1116 |
} |
| 1117 |
|
| 1118 |
template funAB15777() |
| 1119 |
{ |
| 1120 |
import imports.test15777a; |
| 1121 |
import imports.test15777b; |
| 1122 |
alias funAB15777 = fun; |
| 1123 |
} |
| 1124 |
|
| 1125 |
void foo15777(alias tpl)() |
| 1126 |
{ |
| 1127 |
alias seq = AliasSeq!(tpl!()); |
| 1128 |
// Make alias of 'overload set' in tuple elements |
| 1129 |
static assert(seq.length == 1); |
| 1130 |
foreach (i, n; seq) |
| 1131 |
{ |
| 1132 |
static assert(__traits(identifier, seq[i]) == "fun"); |
| 1133 |
} |
| 1134 |
} |
| 1135 |
|
| 1136 |
void test15777() |
| 1137 |
{ |
| 1138 |
foo15777!funA15777; |
| 1139 |
foo15777!funB15777; |
| 1140 |
foo15777!funAB15777; |
| 1141 |
} |
| 1142 |
|
| 1143 |
/***************************************/ |
| 1144 |
// https://issues.dlang.org/show_bug.cgi?id=17041 |
| 1145 |
|
| 1146 |
auto ref int[2] foo17041(A...)(auto ref A args) |
| 1147 |
{ |
| 1148 |
foreach(a; args) |
| 1149 |
{ |
| 1150 |
a = [12, 22]; |
| 1151 |
} |
| 1152 |
foreach(ref a; args) |
| 1153 |
{ |
| 1154 |
a = [31, 41]; |
| 1155 |
return args[0]; |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
void test17041() |
| 1160 |
{ |
| 1161 |
int[2] x = [10, 20]; |
| 1162 |
foreach(a; AliasSeq!(x)) |
| 1163 |
{ |
| 1164 |
a = [11, 21]; |
| 1165 |
} |
| 1166 |
assert(x == [10, 20]); // test by value |
| 1167 |
foreach(ref a; AliasSeq!(x)) |
| 1168 |
{ |
| 1169 |
a = [30, 40]; |
| 1170 |
} |
| 1171 |
assert(x == [30, 40]); // test by ref value |
| 1172 |
|
| 1173 |
assert(foo17041(x) == [31, 41]); // test lvalue |
| 1174 |
assert(x == [31, 41]); |
| 1175 |
assert(foo17041(cast(int[2]) [10, 20]) == [31, 41]); // test rvalue |
| 1176 |
} |
| 1177 |
|
| 1178 |
/***************************************/ |
| 1179 |
|
| 1180 |
int main() |
| 1181 |
{ |
| 1182 |
test1(); |
| 1183 |
test2411(); |
| 1184 |
test2442(); |
| 1185 |
test2443(); |
| 1186 |
test3187(); |
| 1187 |
test4090a(); |
| 1188 |
test4090b(); |
| 1189 |
test5605(); |
| 1190 |
test7004(); |
| 1191 |
test10221(); |
| 1192 |
test7406(); |
| 1193 |
test6659(); |
| 1194 |
test6659a(); |
| 1195 |
test6659b(); |
| 1196 |
test6659c(); |
| 1197 |
test7814(); |
| 1198 |
test6652(); |
| 1199 |
test9068(); |
| 1200 |
test11885(); |
| 1201 |
test10475a(); |
| 1202 |
test10475b(); |
| 1203 |
test11291(); |
| 1204 |
test12103(); |
| 1205 |
test12739(); |
| 1206 |
test12932(); |
| 1207 |
test13756(); |
| 1208 |
test14653(); |
| 1209 |
test17041(); |
| 1210 |
|
| 1211 |
printf("Success\n"); |
| 1212 |
return 0; |
| 1213 |
} |