Wiki Roadmap Timeline Tickets New Ticket Source Search Help / Guide About Trac Login

Ticket #355 (new defect)

Opened 2 years ago

Last modified 2 years ago

Calling private or package override methods calls the base implementation

Reported by: Cyborg16 Assigned to: ChristianK
Priority: major Milestone:
Component: unspecified Version: release 0.9.1
Keywords: Cc:

Description

Probably related to #354, when overriding methods with an implementation in a base class, from a reference of type base class, the wrong implementations get called.

OK, so overriding private functions is illegal according to the spec, but since the compiler doesn't complain, I included those to show the similarity between package and private functions (same error with both).

// Declare and define some methods...
class A {
    private void iPriv ()	{ assert(false, "iPriv"); }
    protected void iProt ()	{ assert(false, "iProt"); }
    package void iPack ()	{ assert(false, "iPack"); }
    public void iPub ()		{ assert(false, "iPub"); }
}

// ... but override them all here:
class B : A {
    private override void iPriv() {}	// (lesser issue: shouldn't the compiler complain here?)
    protected override void iProt() {}
    package override void iPack() {}
    public override void iPub() {}
}

void main() {
    // None of these assert of course (overriden functions are called):
    B b = new B;
    b.iPriv;
    b.iProt;
    b.iPack;
    b.iPub;
    
    // But when we call from a base class with an implementation:
    A a = b;
    a.iPriv;	// This calls A.iPriv and asserts
    a.iProt;
    a.iPack;	// This calls A.iPack and asserts
    a.iPub;
}

Unless the a.iPriv; and a.iPack; calls are removed, the program asserts when run.

  1. With private functions this is nearly OK, but the compiler should complain about trying to override them instead of silently calling the base implementation.
  2. With package protection, the wrong implementation of iPack() is called.

Change History

08/19/09 02:32:35 changed by Cyborg16

DMD bug report (identical): http://d.puremagic.com/issues/show_bug.cgi?id=3258 Exactly the same problem with dmd 1.045, dmd 2.031, ldc 0.9.1.

Copyright © 2008, LDC Development Team.