5396 – [CTFE] Invalid code with nested functions in CTFE

D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5396 - [CTFE] Invalid code with nested functions in CTFE
Summary: [CTFE] Invalid code with nested functions in CTFE
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Linux
: P2 normal
Assignee: No Owner
URL:
Keywords: accepts-invalid
Depends on:
Blocks:
Reported: 2011年01月01日 10:54 UTC by Robert Clipsham
Modified: 2015年06月09日 05:11 UTC (History)
1 user (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 Robert Clipsham 2011年01月01日 10:54:57 UTC
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 Don 2011年05月24日 21:40:59 UTC
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


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