[Dlang-internal] Detect CTFE in AssignExp:semantic

Lucia Cojocaru via Dlang-internal dlang-internal at puremagic.com
Mon Jan 16 14:09:00 PST 2017


On Thursday, 12 January 2017 at 01:29:12 UTC, Walter Bright wrote:
> Take a look at the D test suite, such as:
>
>     
> https://github.com/dlang/dmd/blob/master/test/runnable/test42.d
>
> Each of these tests cases started out as some long complicated 
> thing that got reduced to a very small self-contained test case.

Here is a smaller example:

ptrdiff_t f(char[] s) {
     // foreach(c; s) if (c == '.') return 0; // (0)fails with the 
same error message
     foreach(char c; s) if (c == '.') return 0; // (1)
     // foreach(dchar c; s) if (c == '.') return 0; // (2) 
different compile path, doesn't fail

     /*
     for (size_t i = 0; i < s.length; i++)
     {
         if (s[i] == '.') return i;
     }
     */
     return -1;
}

pragma(msg, f(['z', 'b', 'c', '.', 'd']));

-------------------------------------------------------

ctfe.d(5): Error: variable __r54 cannot be read at compile time
ctfe.d(5):        called from here: _d_arraycopyT(__r54, s[])
ctfe.d(15):        called from here: f(['z', 'b', 'c', '.', 'd'])
ctfe.d(15):        while evaluating pragma(msg, f(['z', 'b', 'c', 
'.', 'd']))

-------------------------------------------------------

It looks like the problem is with the foreach loop. In the 
example above, the (0) and (1) loops result in the compilation 
error mentioned. Loop (2) with dchar is fine.

I also tried using a regular for loop and this one compiled.


More information about the Dlang-internal mailing list