Uncallable delegates

Timon Gehr timon.gehr at gmx.ch
Sun May 17 00:31:07 UTC 2026


On 5/16/26 09:47, Meta wrote:
> 
> What I mean is this:
> ```d
> struct Delegate
> {
>      void* funcptr;
>      void* context;
> }
> ```
> 
> Currently immutable(int* delegate()) means that d.funcptr is immutable, 
> but d.context is still mutable. So my suggestion is that it also make 
> d.context immutable. This would disallow the code with UB that you 
> outlined, because:
> 
> struct T{
>      int* delegate() dg;
>      int* q;
> }
> 
> ..
> 
> immutable ps = foo();
> 
> ps' type is immutable(T), so ps.dg is immutable(int* delegate()), which 
> with my suggested change, "decays" to immutable(int* delegate() 
> immutable), and now the compiler catches this because a delegate with an 
> immutable context pointer is not allowed to access mutable data.

The conversion to `immutable` is allowed because `foo` is a `pure` 
factory function, not because the compiler assumes there is no qualified 
indirection in the delegate type. If you make `foo` not `pure`, the 
conversion is rejected.


More information about the dip.development mailing list