D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 17218 - foreach on tupleof inside switch returns ref to undefined address
Summary: foreach on tupleof inside switch returns ref to undefined address
Status: RESOLVED DUPLICATE of issue 16521
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2017-02-23 13:36 UTC by Alex Goltman
Modified: 2017-04-23 13:38 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Alex Goltman 2017-02-23 13:36:08 UTC
```
import std.stdio;

struct S {
    int x;
}
S s;
void main() {
    foreach (i, ref m; s.tupleof) {
        writefln("%s %s, %s", &s.tupleof[i], &m, m);
    }
    switch (0) {
        foreach (i, ref m; s.tupleof) {
            case i:
                writefln("%s %s, %s", &s.tupleof[i], &m, m);
        }
        default:
    }
}
```

Output:
```
7FAEC3D00260 7FAEC3D00260, 0
7FAEC3D00260 10DBF58F7, 15760771
```

Sometimes it crashes on segmentation fault since it doesn't really copy `m` where it thinks it is.
Same happens when `s` is `__gshared S s;`.
Reproduces both on `dmd v2.071.0 on OSX` and `ldc2 1.1.0git-3139c87 on linux`.
Comment 1 Steven Schveighoffer 2017-04-23 13:38:34 UTC

*** This issue has been marked as a duplicate of issue 16521 ***