[Issue 6247] New: Disallow static mutable variables definitions in pure functions

d-bugmail at puremagic.com d-bugmail at puremagic.com
Mon Jul 4 04:17:06 PDT 2011


http://d.puremagic.com/issues/show_bug.cgi?id=6247

           Summary: Disallow static mutable variables definitions in pure
                    functions
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: diagnostic
          Severity: enhancement
          Priority: P2
         Component: DMD
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2011-07-04 04:12:02 PDT ---
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() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list