tg counter robustness

Dukc ajieskola at gmail.com
Fri May 30 10:08:54 UTC 2025


On Thursday, 29 May 2025 at 19:03:49 UTC, monkyyy wrote:
> ```d
> enum counter=cast(immutable(void)*)[0].ptr;
> auto setcount(int i)=>(*(st(int*)counter))=i;
> ```

This is undefined behaviour. `counter` is typed as pointing to 
immutable data, but you're mutating it anyway in `setcount` which 
the compiler can assume you don't do. So there's no point in 
discussing what should make the unit test to pass or fail.

Yes, the one-element array is not originally typed as immutable, 
but it doesn't matter. `immutable` means that no-one will mutate 
the referenced data as long as the `immutable` reference exists, 
regardless of whether the data was read-only to begin with.


More information about the Digitalmars-d mailing list