Bug 5171 – Prevent compiling of class when @disable is used on an overriding function

Status
RESOLVED
Resolution
DUPLICATE
Severity
enhancement
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
Other
OS
Windows
Creation time
2010-11-05T10:15:28Z
Last change time
2020-03-21T03:56:39Z
Assigned to
No Owner
Creator
Jesse Phillips

Comments

Comment #0 by Jesse.K.Phillips+D — 2010-11-05T10:15:28Z
When overriding a function of a base class @disable does not cause compile time errors when called. class A { @disable override equals_t opEquals(Object other) { return false; } } void main() { auto a = new A(); auto b = new A(); if(a == b) assert(0); }
Comment #1 by schveiguy — 2010-11-05T11:00:33Z
This isn't actually possible. What I would suggest is the compiler failing to compile your class instead, because you can't disable a base function. If for example, you have a function like this: bool foo(Object o1, Object o2) {...} Then would it be safe to assume that you could pass both a and b to foo? If so, then isn't it possible for foo to call o1 == o2? And how could the compiler possibly statically disable this? I'm going to mark it as invalid, and if you think you'd rather have the behavior where @disable doesn't compile on overridden functions, then you can reopen with that description.
Comment #2 by bearophile_hugs — 2010-11-05T11:15:45Z
(In reply to comment #1) > This isn't actually possible. What I would suggest is the compiler failing to > compile your class instead, because you can't disable a base function. I agree. Where possible a good compiler has to statically disallow impossible code :-)
Comment #3 by bearophile_hugs — 2010-11-05T11:17:54Z
Added a note to bug 3934
Comment #4 by Jesse.K.Phillips+D — 2010-11-09T11:10:36Z
class A { void hello() { } } class B : A { @disable override void hello() { } } void main() { auto a = new A(); A b = new B(); b.hello(); } The compiler should not compile the class saying something to the effect of: Can not disable method hello in base class A from B. Or another suggestion "Cannot @disable overriding function hello in B" Note that I think the code below should still compile: class A { @disable void hello() { } } class B : A { override void hello() { } } void main() { auto a = new A(); B b = new B(); b.hello(); }
Comment #5 by b2.temp — 2017-02-25T18:26:46Z
*** This issue has been marked as a duplicate of issue 6760 ***