Bug 16576 – Strange behavior using static enum in struct

Status
RESOLVED
Resolution
DUPLICATE
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2016-10-02T15:30:57Z
Last change time
2021-11-12T03:20:42Z
Keywords
wrong-code
Assigned to
No Owner
Creator
Sophie

Comments

Comment #0 by meapineapple — 2016-10-02T15:30:57Z
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("?"); }
Comment #1 by b2.temp — 2019-11-24T11:44:11Z
on linux x86 too
Comment #2 by boris2.9 — 2021-11-12T03:20:42Z
*** This issue has been marked as a duplicate of issue 16579 ***