[Issue 17376] New: modify global variable with pure method

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun May 7 01:36:11 PDT 2017


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

          Issue ID: 17376
           Summary: modify global variable with pure method
           Product: D
           Version: D2
          Hardware: x86
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: arbmind at gmail.com

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.

--


More information about the Digitalmars-d-bugs mailing list