Bug 13729 – [REG2.067a] One not detected case of not purity
Status
RESOLVED
Resolution
FIXED
Severity
regression
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2014-11-13T12:44:00Z
Last change time
2015-06-17T21:01:28Z
Keywords
accepts-invalid, pull
Assigned to
nobody
Creator
bearophile_hugs
Comments
Comment #0 by bearophile_hugs — 2014-11-13T12:44:47Z
This compiles with no errors, but main() is not pure:
int x;
void main() pure {
static void foo() {
x++;
}
foo();
}
This correctly gives an error:
int x;
void bar() {
x++;
}
void main() pure {
bar(); // Error
}
Comment #3 by bearophile_hugs — 2015-02-28T10:54:09Z
Now this code:
int x;
void main() pure {
static void foo() {
x++;
}
foo();
}
Gives:
test.d(4,9): Error: pure function 'D main' cannot access mutable static data 'x'
I think a more precise error message should say that the pure function main() can't call the impure static function foo(), and foo() can't be pure because it writes static data.
This program shows that foo() doesn't need to be annotated with "pure" for the code to compile:
int x;
void main() pure {
static void foo() {
}
foo();
}
Comment #4 by github-bugzilla — 2015-02-28T21:07:30Z