Bug 17376 – modify global variable with pure method
Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2017-05-07T08:36:00Z
Last change time
2017-05-07T08:59:20Z
Assigned to
nobody
Creator
arbmind
Comments
Comment #0 by arbmind — 2017-05-07T08:36:11Z
Consider this test code:
int ga;
struct Tester {
int* pa;
this(int) { pa = &ga; }
void test() pure {
*pa = 3;
}
}
void main() {
auto t = Tester(1);
assert(ga == 0);
t.test();
assert(ga == 3);
}
From the perspective of the caller, he is calling a pure function, but can observe a global state change.
It is also possible to change a static member of the struct in the same manner.
I guess that this inhibits some opportunities for optimizations.
Comment #1 by bugzilla — 2017-05-07T08:59:20Z
This compiles successfully because the 'this' reference is treated like any other parameter.