Comment #0 by bearophile_hugs — 2011-03-01T04:56:19Z
A problem found by Peter Lundgren and David Nadlinger.
Reduced D2 code:
import std.conv: to;
string foo() {
// return to!string(10); // OK
return to!string(10) ~ "";
}
string s = foo();
void main() {}
DMD 2.052 gives the errors:
test.d(4): Error: to(10) ~ "" cannot be interpreted at compile time
test.d(6): Error: cannot evaluate foo() at compile time
test.d(6): Error: cannot evaluate foo() at compile time
I think it's not a problem of to!() because this alternative version works correctly:
import std.conv: to;
string foo() {
return to!string(10);
}
string s = foo();
void main() {}
Comment #1 by clugdbug — 2011-03-01T05:08:35Z
Reduced test case shows it is a constant folding problem.
['a', 'b'] ~ "c" doesn't get constant folded.
string foo5671() {
return ['a', 'b'];
}
string bug5671() {
return foo5671() ~ "c";
}
static assert(bug5671() == "abc");
Comment #2 by clugdbug — 2011-03-05T17:56:46Z
Can be reduced even further, showing CTFE isn't involved at all:
static assert( ['a', 'b'] ~ "c" == "abc" );