Thread overview
[Issue 6316] New: Class downcast is rejected in @safe code
Jul 14, 2011
Harry Vennik
Jul 14, 2011
kennytm@gmail.com
[Issue 6316] Regression(2.054): Class downcast is rejected in @safe code
Jul 14, 2011
yebblies
Jul 15, 2011
Walter Bright
July 14, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6316

           Summary: Class downcast is rejected in @safe code
           Product: D
           Version: D2
          Platform: x86
        OS/Version: Mac OS X
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: htvennik@gmail.com


--- Comment #0 from Harry Vennik <htvennik@gmail.com> 2011-07-14 06:30:27 PDT ---
The D language specification does not forbid downcasting in @safe code, but the compiler rejects it. Downcasting is actually safe because such casts are checked run-time.

The minimal testcase is:

------- CODE -------
class A { }
class B : A { }

@safe void main()
{
    A a = new A();
    B b = cast(B) a;
}
----- END CODE -----


The above code is rejected by the compiler with the following message: safe_downcast.d(7): Error: cast from safe_downcast.A to safe_downcast.B not allowed in safe code



Possible enhancement: make DMD issue a warning if the result of a downcast is not checked for null.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 14, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6316


kennytm@gmail.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |kennytm@gmail.com


--- Comment #1 from kennytm@gmail.com 2011-07-14 06:46:17 PDT ---
You should use std.conv.to, and then in Phobos the toImpl specialization

T toImpl(T, S)(S value)
    if (!isImplicitlyConvertible!(S, T) &&
        is(S : Object) && !is(typeof(value.opCast!T()) : T) &&
        is(T : Object) && !is(typeof(new T(value))))

should be made @trusted.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 14, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6316


yebblies <yebblies@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch, rejects-valid
                 CC|                            |yebblies@gmail.com
           Platform|x86                         |All
            Summary|Class downcast is rejected  |Regression(2.054): Class
                   |in @safe code               |downcast is rejected in
                   |                            |@safe code
         OS/Version|Mac OS X                    |All


--- Comment #2 from yebblies <yebblies@gmail.com> 2011-07-15 00:16:42 EST ---
https://github.com/D-Programming-Language/dmd/pull/252

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
July 15, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=6316


Walter Bright <bugzilla@digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla@digitalmars.com
         Resolution|                            |FIXED


--- Comment #3 from Walter Bright <bugzilla@digitalmars.com> 2011-07-15 16:19:42 PDT ---
https://github.com/D-Programming-Language/dmd/commit/48b8cee356ef3b3681df26129d7b3f4d9dd9d34c

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------