[Issue 2807] Marking a nested function as 'pure' may cause bad code generations silently accepted

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Apr 22 13:07:16 PDT 2009


http://d.puremagic.com/issues/show_bug.cgi?id=2807





------- Comment #1 from clugdbug at yahoo.com.au  2009-04-22 15:07 -------
And here's a patch. This makes it safe to have nested pure functions.
A nested pure function is only permitted to access its own variables, plus
immutables and manifest constants from outer scopes.


===================================================================
--- expression.c        (revision 24)
+++ expression.c        (working copy)
@@ -3994,12 +3994,21 @@
 #if 1
        if (sc->func) {
                FuncDeclaration *outerfunc=sc->func;
+               bool hasPureParent=false;
                while (outerfunc->toParent2() &&
outerfunc->toParent2()->isFuncDeclaration()) {
+                       hasPureParent |= outerfunc->isPure();
                        outerfunc =
outerfunc->toParent2()->isFuncDeclaration();
                }
-    if (outerfunc->isPure()  && !sc->intypeof && v->isDataseg() &&
!v->isInvariant())
+               hasPureParent |= outerfunc->isPure();
+               // If ANY of its enclosing functions are pure, it cannot do
anything impure.
+               // If it is pure, it cannot access any mutable variables other
than those inside itself.
+       if (hasPureParent && !sc->intypeof && v->isDataseg() &&
!v->isInvariant()) {
                error("pure function '%s' cannot access mutable static data
'%s'", sc->func->toChars(), v->toChars());
-       }
+       } else if (sc->func->isPure() && sc->parent!=v->parent && !sc->intypeof
&& !v->isInvariant() && !(v->storage_class & STCmanifest)) {            
+                       error("pure nested function '%s' cannot access mutable
data '%s'", sc->func->toChars(), v->toChars()); 
+                  if (v->isEnumDeclaration()) error("enum");
+       }       
+       } 
 #else
        if (sc->func && sc->func->isPure() && !sc->intypeof)
        {


-- 



More information about the Digitalmars-d-bugs mailing list