Bug 12636 – extern(C++) class that implements D interface segfaults
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86_64
OS
All
Creation time
2014-04-24T17:36:00Z
Last change time
2015-02-18T03:36:33Z
Keywords
wrong-code
Assigned to
nobody
Creator
schveiguy
Comments
Comment #0 by schveiguy — 2014-04-24T17:36:48Z
Example:
import std.stdio;
interface D
{
void foo();
}
extern(C++) class C : D
{
extern(D) override void foo() { writeln("C foo");}
}
void main()
{
auto c = new C;
c.foo(); // works
D d = c;
d.foo(); // segfault
}
Note, having IUnknown classes (COM classes) that implement D interfaces work properly. I think this should work.
Comment #1 by schveiguy — 2014-04-24T18:09:09Z
Tested on Windows and MacOSX 64 bit.
Comment #2 by yebblies — 2014-07-25T10:09:06Z
I'm not sure this _can_ work. Converting from a class reference to an interface reference requires adjusting the this pointer so it picks up the correct vtable (IIRC) and you can't do this without classinfo (again IIRC).
There is no such thing as COM classes, on COM interfaces, and COM classes would have the same problem. A D class implementing an extern(C++) interface should work.
If so, this should probably be rejected at compile time.
Comment #3 by schveiguy — 2014-07-28T14:07:55Z
A class that implements a COM interface, has default linkage other than extern(D). I thought it was similar to how extern(C++) works.
I don't know what the internal difference is between extern(C++) classes and D classes, but I can understand if we cannot implement both ABIs in the same class. I have to trust you on that one :) TBH, I'm not really interested in making this work, it was a test I ran to see if it did after telling someone on d.learn that you can't create C++ classes in D, and you corrected me.
Feel free to overtake this bug as an "Accepts invalid".
Comment #4 by yebblies — 2014-07-28T14:14:17Z
(In reply to Steven Schveighoffer from comment #3)
> A class that implements a COM interface, has default linkage other than
> extern(D). I thought it was similar to how extern(C++) works.
extern(C++) interfaces work the same as COM interfaces except for linkage. Classes on the other hand...
>
> I don't know what the internal difference is between extern(C++) classes and
> D classes, but I can understand if we cannot implement both ABIs in the same
> class. I have to trust you on that one :)
Yeah, either the first vtable entry is the classinfo pointer or not, there isn't much middle ground there.
> TBH, I'm not really interested in
> making this work, it was a test I ran to see if it did after telling someone
> on d.learn that you can't create C++ classes in D, and you corrected me.
>
> Feel free to overtake this bug as an "Accepts invalid".
At the very least it's a diagnostic issue. Should be easy enough to detect.