[Issue 1983] Delegates violate const

d-bugmail at puremagic.com d-bugmail at puremagic.com
Thu Feb 4 01:08:05 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=1983

--- Comment #26 from Bolpat <qs.il.paperinik at gmail.com> ---
(In reply to timon.gehr from comment #25)
> > It's even hard to pin-point which exact part of the code should be an error.
> 
> The problem is that you shouldn't be able to call "dg" in "kaboom", as its
> type is `const(void delegate()pure)`. It would have to be at least something
> like `const(void delegate()pure const)`.

My position is that distinguishing delegates for mutability of the context is a
bad idea. Thinking about it for a while, I come to the conclusion the problem
is elsewhere, namely uniqueness deduction is broken:

>         this(int value) pure
>         {
>             this.value = value;
>             this.dg = &this.mut;
>         }

The mere fact that the struct has a delegate field means a reference to it
*can* exist in it. Therefore, the result of the constructor cannot be assumed
to be unique. The implicit cast to immutable is invalid. IMO, the way the type
system currently works, this is the issue.

> Finally, maybe Walter thinks the code snippet has UB anyway as it creates
> internal references into a struct.

As you showed, it can be mitigated using a class instead of a struct. I haven't
tried.

> void main()pure{
>     immutable a=new A(0); // pure constructor, so this is okay

"pure constructor, so this is okay": That's where the bad stuff begins.
Uniqueness isn't just slapping pure on stuff. A pure delegate need not return a
unique result as it can use its context to get its result from. That's
basically what happens here.

Requiring const or immutable annotations on delegates for the context is a
breaking change and hell is it breaking.

--


More information about the Digitalmars-d-bugs mailing list