Bug 6247 – Disallow static mutable variables definitions in pure functions

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-07-04T04:12:02Z
Last change time
2024-12-13T17:55:41Z
Keywords
diagnostic
Assigned to
No Owner
Creator
bearophile_hugs
Moved to GitHub: dmd#18350 →

Comments

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
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/18350 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB