D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5671 - CTFE string concat problem
Summary: CTFE string concat problem
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: rejects-valid
Depends on:
Blocks:
 
Reported: 2011-03-01 04:56 UTC by bearophile_hugs
Modified: 2011-04-18 16:02 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-03-01 04:56:19 UTC
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 Don 2011-03-01 05:08:35 UTC
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 Don 2011-03-05 17:56:46 UTC
Can be reduced even further, showing CTFE isn't involved at all:

static assert( ['a', 'b'] ~ "c" == "abc" );