D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 6511 - [CTFE] Array op gives wrong result
Summary: [CTFE] Array op gives wrong result
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86 Windows
: P2 normal
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2011-08-16 13:49 UTC by bearophile_hugs
Modified: 2011-08-23 02:10 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description bearophile_hugs 2011-08-16 13:49:05 UTC
T foo(T)() {
    T[1] a = [1];
    a[] += a[];
    return a[0];
}
static assert(foo!ulong() == 2); // error
static assert(foo!long() == 2); // error
void main() {
    assert(foo!ulong() == 2); // OK
    assert(foo!long() == 2); // OK
}


DMD gives:

test.d(6): Error: static assert  (1LU == 2LU) is false
Comment 2 bearophile_hugs 2011-08-22 23:21:42 UTC
The given code example now works. But this:


T foo(T)() {
    T[1] a = [1];
    a[] += a[];
    return a[0];
}
static assert(foo!long() == 2); // OK
static assert(foo!int() == 2); // error
void main() {}


Gives:

test.d(3): Error: _arraySliceSliceAddass_i cannot be interpreted at compile time, because it has no available source code
test.d(7): Error: cannot evaluate foo() at compile time
test.d(7): Error: static assert  (foo() == 2) is not evaluatable at compile time

This behaviour difference between long and int is not good. Do you prefer me to reopen this bug or to open a new one?
Comment 3 Don 2011-08-23 01:29:13 UTC
(In reply to comment #2)
> The given code example now works. But this:
> 
> 
> T foo(T)() {
>     T[1] a = [1];
>     a[] += a[];
>     return a[0];
> }
> static assert(foo!long() == 2); // OK
> static assert(foo!int() == 2); // error
> void main() {}
> 
> 
> Gives:
> 
> test.d(3): Error: _arraySliceSliceAddass_i cannot be interpreted at compile
> time, because it has no available source code
> test.d(7): Error: cannot evaluate foo() at compile time
> test.d(7): Error: static assert  (foo() == 2) is not evaluatable at compile
> time
> 
> This behaviour difference between long and int is not good. Do you prefer me to
> reopen this bug or to open a new one?

A new one. Surprisingly, it actually has *nothing* in common with this bug.
This bug was actually a problem with array slicing, and didn't require array operations.
The new case is that the array operations which are special-cased in the runtime don't have special case treatment in the CTFE engine, since CTFE doesn't have access to the source code of druntime. The array ops which aren't special-cased don't use druntime code, so they should all work.
Comment 4 bearophile_hugs 2011-08-23 02:10:18 UTC
(In reply to comment #3)

> A new one.

OK. It is bug 6545