D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 4291 - Pure functions cannot access mixed in variables
Summary: Pure functions cannot access mixed in variables
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P2 normal
Assignee: No Owner
URL:
Keywords: patch, rejects-valid
Depends on:
Blocks:
 
Reported: 2010-06-07 02:40 UTC by Shin Fujishiro
Modified: 2014-02-15 02:43 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Shin Fujishiro 2010-06-07 02:40:13 UTC
DMD raises a compiler error when a mixed in variable is used in a pure function.

--------------------
void test() pure
{
    mixin declareVariable;
    var = 42; // Error: pure nested function 'test' cannot access
              //        mutable data 'var'
}
template declareVariable() { int var; }
--------------------

The mixed-in variable var should be treated as if it's directly declared in test()'s scope.  So the above code should be correct and accepted.
Comment 1 Shin Fujishiro 2010-06-07 02:41:25 UTC
Patch against DMD r524:
====================
--- src/expression.c
+++ src/expression.c
@@ -4406,7 +4406,7 @@ Expression *VarExp::semantic(Scope *sc)
                 error("pure function '%s' cannot access mutable static data '%s'",
                     sc->func->toChars(), v->toChars());
             }
-            else if (sc->func->isPure() && sc->parent != v->parent &&
+            else if (sc->func->isPure() && sc->parent->pastMixin() != v->parent->pastMixin() &&
                 !v->isImmutable() &&
                 !(v->storage_class & STCmanifest))
             {
====================

The patched code also deals with function's scope (sc->parent->pastMixin) so that this valid code is accepted:
--------------------
void test() pure
{
    mixin declareVariable;
    mixin declareFunction;
    readVar();
}
template declareVariable() { int var; }
template declareFunction()
{
    int readVar() { return var; }
}
--------------------
Comment 2 Walter Bright 2010-08-28 14:30:12 UTC
http://www.dsource.org/projects/dmd/changeset/645