D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7514 - [e2ir] Error in e2ir at dynamic array to static array cast
Summary: [e2ir] Error in e2ir at dynamic array to static array cast
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 minor
Assignee: No Owner
URL:
Keywords: ice
: 13849 (view as issue list)
Depends on: 11484
Blocks: 13810
  Show dependency treegraph
 
Reported: 2012-02-15 18:32 UTC by bearophile_hugs
Modified: 2015-06-22 05:22 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2012-02-15 18:32:14 UTC
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]
Comment 1 yebblies 2012-02-15 19:50:08 UTC

*** This issue has been marked as a duplicate of issue 5113 ***
Comment 2 Denis Shelomovskii 2013-11-09 10:30:28 UTC
(In reply to comment #0)
> I think the error message contains "e2ir: " by mistake, DMD 2.058:

It's a minor dmd ICE.
Comment 3 yebblies 2013-11-16 20:37:34 UTC
I put the fix in the pull in issue 11484
Comment 4 yebblies 2013-11-16 20:38:07 UTC
*** Issue 5113 has been marked as a duplicate of this issue. ***
Comment 5 yebblies 2014-01-24 22:43:00 UTC
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.
Comment 6 bearophile_hugs 2015-01-15 02:09:19 UTC
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]
Comment 7 Kenji Hara 2015-01-15 02:10:56 UTC
*** Issue 13849 has been marked as a duplicate of this issue. ***
Comment 8 bearophile_hugs 2015-01-15 02:12:59 UTC
(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.
Comment 9 bearophile_hugs 2015-01-15 02:15:48 UTC
(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
}
Comment 10 Kenji Hara 2015-01-15 04:09:19 UTC
(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.
Comment 11 bearophile_hugs 2015-01-15 09:20:17 UTC
(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];
Comment 12 Kenji Hara 2015-01-22 16:22:49 UTC
(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.
Comment 13 Kenji Hara 2015-06-22 05:22:42 UTC
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