Prevent struct going into heap memory

Paul Backus snarwin at gmail.com
Mon Jul 21 00:00:45 UTC 2025


On Sunday, 20 July 2025 at 17:04:11 UTC, Richard (Rikki) Andrew 
Cattermole wrote:
> In response to some concerns around the situation for RAII 
> structs going into closures, I am proposing a resolution to 
> this that will be a language-wide guarantee, not an 
> ultra-specific, not-a-guarantee solution.
>
> https://github.com/dlang/dmd/issues/18704
>
> If a struct destructor is annotated with an attribute 
> ``@stackonly`` it may only be called if the ``this`` pointer is 
> allocated on the stack. It does not overload.

Isn't the obvious solution to this bug (and related ones like 
[issue #19119][1]) to simply skip the end-of-scope destructor 
call for structs that are allocated in heap closures? Why 
introduce this weird, special-purpose hack instead of fixing the 
root cause of the problem?

[1]: https://github.com/dlang/dmd/issues/19119


More information about the dip.ideas mailing list