www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 11722] New: Qualifier-only casts should not invoke opCast

reply d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722

           Summary: Qualifier-only casts should not invoke opCast
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: wrong-code
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: k.hara.pg gmail.com


--- Comment #0 from Kenji Hara <k.hara.pg gmail.com> 2013-12-10 17:17:25 PST ---
This is a spin-off issue from bug 5747.

D has a cast syntax for modifying just only the type qualifier of the operand.

http://dlang.org/expression#CastExpression

CastExpression:
    cast ( Type ) UnaryExpression
    cast ( CastQual ) UnaryExpression   <--
    cast ( ) UnaryExpression            <--

However, if the operand is class or struct, and has opCast member function, the
qualifier-cast cast(qual)exp is translated to cast(qual(typeof(exp))exp, then
it will unintendedly invoke opCast. For example, following code wrongly asserts
in C.opCast.

class C
{
    T opCast(T)() { assert(0); }
}
void main()
{
    C c = new C();
    shared C sc = cast(shared)c;
}

I think this is not good behavior in generic code, and should be fixed.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2013
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull


--- Comment #1 from Kenji Hara <k.hara.pg gmail.com> 2013-12-10 17:43:11 PST ---
https://github.com/D-Programming-Language/dmd/pull/2946

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 10 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722



--- Comment #2 from github-bugzilla puremagic.com 2013-12-11 00:50:41 PST ---
Commits pushed to master at https://github.com/D-Programming-Language/dmd

https://github.com/D-Programming-Language/dmd/commit/dab021c5b716b3a6eedbb1d883ce55f084b98aca
fix Issue 11722 - Qualifier-only casts should not invoke opCast

https://github.com/D-Programming-Language/dmd/commit/b32a5205390f69e3fc72b3acb912306f22616fbf
Merge pull request #2946 from 9rnsr/fix11722

Issue 11722 - Qualifier-only casts should not invoke opCast

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722


Max Samukha <samukha voliacable.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |samukha voliacable.com


--- Comment #3 from Max Samukha <samukha voliacable.com> 2013-12-11 04:08:25
PST ---
I find the existing behavior reasonable enough, provided it is
cast(qual(unqual(typeof(exp)))exp instead of cast(qual(typeof(exp))exp.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |FIXED


-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722



--- Comment #4 from Max Samukha <samukha voliacable.com> 2013-12-11 12:20:07
PST ---
I disagree. That was not necessary, unless you prove otherwise.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Dec 11 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722



--- Comment #5 from monarchdodra gmail.com 2013-12-12 23:34:46 PST ---
(In reply to comment #0)
 This is a spin-off issue from bug 5747.
 
 D has a cast syntax for modifying just only the type qualifier of the operand.
 
 http://dlang.org/expression#CastExpression
 
 CastExpression:
     cast ( Type ) UnaryExpression
     cast ( CastQual ) UnaryExpression   <--
     cast ( ) UnaryExpression            <--
Kenji: I'm wondering: Your original post seemed to imply the enhancement was *only* for qualifier cast. However, the final result seems to be for *any* kind of cast, whith matching types. I mean: const(S) s; auto a = cast() s; auto b = cast(S) s; In the case of "a": We have a "CastQual" type cast, and this bypasses the opCast operator entirelly. In the case of "b": We have a *Type* cast, but that happens to result only in a qualifier change. In the case of "b", *should* we call opCast? Please clarify. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 12 2013
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722



--- Comment #6 from Kenji Hara <k.hara.pg gmail.com> 2013-12-12 23:44:33 PST ---
(In reply to comment #5)
 (In reply to comment #0)
 This is a spin-off issue from bug 5747.
 
 D has a cast syntax for modifying just only the type qualifier of the operand.
 
 http://dlang.org/expression#CastExpression
 
 CastExpression:
     cast ( Type ) UnaryExpression
     cast ( CastQual ) UnaryExpression   <--
     cast ( ) UnaryExpression            <--
Kenji: I'm wondering: Your original post seemed to imply the enhancement was *only* for qualifier cast. However, the final result seems to be for *any* kind of cast, whith matching types. I mean: const(S) s; auto a = cast() s; auto b = cast(S) s; In the case of "a": We have a "CastQual" type cast, and this bypasses the opCast operator entirelly. In the case of "b": We have a *Type* cast, but that happens to result only in a qualifier change. In the case of "b", *should* we call opCast? Please clarify.
Yes. And actually, with git-head, following code asserts only in the case of "b". struct S { T opCast(T)() const { assert(0); } } void main() { const(S) s; auto a = cast() s; // bypass opCast auto b = cast(S) s; // call opCast and assert } -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 12 2013
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=11722



--- Comment #7 from monarchdodra gmail.com 2013-12-12 23:49:57 PST ---
(In reply to comment #6)
 (In reply to comment #5)
 (In reply to comment #0)
 This is a spin-off issue from bug 5747.
 
 D has a cast syntax for modifying just only the type qualifier of the operand.
 
 http://dlang.org/expression#CastExpression
 
 CastExpression:
     cast ( Type ) UnaryExpression
     cast ( CastQual ) UnaryExpression   <--
     cast ( ) UnaryExpression            <--
Kenji: I'm wondering: Your original post seemed to imply the enhancement was *only* for qualifier cast. However, the final result seems to be for *any* kind of cast, whith matching types. I mean: const(S) s; auto a = cast() s; auto b = cast(S) s; In the case of "a": We have a "CastQual" type cast, and this bypasses the opCast operator entirelly. In the case of "b": We have a *Type* cast, but that happens to result only in a qualifier change. In the case of "b", *should* we call opCast? Please clarify.
Yes. And actually, with git-head, following code asserts only in the case of "b".
Ah. Oops. I was testing the wrong code. Thanks. -- Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Dec 12 2013