pure is as pure does with LLVM compiler

Ben Cumming bcumming at cscs.ch
Mon Jan 13 05:37:05 PST 2014


Hi There,

I am playing around CTFE, and I get different compile time
behavior with the reference compiler (both 64-bit Linux):
       DMD64 D Compiler v2.064
and the LLVM compiler:
       LDC - the LLVM D compiler (37ee99): based on DMD v2.063.2 
and
LLVM 3.3

The code snippet at the bottom of the post compiles fine with the
reference compiler, but with LDC I get the following error:
> ldc2 example.d
example.d(4): Error: pure function 'append_index' cannot call
impure function 'to'

'to' is not pure, however it seems the reference compiler is able
to determine that to is effectively pure. If I remove the 'pure'
keyword from the definition of 'append_index', the problem is
resolved.

Is it "reasonable" that ldc gives this error?
What is the best practice in this situation (I require the result
of such function calls for constructing more complicated strings
at compile time).

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

import std.conv;

string append_index(uint i) pure {
           return "v_" ~ to!string(i);
}

void main() {
       static assert(A.append_index(3)=="v_3");
}


More information about the Digitalmars-d-learn mailing list