2542 – array casts behave differently at compile and runtime

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 2542 - array casts behave differently at compile and runtime
Summary: array casts behave differently at compile and runtime
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D1 (retired)
Hardware: x86 Linux
: P2 normal
Assignee: Walter Bright
URL:
Keywords: spec
Depends on:
Blocks:
Reported: 2008年12月26日 11:19 UTC by Christian Kamm
Modified: 2014年03月01日 00:36 UTC (History)
1 user (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Christian Kamm 2008年12月26日 11:19:57 UTC
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 Walter Bright 2008年12月27日 04:35:12 UTC
You're right, this is a documentation issue. Casts of array literals are treated differently.
Comment 2 Christian Kamm 2008年12月27日 06:11:38 UTC
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 Stewart Gordon 2009年01月06日 06:53:27 UTC
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 Don 2009年04月03日 07:42:11 UTC
Fixed DMD2.023 and DMD1.039


AltStyle によって変換されたページ (->オリジナル) /