7197 – enum string doesn't work with CTFE

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 7197 - enum string doesn't work with CTFE
Summary: enum string doesn't work with CTFE
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Mac OS X
: P2 regression
Assignee: No Owner
URL:
Keywords: CTFE
Depends on:
Blocks:
Reported: 2012年01月01日 17:44 UTC by Andrei Alexandrescu
Modified: 2012年01月27日 02:15 UTC (History)
4 users (show)

See Also:


Attachments
Add an attachment (proposed patch, testcase, etc.)

Note You need to log in before you can comment on or make changes to this issue.
Description Andrei Alexandrescu 2012年01月01日 17:44:07 UTC
import std.stdio;
string toConcatenatedForm(string m, string[] args...)
{
 string result = "q{";
 for (;;)
 {
 if (!m.length)
 {
 return result ~ '}';
 }
 if (m[0] != '$')
 {
 result ~= m[0];
 m = m[1 .. $];
 continue;
 }
 if (m[1] >= '1' && m[1] <= '9')
 {
 result ~= "} ~ ";
 result ~= "expand!(q{" ~ args[m[1] - '1'] ~ "}) ~ q{";
 m = m[2 .. $];
 continue;
 }
 }
}
template expand(string m, args...)
{
 enum string expand = mixin(toConcatenatedForm(m, args));
}
unittest
{
 enum string s = expand!("1ドル", "abc");
 writeln(s);
 writeln(expand!("1ドル", s));
}
void main(){}
The code above causes an ICE:
Assertion failed: (v->ctfeAdrOnStack >= 0 && v->ctfeAdrOnStack < stackPointer()), function setValue, file interpret.c, line 100.
Replacing the first line in the unittest with
 static immutable string s = expand!("1ドル", "abc");
makes the code work.
Comment 1 bearophile_hugs 2012年01月01日 18:15:19 UTC
Reduced a bit:
int foo(int[] x...) {
 return 0;
}
template bar(y...) {
 enum int bar = foo(y);
}
enum int z = 1;
enum int w = bar!(z);
void main() {}
Comment 2 Geoffrey Brown 2012年01月25日 10:00:46 UTC
Weird, it can be fixed by changing z to a const:
int foo(int[] x...) {
 return 0;
}
template bar(y...) {
 enum int bar = foo(y);
}
const int z = 1;
enum int w = bar!(z);
void main() {}
I'm unable to upgrade to 2.057 because of this bug, it works fine in 2.056.
Comment 3 github-bugzilla 2012年01月27日 00:51:34 UTC
Commit pushed to master at https://github.com/D-Programming-Language/dmd
https://github.com/D-Programming-Language/dmd/commit/6c081a2bf05524c83e87c71819a9240009a8a7e7
7197 enum string doesn't work with CTFE
Fixes bug 7197.
Oddly, this was actually a bug in the const folding for comma expressions.
The code was originally there as a workaround for another problem which has
since been fixed.


AltStyle によって変換されたページ (->オリジナル) /