Bug 2542 – array casts behave differently at compile and runtime

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Linux
Creation time
2008-12-26T11:19:00Z
Last change time
2014-03-01T00:36:46Z
Keywords
spec
Assigned to
bugzilla
Creator
kamm-removethis

Comments

Comment #0 by kamm-removethis — 2008-12-26T11:19:57Z
This is related to casts of arrays not being documented: http://d.puremagic.com/issues/show_bug.cgi?id=2494 The issue is that the following produce different results: const short[] ct = cast(short[]) [cast(byte)1, 1]; // ct is [1, 1] short[] rt = cast(short[]) [cast(byte)1, 1].dup; // rt is [ 257 ] The runtime cast seems to be defined as 'reinterpret the data as an array of a different type, recompute the length' whereas the compile time cast seems to be 'convert each element to the other element type, keep length unchanged'.
Comment #1 by bugzilla — 2008-12-27T04:35:12Z
You're right, this is a documentation issue. Casts of array literals are treated differently.
Comment #2 by kamm-removethis — 2008-12-27T06:11:38Z
While you're at it, could you also document these casts for string literals? short[] ct1 = cast(short[]) "\x01\x01"; // [ 257 ] short[] ct2 = cast(short[]) [cast(char)1, 1]; // [1, 1] even though both literals represent the same char[2] value.
Comment #3 by smjg — 2009-01-06T06:53:27Z
So [cast(byte)1, 1] isn't a byte[], an int[] or anything like that, but of an internal 'array literal' type. Then why is [cast(byte)1, 1].dup not also of this internal 'array literal' type?
Comment #4 by clugdbug — 2009-04-03T07:42:11Z
Fixed DMD2.023 and DMD1.039