[Issue 3912] pure static nested functions are not recognized as pure

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Mar 10 13:48:08 PST 2010


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


Don <clugdbug at yahoo.com.au> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch


--- Comment #1 from Don <clugdbug at yahoo.com.au> 2010-03-10 13:48:03 PST ---
This was a mistake in my patch for bug 2804 (one of my first ever patches).
I had only considered one level of nesting.
I've also changed the error message so that it names the pure function, not the
nested impure one.
There's also some error messages with spurious newlines in them.
Here's a test case which should fail:
---
      void f() pure
      { void g()
        {
         void h() pure
         {
            void i() { }
            void j() { i(); g(); }
         }
        }
      }
------

PATCH against DMD2.041. Expression::checkPurity(), around line 1280.

Index: expression.c
===================================================================
--- expression.c    (revision 413)
+++ expression.c    (working copy)
@@ -1296,17 +1296,31 @@
      * i() can call h() and g() but not f()
      */
     FuncDeclaration *outerfunc = sc->func;
-    while (outerfunc->toParent2() &&
outerfunc->toParent2()->isFuncDeclaration())
+    // Find the closest pure parent of the calling function
+    while (outerfunc->toParent2() && !outerfunc->isPure()
+       && outerfunc->toParent2()->isFuncDeclaration() )
     {
         outerfunc = outerfunc->toParent2()->isFuncDeclaration();
     }
-    if (outerfunc->isPure() && !sc->intypeof && (!f->isNested() &&
!f->isPure()))
-        error("pure function '%s' cannot call impure function '%s'\n",
-        sc->func->toChars(), f->toChars());
+    // Find the closest pure parent of the called function
+    FuncDeclaration *calledparent = f;
+    while (calledparent->toParent2() && !calledparent->isPure()
+        && calledparent->toParent2()->isFuncDeclaration() )
+    {
+        calledparent = calledparent->toParent2()->isFuncDeclaration();
+    }
+    // If the caller has a pure parent, then either the called func must be
pure,
+        // OR, they must have the same pure parent.
+    if (outerfunc->isPure() && !sc->intypeof && 
+        !(f->isPure() || (calledparent == outerfunc)))
+    {
+        error("pure function '%s' cannot call impure function '%s'",
+        outerfunc->toChars(), f->toChars());
+    }
     }
 #else
     if (sc->func && sc->func->isPure() && !sc->intypeof && !f->isPure())
-    error("pure function '%s' cannot call impure function '%s'\n",
+    error("pure function '%s' cannot call impure function '%s'",
         sc->func->toChars(), f->toChars());
 #endif
 }
@@ -1315,7 +1329,7 @@
 {
     if (sc->func && sc->func->isSafe() && !sc->intypeof &&
     !f->isSafe() && !f->isTrusted())
-    error("safe function '%s' cannot call system function '%s'\n",
+    error("safe function '%s' cannot call system function '%s'",
         sc->func->toChars(), f->toChars());
 }
 #endif

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list