Bug 4291 – Pure functions cannot access mixed in variables

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2010-06-07T02:40:00Z
Last change time
2014-02-15T02:43:10Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
rsinfu

Comments

Comment #0 by rsinfu — 2010-06-07T02:40:13Z
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 by rsinfu — 2010-06-07T02:41:25Z
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 by bugzilla — 2010-08-28T14:30:12Z