D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5497 - [CTFE] Cannot (reinterpret) cast string/array to different array
Summary: [CTFE] Cannot (reinterpret) cast string/array to different array
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-01-27 13:35 UTC by Aziz Köksal
Modified: 2015-06-09 05:11 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Aziz Köksal 2011-01-27 13:35:17 UTC
Example code:

uint[] toArray(string s)
{
  return cast(uint[])s;
}
version(BigEndian)
static assert(toArray("\xdd\xcc\xbb\xaa")[0] == 0xddccbbaa);
else
static assert(toArray("\xdd\xcc\xbb\xaa")[0] == 0xaabbccdd);



// The following code works, of course:
static this()
{
version(BigEndian)
  assert(toArray("\xdd\xcc\xbb\xaa")[0] == 0xddccbbaa);
else
  assert(toArray("\xdd\xcc\xbb\xaa")[0] == 0xaabbccdd);
}
Comment 1 Don 2011-11-08 02:43:00 UTC
This test case generated wrong code in 2.053 and earlier, but it actually should have been accepts-invalid.

Since 2.054 it generates

test.d(3): Error: array cast from string to uint[] is not supported at compile t
ime
test.d(8): Error: cannot evaluate toArray("\xdd\xcc\xbb\xaa") at compile time
test.d(8): Error: static assert  (toArray("\xdd\xcc\xbb\xaa")[0u] == -1430532899
u) is not evaluatable at compile time



https://github.com/D-Programming-Language/dmd/commit/5d6df984d50862f25b481790ad4ef86b0afd6c9a

Also the spec now states that such casts are not legal in CTFE.