Bug 5396 – [CTFE] Invalid code with nested functions in CTFE
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Linux
Creation time
2011-01-01T10:54:00Z
Last change time
2015-06-09T05:11:41Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
robert
Comments
Comment #0 by robert — 2011-01-01T10:54:57Z
The following code:
----
void outer(bool b)
{
string inner()
{
if (b)
{
return "true";
}
else
{
return "false";
}
}
pragma(msg, inner());
}
----
Compiles and evaluates without error, despite the dependence on the runtime value b.
Comment #1 by clugdbug — 2011-05-24T21:40:59Z
Extended test cases (applies to delegate literals as well as inner functions):
void bug5396(int b)
{
int inner() { return b; }
static int staticInner() { return 6; }
static assert(is(typeof(compiles!(
function int () { return 7; }()
))));
static assert(!is(typeof(compiles!(
delegate int () { return b; }()
))));
static assert(!is(typeof(compiles!(
inner()
))));
static assert(is(typeof(compiles!(
staticInner()
))));
}
Patch in interpret.c, CallExp::interpret(). Problem is, this breaks bug 1461 which seems to be an important use case.
if (!istate && fd && fd->isNested() && !fd->isStatic() &&
/* BUG: Delegate literals report 'isNested()' even if they are
* declared at module scope.
*/
!(fd->isFuncLiteralDeclaration() && !fd->toParent2()->isFuncDeclaration()))
{
error("cannot directly interpret non-static nested function %s", fd->toChars());
return EXP_CANT_INTERPRET;
}
if (pthis && fd)
{ // Member function call