Placement new and @trusted

IchorDev zxinsworld at gmail.com
Fri Sep 12 09:31:21 UTC 2025


On Thursday, 11 September 2025 at 11:47:40 UTC, Dennis wrote:
> The trick that's used in druntime is putting the part that 
> still needs to be checked for attributes inside an `if (false)` 
> block, for example:
>
> ```d
> private T moveImpl(T)(return scope ref T source)
> {
>     // Properly infer safety from moveEmplaceImpl as the 
> implementation below
>     // might void-initialize pointers in result and hence needs 
> to be @trusted
>     if (false) moveEmplaceImpl(source, source);
>
>     return trustedMoveImpl(source);
> }
> ```

Well, that would work. Thank you for pointing it out. It's 
probably better to do it like this to avoid the potential for an 
unconditional cache miss (depending on how smart the compiler 
feels today):
```d
private T moveImpl(T)(return scope ref T source){
	if(true) return trustedMoveImpl(source);
	moveEmplaceImpl(source, source);
	assert(0);
}
```
It would still be nice to have a proper, intuitive way of doing 
this though. Hacks are great and all, but they are still hacks. 
One time I got my code to call into Swift code by writing inline 
assembly to match Swift's calling convention; but that was a 
hack, and I'm still excited that LDC has recently gained some 
degree of native Swift interoperability.


More information about the Digitalmars-d-learn mailing list