Bug 13897 – Wrong purity of inner function

Status
RESOLVED
Resolution
INVALID
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-12-26T12:46:00Z
Last change time
2014-12-27T09:08:51Z
Keywords
accepts-invalid
Assigned to
nobody
Creator
bearophile_hugs

Comments

Comment #0 by bearophile_hugs — 2014-12-26T12:46:59Z
This compiles with no errors in dmd 2.067alpha: void main() { int x; void foo() pure { x++; } }
Comment #1 by sinkuupump — 2014-12-27T00:24:55Z
Comment #2 by k.hara.pg — 2014-12-27T07:14:45Z
(In reply to bearophile_hugs from comment #0) > This compiles with no errors in dmd 2.067alpha: It's an intentional language change. Until 2.066, the nested function foo had been deduced as a strong purity function. But if foo is declared in struct or class scope, it had been deduced as a weak purity function. That was inconsistent. // 2.066 and earlier void main() { int x; void foo() pure { // wrongly deduced as strong purity x++; // modifying enclosing scope via context pointer was rejected. } } struct S { int x; void foo() pure { // weak purity x++; // OK } } In git-head, a nested function without any type qualifiers is deduced to weak purity so it has a hidden mutable context pointer, and the semantics is consistent with member functions. // 2.067alpha void main() { int x; void foo() pure { // deduced as weak purity, so x++; // modifying enclosing scope via context pointer is accepted. } void bar() immutable pure { // deduced as strong purity x++; // NG } } struct S { int x; void foo() pure { // weak purity x++; } void bar() immutable pure { // strong purity x++; // NG } } See also issue 9148 comments for more detail.
Comment #3 by bearophile_hugs — 2014-12-27T09:08:51Z
Now there are "immutable pure" functions in D to represent strongly pure nested functions? That's interesting...