[Issue 24709] New: Cannot get a reference to each element of an array on foreach-loop with -preview=nosharedaccess

d-bugmail at puremagic.com d-bugmail at puremagic.com
Sat Aug 17 15:39:00 UTC 2024


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

          Issue ID: 24709
           Summary: Cannot get a reference to each element of an array on
                    foreach-loop with -preview=nosharedaccess
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: komatsu.kazuki.op at tut.jp

References to shared variables can be obtained without atomic operations, but
foreach statements that take references to array elements produce unexpected
errors with -preview=nosharedaccess.
I have confirmed that all dmd compilers from version 2.093.1 to latest (and
also in the current nightly where the last commit hash is 1d2e15e) have the
same bug.

```d
void main()
{
    //OK: We can get a reference of a shared variable.
    {
        void foo(ref shared(int) a) {}
        shared int a;
        foo(a);
    }


    //NG: Cannot get a reference to each element of an array on foreach-loop
    {
        shared(int)[] arr;

        // Error: direct access to shared `__r2[__key3]` is not allowed, see
`core.atomic`
        foreach(ref e; arr) {}
    }
}
```

--


More information about the Digitalmars-d-bugs mailing list