Jump to page: 1 2
Thread overview
[Issue 9149] Disallow converting delegates to const
Nov 17, 2015
Sobirari Muhomori
Nov 17, 2015
timon.gehr@gmx.ch
May 24, 2016
Sobirari Muhomori
May 24, 2016
timon.gehr@gmx.ch
May 24, 2016
timon.gehr@gmx.ch
May 25, 2016
Sobirari Muhomori
May 25, 2016
Sobirari Muhomori
May 25, 2016
timon.gehr@gmx.ch
May 25, 2016
timon.gehr@gmx.ch
May 25, 2016
timon.gehr@gmx.ch
May 25, 2016
timon.gehr@gmx.ch
May 26, 2016
Sobirari Muhomori
Dec 17, 2022
Iain Buclaw
Feb 02, 2023
Bolpat
[Issue 9149] Disallow calling const delegates with a mutable context
Jan 11, 2024
timon.gehr@gmx.ch
Jan 11, 2024
timon.gehr@gmx.ch
Jan 13, 2024
Walter Bright
November 17, 2015
https://issues.dlang.org/show_bug.cgi?id=9149

Sobirari Muhomori <dfj1esp02@sneakemail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #1 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
Nothing mutable is accessible through a const reference.

*** This issue has been marked as a duplicate of issue 1983 ***

--
November 17, 2015
https://issues.dlang.org/show_bug.cgi?id=9149

timon.gehr@gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|DUPLICATE                   |---

--- Comment #2 from timon.gehr@gmx.ch ---
This is not a duplicate.

Issue 1983 is a completely unrelated bug in the type checker (note that it will no longer compile if one removes the const overload).

--
May 24, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #3 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
How about this?

class A
{
    int i;
    const void delegate() dg;
    this() pure { dg=&f; }
    void f(){ i++; }
}

unittest
{
    const A a = new A;
    a.dg();
}

--
May 24, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #4 from timon.gehr@gmx.ch ---
(In reply to Sobirari Muhomori from comment #3)
> How about this?
> 
> class A
> {
> 	int i;
> 	const void delegate() dg;
> 	this() pure { dg=&f; }
> 	void f(){ i++; }
> }
> 
> unittest
> {
> 	const A a = new A;
> 	a.dg();
> }

The assignment in the constructor shouldn't compile.

--
May 24, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #5 from timon.gehr@gmx.ch ---
(In reply to timon.gehr from comment #4)
> (In reply to Sobirari Muhomori from comment #3)
> > How about this?
> > 
> > class A
> > {
> > 	int i;
> > 	const void delegate() dg;
> > 	this() pure { dg=&f; }
> > 	void f(){ i++; }
> > }
> > 
> > unittest
> > {
> > 	const A a = new A;
> > 	a.dg();
> > }
> 
> The assignment in the constructor shouldn't compile.

And indeed, DMD rejects the assignment.

--
May 25, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #6 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
really? https://dpaste.dzfl.pl/ba8fdf6711e3

--
May 25, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #7 from Sobirari Muhomori <dfj1esp02@sneakemail.com> ---
(In reply to timon.gehr from comment #0)
> Reading a mutable delegate out of a const reference

For there to be mutable data accessible through a const reference, the const qualifier should not be transitive?

--
May 25, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #8 from timon.gehr@gmx.ch ---
(In reply to Sobirari Muhomori from comment #6)
> really? https://dpaste.dzfl.pl/ba8fdf6711e3

No, you are right, DMD accepts it even though it is invalid. Not sure why I claimed otherwise (maybe I accidentally tested with the wrong compiler).

Anyway, void delegate() shouldn't convert to const(void delegate()) implicitly.

--
May 25, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

--- Comment #9 from timon.gehr@gmx.ch ---
(In reply to Sobirari Muhomori from comment #7)
> (In reply to timon.gehr from comment #0)
> > Reading a mutable delegate out of a const reference
> 
> For there to be mutable data accessible through a const reference, the const qualifier should not be transitive?
 i
What I meant there is that the type of the field is a mutable delegate type
(and hence it could in principle contain a mutable delegate), which is accessed
through a const 'this' reference.

The issue is that transitivity of const is currently broken for delegate types. The access must be banned in order to ensure mutable data is not accessible through a const reference. Otherwise delegates provide a backdoor for converting const data back to mutable. (I don't think this is much of an issue, but the designed semantics are that const should be transitive.)

--
May 25, 2016
https://issues.dlang.org/show_bug.cgi?id=9149

Steven Schveighoffer <schveiguy@yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |schveiguy@yahoo.com

--- Comment #10 from Steven Schveighoffer <schveiguy@yahoo.com> ---
I think the delegate assignment should be legal. The auto-conversion from pure ctor to immutable should be illegal (note that converting to const is not a good test case, because you can easily assign an A to a const A without purity), because casting the A to immutable does not affect the 'this' pointer of the delegate member, and that could point at the given object that you are casting or any member. I had thought that the 'this' pointer was const-agnostic. That is:

class C
{
   void foo() {}
   void ifoo() immutable {}
}

void main()
{
   mc = new C;
   ic = new C;
   void delegate() x = &mc.foo;
   x = &mc.ifoo; // why not?
}

Why does it matter if x's delegate has an immutable 'this'? that detail is insignificant to the caller.

--
« First   ‹ Prev
1 2