www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 3282] New: The overload and override issue of const/immutable member functions

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282

           Summary: The overload and override issue of const/immutable
                    member functions
           Product: D
           Version: 2.031
          Platform: Other
        OS/Version: Windows
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: rayerd.wiz gmail.com


import std.stdio;
class Base
{
    string f()
    {
        return "Base.f()";
    }
}
class Derived : Base
{
    string f()
    {
        return "Derived.f()";
    }
    string f() const // or immutable
    {
        return "Derived.f() const";
    }
}
void main()
{
    auto x = new Base;
    writeln(x.f());
    auto y = new Derived;
    writeln(y.f());
    auto z = new const(Derived); // or immutable
    writeln(z.f()); //object.Error: Access Violation
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Sep 03 2009
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282




PDT ---
This problem occurs when "const" is "shared".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Oct 19 2009
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Rainer Schuetze <r.sagitario gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.sagitario gmx.de



PST ---
This kind of works now with changeset 344. But both "f()" and "f() const"
overload "f()" in the base class, and the last one wins. I think dmd should
either issue an error or add another entry to the vtbl for the non-exact but
covariant match.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 22 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282




PST ---
This problem's characteristics changed at dmd2.040.

import std.stdio;
class Base
{
    string f()
    {
        return "Base.f()";
    }
}
class Derived : Base
{
    string f()
    {
        return "Derived.f()";
    }
    string f() immutable
    {
        return "Derived.f() immutable";
    }
    string f() shared
    {
        return "Derived.f() shared";
    }
}
void main()
{
    auto x = new Base;
    writeln(x.f());
    auto y = new Derived;
    writeln(y.f());
    auto z = new immutable(Derived);
        //main.d(15): Error: function main.Derived.f of type immutable string()
overrides but is not covariant with main.Base.f of type string()
    writeln(z.f());
    auto w = new shared(Derived);
        //main.d(19): Error: function main.Derived.f of type shared string()
overrides but is not covariant with main.Base.f of type string()
    writeln(w.f());
}

Are both errors correctly?
I expected that z.f() calls "Derived.f() immutable" and w.f() calls
"Derived.f() shared".

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 30 2010
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |bugzilla digitalmars.com
         Resolution|                            |FIXED



01:28:50 PST ---
I believe the errors are correct.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|FIXED                       |WORKSFORME


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
                 CC|                            |timon.gehr gmx.ch
         Resolution|WORKSFORME                  |



I think it is a bug. The derived class introduces two additional overloads. The
compiler claims that all three overloads override the same function.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID



15:47:44 PST ---

 I think it is a bug. The derived class introduces two additional overloads. The
 compiler claims that all three overloads override the same function.
The error messages are deliberate. -- Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email ------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |



If so, why is this code accepted?

class A{
    void f(int){}
}
class B: A{
    override void f(int){}
    void f(immutable int){}
    void f(shared int){}
}

What is the point of deliberately treating the hidden this pointer special
regarding overloading?

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282




Furthermore, this works, of course:

class B{
    void f(int){}
    void f(int)immutable{}
    void f(int)shared{}
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Walter Bright <bugzilla digitalmars.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |INVALID



19:36:45 PST ---
The return types are the issue. You cannot, for example, override a function
that returns a mutable array with one that returns an immutable one. It would
be a giant hole in the type system.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 23 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


timon.gehr gmx.ch changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |



The signature of the overriding function is identical to the overridden one in
all cases discussed here.

class A{
    void f(){} // note: return type void
}
class B: A{
    override void f(){} // overrides base f                                     
    void f()immutable{} // new overload                                         
    void f()shared{}       // new overload                                      
}

It is impossible to break the type system (even if the return types would be
different): the new overloads are not even reachable through a base class
reference. The bug is that the compiler does not take into account the storage
class of the hidden this pointer while deciding what overrides what.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 24 2012
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |pull



---
https://github.com/D-Programming-Language/dmd/pull/779

I also think this never breaks type systems, and additional overloads is
useful.
e.g. http://d.puremagic.com/issues/show_bug.cgi?id=7534#c6

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Mar 03 2012
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=3282


Kenji Hara <k.hara.pg gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|REOPENED                    |RESOLVED
         Resolution|                            |FIXED


-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jun 06 2012