Comment #0 by bearophile_hugs — 2011-07-04T04:12:02Z
This is a spinoff of bug 4031
This program:
pure int foo() {
static int x = 1;
return x; // line 3
}
void main() {}
In DMD 2.053 it generates an error in returning x:
test.d(3): Error: pure function 'foo' cannot access mutable static data 'x'
The error message is but I suggest to disallow the definition of static mutable data too, this means disallowing line 2 too.
But mutable static variables can't be used in pure functions, so it's better to really disallow them, and produce a compile error at line 2 too, because there's no point in allowing their definition and disallowing just their usage.
Some answers to yebblies, from bug 4031:
> I'm not sure if disabling mutable static variables inside pure functions is
> valid, as some actions on them (eg. returning their address) do make sense.
Returning the address of a static variable defined inside a pure function? I
don't see the purpose of this. It smells.
> They can also be accessed from any statements inside debug {}.
But this need is probably uncommon, and when this needs arises you are able to
move the definition of such variable inside the debug:
pure int foo(int x) {
debug {
static int y = 10;
}
return x;
}
void main() {}
Comment #1 by robert.schadek — 2024-12-13T17:55:41Z