Bug 6374 – [CTFE] Cannot subscript using pointer to array
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Mac OS X
Creation time
2011-07-24T14:05:00Z
Last change time
2011-07-26T15:18:37Z
Keywords
rejects-valid
Assigned to
nobody
Creator
kennytm
Comments
Comment #0 by kennytm — 2011-07-24T14:05:05Z
Test case:
---------------------
static assert({
int[] arr = [1];
arr.ptr[0] = 2; // <-- line 3
return arr[0];
}() == 2);
---------------------
y.d(3): Error: cannot determine length of cast(int*)arr at compile time
y.d(5): Error: cannot evaluate delegate pure nothrow int()
{
int[] arr = [1];
(cast(int*)arr)[0u] = 2;
return arr[0u];
}
() at compile time
y.d(1): Error: static assert (delegate pure nothrow int()
{
int[] arr = [1];
(cast(int*)arr)[0u] = 2;
return arr[0u];
}
() == 2) is not evaluatable at compile time
---------------------
Slicing is OK, though.
Comment #1 by kennytm — 2011-07-24T14:53:02Z
Note that this prevents std.array.appender from being CTFE-able, because it contains the code:
_data.arr.ptr[len] = cast(Unqual!T)item; // <-- error
_data.arr = _data.arr.ptr[0 .. len + 1]; // (OK)
Currently, this could be worked-around by swapping the two statements and use the standard indexing:
_data.arr = _data.arr.ptr[0 .. len+1]; // OK
_data.arr[len] = cast(Unqual!T)item; // now OK
but I think the more proper solution is to fix this bug.