www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 21321] New: Class with unimplemented interface method

https://issues.dlang.org/show_bug.cgi?id=21321

          Issue ID: 21321
           Summary: Class with unimplemented interface method compiles,
                    links, then segfaults, if inherited through abstract
                    base class
           Product: D
           Version: D2
          Hardware: x86_64
                OS: Linux
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: eiderdaus gmail.com

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.

--
Oct 17 2020