Skip to content

Commit

Permalink
Merge pull request #8526 from JinShil/fix_15373
Browse files Browse the repository at this point in the history
Fix Issue 15373 - Segfault when using typeid on extern(C++) class with virtual functions
merged-on-behalf-of: Jacob Carlborg <jacob-carlborg@users.noreply.github.com>
  • Loading branch information
dlang-bot committed Jul 30, 2018
2 parents 1bb0dce + 76d4781 commit 2de9326
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/dmd/expressionsem.d
Expand Up @@ -4726,13 +4726,22 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
ta.checkComplexTransition(exp.loc, sc);

Expression e;
if (ea && ta.toBasetype().ty == Tclass)
auto tb = ta.toBasetype();
if (ea && tb.ty == Tclass)
{
/* Get the dynamic type, which is .classinfo
*/
ea = ea.expressionSemantic(sc);
e = new TypeidExp(ea.loc, ea);
e.type = Type.typeinfoclass.type;
if (tb.toDsymbol(sc).isClassDeclaration().classKind == ClassKind.cpp)
{
error(exp.loc, "Runtime type information is not supported for `extern(C++)` classes");
e = new ErrorExp();
}
else
{
/* Get the dynamic type, which is .classinfo
*/
ea = ea.expressionSemantic(sc);
e = new TypeidExp(ea.loc, ea);
e.type = Type.typeinfoclass.type;
}
}
else if (ta.ty == Terror)
{
Expand Down
22 changes: 22 additions & 0 deletions test/fail_compilation/test15373.d
@@ -0,0 +1,22 @@
/*
TEST_OUTPUT:
---
fail_compilation/test15373.d(21): Error: Runtime type information is not supported for `extern(C++)` classes
---
*/

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

// Using `typeid` on an `extern(C++) class` type is ok as it is evaluated at compile-time
// See test/runnable/test15373.d

// Using `typeid` on an `extern(C++) class` instance is not ok because `extern(C++) class`
// instances are not rooted in `Object`

extern(C++) class C { }

void foo()
{
C c = new C();
auto ti = typeid(c);
}
15 changes: 15 additions & 0 deletions test/runnable/test15373.d
@@ -0,0 +1,15 @@
// https://issues.dlang.org/show_bug.cgi?id=15373

// Using `typeid` on an `extern(C++) class` type is fine as it is evaluated at compile-time

// Using `typeid` on an `extern(C++) class` instance is not ok because `extern(C++) class`
// instances are not rooted in `Object`. See test/fail_compilation/test15373.d

extern(C++) class C
{ }

void main()
{
auto Cti = typeid(C);
assert(Cti.name == "test15373.C");
}

0 comments on commit 2de9326

Please sign in to comment.