Should I worry about this hashOf warning ?

axricard axelrwiko at gmail.com
Wed Jun 25 15:38:55 UTC 2025


When trying to use Nullable with a struct containing an 
overlapping pointer:

```D
import std;

struct S
{
     union
     {
     	long foo;
        	int[] bar;
     }
}

void main()
{
     Nullable!S s;
}
```

I get this warning:

  ```
/dlang/dmd/linux/bin64/../../src/phobos/std/typecons.d(3820): 
Deprecation: `@safe` function `toHash` calling `hashOf`
             return _isNull ? 0 : .hashOf(_value.payload);
                                         ^
/dlang/dmd/linux/bin64/../../src/druntime/import/core/internal/hash.d-mixin-551(664):        and accessing overlapped field `S.bar` with pointers makes it fail to infer `@safe`
```

The [spec](https://dlang.org/spec/function.html#safe-functions) 
is states that a @safe functions "cannot access union fields that 
have pointers or references overlapping with other types", and 
this toHash in std.typecons is @safe.

So I wonder, why is it just a warning and not a compile error 
(the code compiles and runs normally) ? Is there any risk at 
ignoring this warning ?

And if there is a risk, is there a workaround or do I have to 
drop the Nullable struct ?


More information about the Digitalmars-d-learn mailing list