Bug 3757 – Overloading const function with overridden non-const function results in seg fault.

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2010-01-30T19:30:00Z
Last change time
2012-01-28T14:05:08Z
Keywords
wrong-code
Assigned to
nobody
Creator
rayerd.wiz
Depends on
3282

Comments

Comment #0 by rayerd.wiz — 2010-01-30T19:30:03Z
import std.stdio; class Base { string f() { return "Base.f()"; } } class Derived : Base { string f() { return "Derived.f()"; } string f() const { return "Derived.f() const"; } } void main() { auto x = new Base; writeln(x.f()); auto y = new Derived; writeln(y.f());// calls "Derived.f() const", but it is expected that be called non-const. auto z = new const(Derived); writeln(z.f()); }
Comment #1 by schveiguy — 2011-10-31T08:02:36Z
This is neither invalid nor fixed. Tested in Linux 32-bit. Tested with 2.055: Base.f() Derived.f() const Derived.f() const Tested with 2.056: Base.f() Derived.f() Segmentation fault Clearly there is some invalid vtable stuff going on here. Probably was going on before, but manifested in a different way. If I remove the base class from the picture, it correctly prints out: Derived.f() Derifed.f() const Expected printout for original code should be: Base.f() Derived.f() Derived.f() const
Comment #2 by bugzilla — 2012-01-28T00:17:42Z
This is an undiagnosed error. Both Derived.f functions override Base.f.
Comment #3 by github-bugzilla — 2012-01-28T00:23:19Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/588feaeaee1342984f5f53be762fdbc198cd112d fix Issue 3757 - Overloading const function with overridden non-const function results in seg fault.
Comment #4 by github-bugzilla — 2012-01-28T00:24:10Z
Commit pushed to dmd-1.x at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/04e632617be33cce3e92b5d48a7fa20a6ca621fb fix Issue 3757 - Overloading const function with overridden non-const function results in seg fault.
Comment #5 by github-bugzilla — 2012-01-28T00:24:41Z
Commit pushed to master at https://github.com/D-Programming-Language/dmd https://github.com/D-Programming-Language/dmd/commit/470080a7735309b804920954b892ef615bb972de fix Issue 3757 - Overloading const function with overridden non-const function results in seg fault.
Comment #6 by timon.gehr — 2012-01-28T13:21:17Z
(In reply to comment #2) > This is an undiagnosed error. Both Derived.f functions override Base.f. The first Derived.f overrides Base.f, the second is an additionally introduced overload. According to http://www.d-programming-language.org/function , this should not be a compile error but behave as stated in the bug report.
Comment #7 by timon.gehr — 2012-01-28T14:05:08Z
On second thought, I might be wrong. The spec states: - "Virtual functions all have a hidden parameter called the this reference, which refers to the class object for which the function is called." - "A functions in a derived class with the same name and parameter types as a function in a base class overrides that function:" If the type of the hidden parameter was included in the "parameter types" part of the second statement, no function would override another, therefore it apparently is not. Therefore the commit indeed fixed the discrepancy with the spec. I think the design should be revisited at some point to make overriding more intuitive and powerful.