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

--- Comment #0 from Harry Vennik <> 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:
------- You are receiving this mail because: -------

Reply via email to