(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...