Issue 24709 - Cannot get a reference to each element of an array on foreach-loop with -preview=nosharedaccess
Summary: Cannot get a reference to each element of an array on foreach-loop with -prev...
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 normal
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-08-17 15:39 UTC by Kazuki Komatsu
Modified: 2024-08-17 15:39 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Kazuki Komatsu 2024-08-17 15:39:00 UTC
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) {}
    }
}
```