import std.stdio; char[] get_str() { char[4] retVal = ['t', 'e', 's', 't']; return retVal; } void main() { char[] res = get_str(); writefln(res == "test" ? "SUCCESS" : "FAIL"); } Results: GDC 0.21 Mac, gdc -o testcase_debug -fdebug -fbounds-check unique.d FAIL GDC 0.21 Mac, gdc -o testcase -O3 -finline -fno-bounds-check unique.d SUCCESS DMD 1.002 Linux, dmd -oftestcase_debug -debug unique.d FAIL DMD 1.002 Linux, dmd -oftestcase -O -release -inline unique.d SUCCESS ------ Attempting to output res results in "`_Error: 4invalid UTF-8 sequence" when this comparison fails. This bug also has a weird bonus: If the comparison is moved into a function it succeeds in debug builds, but attempting to output res results in the above UTF-8 error.
It fails because statically dimensioned arrays, like char[4], are allocated on the stack, and the return statement is returning a reference to that stack variable. When the function returns, the stack variable becomes garbage.
Please add an error for this. Probably it is not possible to catch all such cases. But it should be possible for the trivial case: returning a reference to local defined char[4]. This will prevent more ppl from having that problem. I changed this item from bug to enhancement.
Fixed DMD 1.005
AltStyle によって変換されたページ (->オリジナル) / アドレス: モード: デフォルト 音声ブラウザ ルビ付き 配色反転 文字拡大 モバイル