Note: This website is archived. For up-to-date information about D projects and development, please visit wiki.dlang.org.

Changeset 646

Show
Ignore:
Timestamp:
08/28/10 21:55:03 (14 years ago)
Author:
walter
Message:

Issue 4177 - ctfe can't be used in pure functions

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/expression.c

    r645 r646  
    42584258             * i() can modify hx and gx but not fx 
    42594259             */ 
    42604260 
    42614261            /* Determine if sc->func is pure or if any function that 
    42624262             * encloses it is also pure. 
    42634263             */ 
    42644264            bool hasPureParent = false; 
    42654265            for (FuncDeclaration *outerfunc = sc->func; outerfunc;) 
    42664266            { 
    42674267                if (outerfunc->isPure()) 
    42684268                { 
    42694269                    hasPureParent = true; 
    42704270                    break; 
    42714271                } 
    42724272                Dsymbol *parent = outerfunc->toParent2(); 
    42734273                if (!parent) 
    42744274                    break; 
    42754275                outerfunc = parent->isFuncDeclaration(); 
    42764276            } 
    42774277 
    4278             /* If ANY of its enclosing functions are pure, 
    4279              * it cannot do anything impure. 
    4280              * If it is pure, it cannot access any mutable variables other 
    4281              * than those inside itself 
     4278            /* Magic variable __ctfe never violates pure or safe 
    42824279             */ 
    4283             if (hasPureParent && v->isDataseg() && 
    4284                 !v->isImmutable()) 
     4280            if (v->ident != Id::ctfe) 
    42854281            { 
    4286                 error("pure function '%s' cannot access mutable static data '%s'", 
    4287                     sc->func->toChars(), v->toChars()); 
    4288             } 
    4289             else if (sc->func->isPure() && 
    4290                 sc->parent->pastMixin() != v->parent->pastMixin() && 
    4291                 !v->isImmutable() && 
    4292                 !(v->storage_class & STCmanifest)) 
    4293             { 
    4294                 error("pure nested function '%s' cannot access mutable data '%s'", 
    4295                     sc->func->toChars(), v->toChars()); 
    4296                 if (v->isEnumDeclaration()) 
    4297                     error("enum"); 
     4282                /* If ANY of its enclosing functions are pure, 
     4283                 * it cannot do anything impure. 
     4284                 * If it is pure, it cannot access any mutable variables other 
     4285                 * than those inside itself 
     4286                 */ 
     4287                if (hasPureParent && v->isDataseg() && 
     4288                    !v->isImmutable()) 
     4289                { 
     4290                    error("pure function '%s' cannot access mutable static data '%s'", 
     4291                        sc->func->toChars(), v->toChars()); 
     4292                } 
     4293                else if (sc->func->isPure() && 
     4294                    sc->parent->pastMixin() != v->parent->pastMixin() && 
     4295                    !v->isImmutable() && 
     4296                    !(v->storage_class & STCmanifest)) 
     4297                { 
     4298                    error("pure nested function '%s' cannot access mutable data '%s'", 
     4299                        sc->func->toChars(), v->toChars()); 
     4300                    if (v->isEnumDeclaration()) 
     4301                        error("enum"); 
     4302                } 
    42984303            } 
    42994304 
    43004305            /* Do not allow safe functions to access __gshared data 
    43014306             */ 
    43024307            if (sc->func->isSafe() && v->storage_class & STCgshared) 
    43034308                error("safe function '%s' cannot access __gshared data '%s'", 
    43044309                    sc->func->toChars(), v->toChars()); 
    43054310        } 
    43064311#else 
    43074312        if (sc->func && sc->func->isPure() && !sc->intypeof) 
    43084313        { 
    43094314            if (v->isDataseg() && !v->isImmutable()) 
    43104315                error("pure function '%s' cannot access mutable static data '%s'", sc->func->toChars(), v->toChars()); 
    43114316        } 
    43124317#endif 
    43134318#endif 
    43144319    } 
    43154320#if 0 
    43164321    else if ((fd = var->isFuncLiteralDeclaration()) != NULL) 
    43174322    {   Expression *e;