[Issue 16576] New: Strange behavior using static enum in struct

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Sun Oct 2 08:30:57 PDT 2016


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

          Issue ID: 16576
           Summary: Strange behavior using static enum in struct
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: meapineapple at gmail.com

Using DMD32 D Compiler v2.071.2-b6 32bit on Win7 results in this example having
an access violation:

    import std.stdio;
    struct Thing{
        static enum Instance = Thing([0, 1, 2, 3]);
        int[] array;
        void iter(in string str) const{
            foreach(tup; this.array) tup.writeln;
        }
    }
    unittest{
        auto test(in string str){return Thing.Instance.iter(str);}
        test("?");
    }

When I first encountered this bug, it appeared that the `array` attribute in
the code I was working on was junk bytes.

This example, with the string arg removed, does not get an access violation but
results in incorrect behavior - the program should print out the numbers 0
through 3 but instead prints nothing:

    import std.stdio;
    struct Thing{
        static enum Instance = Thing([0, 1, 2, 3]);
        int[] array;
        void iter() const{
            foreach(tup; this.array) tup.writeln;
        }
    }
    unittest{
        auto test(){return Thing.Instance.iter();}
        test();
    }

This example, using `static immutable` instead of `static enum` on the third
line, behaves as expected:

    import std.stdio;
    struct Thing{
        static immutable Instance = Thing([0, 1, 2, 3]);
        int[] array;
        void iter(in string str) const{
            foreach(tup; this.array) tup.writeln;
        }
    }
    unittest{
        auto test(in string str){return Thing.Instance.iter(str);}
        test("?");
    }

This example, calling the `iter` method directly instead of via `test`, behaves
as expected:

    import std.stdio;
    struct Thing{
        static enum Instance = Thing([0, 1, 2, 3]);
        int[] array;
        void iter(in string str) const{
            foreach(tup; this.array) tup.writeln;
        }
    }
    unittest{
        Thing.Instance.iter("?");
    }

--


More information about the Digitalmars-d-bugs mailing list