Uncallable delegates

Timon Gehr timon.gehr at gmx.ch
Thu May 14 21:55:32 UTC 2026


On 5/13/26 22:06, Walter Bright wrote:
> On 5/13/2026 12:32 AM, Dukc wrote:
>> We can conclude that calling `const(void delegate(int))` is 
>> fundamentally unsound. It can't be made to work without type system 
>> breaking casts.
> 
> "Unsound" means it leads to corruption of the type system,

It does.

> such as being able to modify an immutable value. Unsound would mean something like 2+2=5.

DMD v2.112.1

```d
import std;

@safe:
class C{ int x=2; }

Tuple!(int*,immutable(int)*) createBadAliasing(){
     static foo()pure{
         auto c = new C;
         auto dg = ()=>&c.x;
	return tuple(dg,c);
     }
     immutable t = foo();
     return tuple(t[0](),&t[1].x);
}

void main(){
     auto ps = createBadAliasing();
     auto p = ps[0], q = ps[1];
     static assert(is(typeof(p)==int*));
     static assert(is(typeof(q)==immutable(int)*));	
     assert(p is q);
     auto x = *q;
     *p = 3;
     assert(x == 2);
     assert(x == *q);
     assert(2 + *q == 5);
}
```


More information about the dip.development mailing list