Test case: -------------------------------------------------- struct Bug6052 { int a; } bool bug6052() { Bug6052[2] arr; for (int i = 0; i < 2; ++ i) { Bug6052 el = {i}; Bug6052 ek = el; arr[i] = el; el.a = i + 2; assert(ek.a == i); // ok assert(arr[i].a == i); // fail } assert(arr[1].a == 1); // ok assert(arr[0].a == 0); // fail return true; } static assert(bug6052()); -------------------------------------------------- x.d(16): Error: assert(arr[cast(uint)i].a == i) failed x.d(23): Error: cannot evaluate bug6052() at compile time x.d(23): Error: static assert (bug6052()) is not evaluatable at compile time -------------------------------------------------- Setting a struct value on a array should perform a bit-copy, so modifying 'el' should not affect 'arr[i]', but currently CTFE does it wrongly.
Note: the last 2 asserts refer to the case when the line 'el.a = i + 2;' is commented out.
A related test case which also fails, but involves static arrays rather than structs: bool bug6052b() { int[][1] arr; int[1] z = [7]; arr[0] = z; assert(arr[0][0] == 7); arr[0] = z; z[0] = 3; assert(arr[0][0] == 3); return true; } static assert(bug6052b()); https://github.com/D-Programming-Language/dmd/commit/bccb02ad1d8578767f99efeab4a230a229e24392 Case b: https://github.com/D-Programming-Language/dmd/commit/2ee56a0038ccac3b2225b7feda9d69798cc203e3 D1: https://github.com/D-Programming-Language/dmd/commit/bccb02ad1d8578767f99efeab4a230a229e24392
The bug still exists if the the struct has a constructor and we're appending to a dynamic array.... --------------------------------------------- struct Bug6052c { int x; this(int a) { x = a; } } static assert({ Bug6052c[] pieces = []; for (int c = 0; c < 2; ++ c) pieces ~= Bug6052c(c); assert(pieces[1].x == 1); // ok assert(pieces[0].x == 0); // asserts return true; }()); --------------------------------------------- x.d(10): Error: assert(pieces[0u].x == 0) failed <snipped> --------------------------------------------- or filling the uninitialized portion of a dynamic array.... --------------------------------------------- static assert({ int[1][] pieces = []; pieces.length = 2; for (int c = 0; c < 2; ++ c) pieces[c][0] = c; assert(pieces[1][0] == 1); // ok assert(pieces[0][0] == 0); // asserts return true; }()); --------------------------------------------- x.d(7): Error: assert(pieces[0u][0u] == 0) failed <snipped> ---------------------------------------------
case c: https://github.com/D-Programming-Language/dmd/commit/0ae51ead31246c7db23438d1d13bbfd8a2145f2f case d: https://github.com/D-Programming-Language/dmd/commit/084258c32964dc61333e057065339895d9aca6f0
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル