Fixing tail mutable TLS/field initializers

Nick Treleaven nick at geany.org
Mon Jun 10 15:41:17 UTC 2024


On Friday, 7 June 2024 at 11:05:03 UTC, Nick Treleaven wrote:
> ```d
> int[] x = [1, 2, 3];
> ```
> The above is really like:
> ```d
> __gshared gsx = [1, 2, 3];
> int[] x = gsx;
> ```

Rather:

```d
__gshared int[3] gsx = [1, 2, 3];
```

So `x` is a TLS slice of `__gshared` data.

And given that `__gshared` mutable variables are not allowed to 
be accessed from @safe code (including basic data types), tail 
mutable initializers should also be disallowed in safe code, to 
prevent data races.

> So if one thread mutates `x[0]`, even though `x` is 
> thread-local, existing and new threads will get their `x[0]` 
> contents mutated too (assuming `x` still points to the 
> initializer data).

Mutable `shared` globals & fields can still be allowed to use 
tail mutable initializers.


More information about the dip.ideas mailing list