Bug 15866 – casting away const from "super" results in "this"

Status
RESOLVED
Resolution
INVALID
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
Windows
Creation time
2016-04-03T13:36:00Z
Last change time
2016-04-03T16:39:36Z
Keywords
wrong-code
Assigned to
nobody
Creator
r.sagitario

Comments

Comment #0 by r.sagitario — 2016-04-03T13:36:26Z
class A { int foo(bool) { return 'a'; } } class B : A { override int foo(bool b) const { return b ? 'b' : (cast()super).foo(b); } } void main() { B b = new B; assert(b.foo(false) == 'a'); } This code causes a stack overflow because (cast()super).foo(b) calls B.foo, not A.foo. I've found this pattern while debugging something else in OutOfMemoryError.toString. It is also used in std.xml.Document.toHash, so maybe issue 14966 is related.
Comment #1 by r.sagitario — 2016-04-03T16:39:36Z
The spec says: "If a member function is called with an explicit reference to super, a non-virtual call is made." I guess "explicit" doesn't include being part of an expression, so this bug report is invalid. The correct call is (cast()this).A.foo(b) We'll have to fix it in druntime/phobos instead.