Bug 4027 – Closures in CTFE generate wrong code

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2010-03-29T14:19:00Z
Last change time
2014-02-15T02:42:23Z
Keywords
wrong-code
Assigned to
nobody
Creator
zan77137

Comments

Comment #0 by zan77137 — 2010-03-29T14:19:25Z
(This bug was posted to the Japanese community site. I translate this report.) This code doesn't work! ------------------------------------------- auto f(string s) { return { return s; }; } void main() { static immutable s = f("aaa")(); static assert(s == "aaa");// Error: static assert (null == "aaa") is false } ------------------------------------------- The function fails regardless of the argument when execute the following codes in CTFE: ------------------------------------------- auto f(string s) { return { assert(s); }; } ------------------------------------------- You can take the following measures to avoid this problem: ------------------------------------------- auto f(string s) { auto _s = s; return { assert(_s); }; } -------------------------------------------
Comment #1 by clugdbug — 2010-03-30T11:42:35Z
The workaround does NOT work. It only looks as though it does. See this, for example: === string delegate() bug4027(string s1) { string s = s1; return { return s; }; } int food() { auto a = bug4027("aaa"); auto b = bug4027("bbb"); assert(a() == "aaa"); // fails -- a() is "bbb" !!! return 1; } static assert(food()==1); ---- The root cause is that closures are not yet supported in CTFE. They should generate an error message at compile time.
Comment #2 by bugzilla — 2010-04-01T13:54:35Z
changeset 429
Comment #3 by clugdbug — 2010-04-09T19:21:30Z
Fixed DMD2.043.