D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 5171 - Prevent compiling of class when @disable is used on an overriding function
Summary: Prevent compiling of class when @disable is used on an overriding function
Status: RESOLVED DUPLICATE of issue 6760
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: Other Windows
: P2 enhancement
Assignee: No Owner
URL: http://www.digitalmars.com/d/2.0/attr...
Keywords:
Depends on:
Blocks:
 
Reported: 2010-11-05 10:15 UTC by Jesse Phillips
Modified: 2020-03-21 03:56 UTC (History)
3 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Jesse Phillips 2010-11-05 10:15:28 UTC
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 Steven Schveighoffer 2010-11-05 11:00:33 UTC
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 bearophile_hugs 2010-11-05 11:15:45 UTC
(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 bearophile_hugs 2010-11-05 11:17:54 UTC
Added a note to bug 3934
Comment 4 Jesse Phillips 2010-11-09 11:10:36 UTC
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 basile-z 2017-02-25 18:26:46 UTC

*** This issue has been marked as a duplicate of issue 6760 ***