Bug 6316 – Regression(2.054): Class downcast is rejected in @safe code

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2011-07-14T06:30:00Z
Last change time
2011-07-15T16:19:42Z
Keywords
patch, rejects-valid
Assigned to
nobody
Creator
htvennik

Comments

Comment #0 by htvennik — 2011-07-14T06:30:27Z
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.
Comment #1 by kennytm — 2011-07-14T06:46:17Z
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.
Comment #2 by yebblies — 2011-07-14T07:16:42Z
Comment #3 by bugzilla — 2011-07-15T16:19:42Z