Issue 6374 - [CTFE] Cannot subscript using pointer to array
Summary: [CTFE] Cannot subscript using pointer to array
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Mac OS X
: P2 normal
Assignee: No Owner
URL:
Keywords: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-07-24 14:05 UTC by kennytm
Modified: 2011-07-26 15:18 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 kennytm 2011-07-24 14:05:05 UTC
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 kennytm 2011-07-24 14:53:02 UTC
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.