[Issue 1563] New: dynamic cast is not always performed

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Oct 9 19:18:30 PDT 2007


http://d.puremagic.com/issues/show_bug.cgi?id=1563

           Summary: dynamic cast is not always performed
           Product: D
           Version: 1.022
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla at digitalmars.com
        ReportedBy: davidl at 126.com


class A{}
class B:A{}
class C:B{}
void main()
{
  A a = new B;
  C c = cast(C)cast(void*)a;
  c = cast(C)c;
  assert(c is null);  // assertion failure
  c = cast(C)cast(A)cast(void*)c;       // workaround
  assert(c is null);  
}

the problem here is we don't distinguish the dynamic cast with the none dynamic
one.
For dynamic cast, it should always perform the check. And people should use
non-dynamic cast whenever it's possible. Only use dynamic cast only when we're
well informed it *requiring* a dynamic cast.
A warning is appreciated on this if the compiler is going to optimize the
dynamic cast away. And a corresponding option for this warning would be nice.
something like : -WCastToItself


-- 



More information about the Digitalmars-d-bugs mailing list