Class with unimplemented interface/implementing class private (reminder)

Started by Forestidia86, May 01, 2021, 09:11:28 PM

Previous topic - Next topic

0 Members and 2 Guests are viewing this topic.

Forestidia86

Nasty compiler bug as a reminder:
Class with unimplemented interface method compiles, links, then segfaults, if inherited through abstract base class -  Bugzilla 21321

"DMD 2.094.0 on 64-bit Linux.

    interface I {
        int f();
    }

    abstract class A : I {
    }

    class B : A {
    }

    void main()
    {
        I i = new B();
        i.f();
    }


This program compiles, links, and then segfaults at runtime once i.f() is called. The call to i.f() is necessary to trigger the segfault.

Expected instead: This program fails to compile.

The compiler should recognize class B as wrongly implemented because B doesn't implement int f(). This definition of class B shouldn't compile. (Or, if you disagree whether the definition of class B should compile, then, at the very least, the compiler should recognize B as abstract and the line "I i = new B();" should error. But I encourage that this empty definition of B itself be an error.)

The impact of this bug is that programs will compile even though their types do not satisfy their interfaces. This breaks a basic promise of the type system: We shouldn't have to call all possible methods in all possible derived classes at runtime merely to find what we should implement.

Workaround: Write "class B : A, I" instead of "class B : A", then we get the correct compiler error already for the definition of the class, even when we delete all code in main().

Related but different bug: "Unimplemented methods of interface are not reported as errors during compilation."
https://issues.dlang.org/show_bug.cgi?id=21184
In that bug, the program compiles, but fails to link."