[Issue 9148] 'pure' is broken

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Jul 3 10:59:42 PDT 2015


https://issues.dlang.org/show_bug.cgi?id=9148

--- Comment #16 from timon.gehr at gmx.ch ---
(In reply to Kenji Hara from comment #11)
> I'm adjusting the implicit pure annotation behavior for nested functions in:
> https://github.com/D-Programming-Language/dmd/pull/4344
> 
> If the nested function is a template, attribute inference is preferred and
> pure will be added only when the function is accessing enclosing pure
> function context.
> 
> auto foo() pure {
>     int x;
>     auto bar()() {   // template function bar can be impure by default
>         // bar will be forced to get weak purity, only when it's trying
>         // to access enclosing pure function context.
>         //x = 1;
> 
>         // otherwise, bar can call impure functions and then
>         // it will be inferred to impure.
>         impureFuncCall().
>     }
> }
> 
> Once nested template function is inferred to impure, it cannot access
> enclosing pure context anymore.
> 
> auto foo() pure {
>     int x;
>     auto bar()() {
>         impureFuncCall().
>         x = 1;  // Error: impure function 'bar' cannot access variable 'x'
>                 // declared in enclosing pure function 'foo'
>     }
> }

I think this is not right. Why should impure nested functions not be able to
access the enclosing context corresponding to frames of pure functions?

--


More information about the Digitalmars-d-bugs mailing list