D2 code: void main() { char[] s = "ABC".dup; auto a = cast(char[3])s; } I think the error message contains "e2ir: " by mistake, DMD 2.058: test.d(3): Error: e2ir: cannot cast s of type char[] to type char[3u]
*** This issue has been marked as a duplicate of issue 5113 ***
(In reply to comment #0) > I think the error message contains "e2ir: " by mistake, DMD 2.058: It's a minor dmd ICE.
I put the fix in the pull in issue 11484
*** Issue 5113 has been marked as a duplicate of this issue. ***
Tricky, thanks to this: auto str2 = cast(wchar[3])("789abc"c); writefln("str2: ", (cast(char[])str2).length , " : ", (cast(char[])str2)); assert(cast(char[])str2 == "789abc"c); I think we should disallow casting a string literal to a static array with a different character size.
Another case (according to Kenji Hara): void main() /*@nogc*/ { int[2][1] m = cast(int[2][1])[1, 2]; } Gives: test2.d(2,34): Error: e2ir: cannot cast [1, 2] of type int[] to type int[2][1]
*** Issue 13849 has been marked as a duplicate of this issue. ***
(In reply to Kenji Hara from comment #7) > *** Issue 13849 has been marked as a duplicate of this issue. *** Your have asked me to aggregate two different problems: - This issue originally was just asking to remove the "e2ir" in the error message. This is a bug report. - Now this issue is also an enhancement request, asking for code like "int[2][1] m = cast(int[2][1])[1, 2];" to be accepted.
(In reply to bearophile_hugs from comment #8) > Your have asked me to aggregate two different problems: And now you have also aggregated: void main() @safe { immutable ulong x = 10; auto y1 = cast(uint[2])x; // Error auto y2 = cast(uint[2])[x]; // Error immutable ulong[] x2 = [10]; auto y3 = cast(uint[2])x2; // Error immutable ulong[1] x3 = [10]; auto y4 = cast(uint[2])x3; // OK }
(In reply to bearophile_hugs from comment #8) > Your have asked me to aggregate two different problems: > - This issue originally was just asking to remove the "e2ir" in the error > message. This is a bug report. It's now properly handled in the issue 13810. So I set the "Blocks" field. > - Now this issue is also an enhancement request, asking for code like > "int[2][1] m = cast(int[2][1])[1, 2];" to be accepted. Why? The "Importance" field had not been set to "enhancement" any time. And you had not *ask* the acceptability about the code by the comments. And, I think the cast from int[] to int[2][1] should not be accepted.
(In reply to Kenji Hara from comment #10) > I think the cast from int[] to int[2][1] should not be accepted. Can you please explain why? Currently this is accepted: int[2] m = cast(int[2])[1, 2];
(In reply to bearophile_hugs from comment #11) > (In reply to Kenji Hara from comment #10) > > > I think the cast from int[] to int[2][1] should not be accepted. > > Can you please explain why? > > Currently this is accepted: > int[2] m = cast(int[2])[1, 2]; In cast(int[2])[1, 2], the array literal can *structurally* match to the type int[2] - all elements can be implicitly convertible to int, and the array literal contains 2 elements. Therefore the cast is accepted. Contrary to that, in cast(int[2][1])[1, 2] compiler cannot find any structural conversions. Therefore it will be a reinterpret cast, but dmd has no proper codegen way for the cast from int[] to int[2][1]. That's why the former is accepted but the latter isn't.
All issue cases in comment #0, comment #6, comment #9 are now fixed in git-head (36320a24ba58b308c71839824a26200ea5c867a0). Related compiler fix: https://github.com/D-Programming-Language/dmd/pull/4691