Bug 10254 – Purity correctness is broken with constructor
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-06-03T07:57:00Z
Last change time
2013-07-13T23:52:46Z
Keywords
accepts-invalid, pull
Assigned to
nobody
Creator
zan77137
Comments
Comment #0 by zan77137 — 2013-06-03T07:57:24Z
This code doesn't work:
---------------
int a;
auto foo() pure
{
static class A { this(){a = 2;} }
return new A; // This line should be a compilation error.
}
void main()
{
a = 1;
auto x = foo(); // Pure function doesn't change any global variables.
assert(a == 1); // Thefore, global variable `a` never changes.
}
Comment #1 by simen.kjaras — 2013-06-03T08:34:47Z
Interestingly, this fails to compile:
int a;
auto foo() pure
{
return (){a=2;} // Error: pure function 'foo.foo.__lambda1'
// cannot access mutable static data 'a'
}
void main()
{
a = 1;
auto x = foo();
x();
assert(a == 1);
}
This behavior indicates to me that it is in fact A's constructor that should be a compilation error. It is a Voldemort type anyway, so how are you ever going to call that constructor safely?