www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 6316] New: Class downcast is rejected in safe code

reply d-bugmail puremagic.com writes:
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



---
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: -------
Jul 14 2011
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=6316


kennytm gmail.com changed:

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



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: -------
Jul 14 2011
prev sibling next sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jul 14 2011
prev sibling parent d-bugmail puremagic.com writes:
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



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: -------
Jul 15 2011