Uncallable delegates
Timon Gehr
timon.gehr at gmx.ch
Fri May 15 03:13:05 UTC 2026
On 5/15/26 04:42, Walter Bright wrote:
> I don't know which of the assertions should hold, and which ones are
> salient to your conclusion. I have a very small brain.
(There is enough context in my post to understand what is going on, so
you can ask any competent LLM to explain it to you.)
All assertions do pass, which you can confirm by running the example.
(But ChatGPT understands that fact from just my post, without running it.)
I cannot tell you which assertions should pass as the program should not
be accepted by the compiler.
Anyway, there are two groups of assertions:
```d
static assert(is(typeof(p)==int*));
static assert(is(typeof(q)==immutable(int)*));
assert(p is q);
```
This proves that I have created aliasing that should be impossible to
create in `@safe` code.
```d
assert(x == 2);
assert(x == *q);
assert(2 + *q == 5);
```
This satisfies your 2+2==5 constraint. You said unsound is 2+2==5, this
is literally 2+2==5. *q is the same as x, which is the same as 2.
Anyway, here is the example cleaned up a bit, I did not have time to do
this earlier as I had to leave in a hurry:
```d
@safe:
struct T{
int* delegate() dg;
int* q;
}
T foo()pure{
auto x = new int(2);
auto dg = ()=>x;
return T(dg,x);
}
void main(){
immutable ps = foo();
auto p = ps.dg(), q = ps.q;
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