Jump to page: 1 2
Thread overview
[Issue 3258] New: Calling private or package override methods calls the base implementation
Aug 19, 2009
Stewart Gordon
Aug 19, 2009
Matti Niemenmaa
Aug 20, 2009
Diggory
Jun 04, 2011
Mike Shulman
Dec 21, 2012
Andrej Mitrovic
Dec 21, 2012
Diggory
Dec 21, 2012
Jonathan M Davis
Dec 21, 2012
Andrej Mitrovic
Dec 22, 2012
Andrej Mitrovic
Dec 22, 2012
Stewart Gordon
Dec 22, 2012
Diggory
Dec 22, 2012
Stewart Gordon
August 19, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3258

           Summary: Calling private or package override methods calls the
                    base implementation
           Product: D
           Version: 1.045
          Platform: x86
        OS/Version: Linux
            Status: NEW
          Keywords: accepts-invalid, wrong-code
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: nobody@puremagic.com
        ReportedBy: diggory.hardy@gmail.com


(Same as: http://www.dsource.org/projects/ldc/ticket/355 Exactly the same problem with dmd 1.045, dmd 2.031, ldc 0.9.1.)

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).

{{{
#!d
// 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.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 19, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3258


Stewart Gordon <smjg@iname.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |smjg@iname.com




--- Comment #1 from Stewart Gordon <smjg@iname.com>  2009-08-19 12:41:34 PDT ---
private and package methods aren't virtual and so don't override.  There should be something in the spec to this effect, but I can't seem to find it.  The bug is that the compiler accepts the override attribute on these, simple as that.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 19, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3258


Matti Niemenmaa <matti.niemenmaa+dbugzilla@iki.fi> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |matti.niemenmaa+dbugzilla@i
                   |                            |ki.fi




--- Comment #2 from Matti Niemenmaa <matti.niemenmaa+dbugzilla@iki.fi>  2009-08-19 13:32:06 PDT ---
http://www.digitalmars.com/d/1.0/attribute.html#ProtectionAttribute says: "Private members cannot be overridden."

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3258





--- Comment #3 from Diggory <diggory.hardy@gmail.com>  2009-08-19 23:14:26 PDT ---
With private functions, yes. But I've never heard that package functions can't be virtual. Stewart Gordan, are you saying package functions aren't virtual by design (I hope not), or that this is just the current implementation?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
August 20, 2009
http://d.puremagic.com/issues/show_bug.cgi?id=3258


Jarrett Billingsley <jarrett.billingsley@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jarrett.billingsley@gmail.c
                   |                            |om




--- Comment #4 from Jarrett Billingsley <jarrett.billingsley@gmail.com>  2009-08-19 23:47:28 PDT ---
(In reply to comment #3)
> With private functions, yes. But I've never heard that package functions can't be virtual. Stewart Gordan, are you saying package functions aren't virtual by design (I hope not), or that this is just the current implementation?

Whether it's by design or not is not entirely clear.  The only thing the spec says about 'package' in this regard is that it is basically an extension of 'private', in which case it makes sense (in a strange way) that 'package' would make a method nonvirtual.  But it just seems like it's conflating two entirely different concepts: access and virtuality.  Private methods can and should be nonvirtual, since that's a valid optimization.  But it's entirely reasonable to have a package method that can be overridden.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 04, 2011
http://d.puremagic.com/issues/show_bug.cgi?id=3258


Mike Shulman <viritrilbia+d@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |viritrilbia+d@gmail.com


--- Comment #5 from Mike Shulman <viritrilbia+d@gmail.com> 2011-06-03 20:48:04 PDT ---
Since private methods are visible to the entire module in which they are defined, and thus could reasonably be overridden by derived classes defined in the same module, I don't see why making them all non-virtual is any more valid of an optimization, in principle, than doing the same for package methods.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3258


Andrej Mitrovic <andrej.mitrovich@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |andrej.mitrovich@gmail.com
            Version|D1 & D2                     |D1


--- Comment #6 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-21 10:39:11 PST ---
I don't know the state of D1 but in D2 these overloads are flagged as errors, so I'm removing D2 from the list. As for non-virtual private/package - this should be an enhancement request, but it's probably been debated many times already (I personally believe access specifiers should not mess with the virtuality of a method).

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3258



--- Comment #7 from Diggory <diggory.hardy@gmail.com> 2012-12-21 11:19:38 PST ---
Andrej, that seems like a funny change to me. Unless D2 either allows virtual package functions or states that these are not allowed it seems that it's still broken to me. The spec says "All non-sta­tic non-pri­vate non-tem­plate mem­ber func­tions are vir­tual." which I interpret to include package members.

Not that I particularly care any more...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3258


Jonathan M Davis <jmdavisProg@gmx.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jmdavisProg@gmx.com


--- Comment #8 from Jonathan M Davis <jmdavisProg@gmx.com> 2012-12-21 11:53:54 PST ---
It's been discussed many times, and the spec is wrong in this case (I keep meaning to update it but haven't done so yet). public and protected member functions are _always_ virtual unless the compiler can determine that they're never overridden (which can only happen when they're final). private and package member functions are _never_ virtual.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
December 21, 2012
http://d.puremagic.com/issues/show_bug.cgi?id=3258



--- Comment #9 from Andrej Mitrovic <andrej.mitrovich@gmail.com> 2012-12-21 15:09:12 PST ---
(In reply to comment #7)
>

Yeah I know, the spec tells one thing on one page, then another thing on another. It says "Package extends private" on the protection attributes page, implying it's final like private.

Small note: In terms of the compiler implementation, the private and package set to non-virtual boils down to a single `if` check in the front-end.

Forcing private and package non-virtual must have a rationale in the spec. There isn't one, so Walter and Andrei should state exactly why this decision was made.

Actually, Walter and Andrei obviously had a miscommunication, because Andrei expected private to allow being virtual in TDPL. And 'package' being non-virtual isn't mentioned in TDPL either (lots of people get confused by this). So maybe Walter is the one to write the rationale.

I also believe it's not too late to change the state of things. We have the `final` keyword after all. Yes, making private and package virtual could potentially slow down code, but fixing it is trivial, it just requires a `final` keyword. Perhaps the the compiler can be made smart enough to optimize non-overriden private/package methods so they're made final. Maybe if -lib or -c are not used it can do such optimizations.

It's ironic that the argument against private/package being virtual is because of performance reasons when we've already made the mistake of making all methods virtual by default. I *still* can't get used to the fact that a public method is virtual by default, it really lacks that documentation aspect of having a 'virtual' keyword right next to it.

And as argumented before, you could easily forget to mark something as 'final' in a library, the user overriding such a method, followed by an API update where you do a bugfix and mark it as final, which ends up breaking the user code. Unfortunately this also becomes an argument against making private/package virtual by default too.

Non-virtual by default + a virtual keyword + no limits on virtuality based on access specifiers => dream come true (for me).

We're in an odd status-quo, all I can do is sigh at the situation.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
« First   ‹ Prev
1 2