February 03, 2021
https://issues.dlang.org/show_bug.cgi?id=21607

          Issue ID: 21607
           Summary: dmd segfaults for forward reference in base class
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody@puremagic.com
          Reporter: moonlightsentinel@disroot.org

Reduced example from https://github.com/adamdruppe/arsd/blob/a8ec499f2b24915cb27a660088024a6b23f5b04b/jni.d#L191-L193:

class CharSequence : JavaClass!CharSequence
{
        string toString();
}

class JavaClass(Class)
{
        alias m = __traits(getMember, Class, "toString");
}

DMD crashes because it tries to access the vtable when it's still empty:

Stack trace generated by inserting `assert(cd.vtbl.length);`:

core.exception.AssertError@src/dmd/dsymbolsem.d(3769): Assertion failure
----------------
??:? _d_assertp [0x7f31b82062d5]
src/dmd/dsymbolsem.d:3769
_ZN22DsymbolSemanticVisitor23funcDeclarationSemanticEP15FuncDeclaration
[0x7f31b7f9781e]
src/dmd/dsymbolsem.d:4165 _ZN22DsymbolSemanticVisitor5visitEP15FuncDeclaration
[0x7f31b7f98c80]
src/dmd/func.d:2634 _ZN15FuncDeclaration6acceptEP7Visitor [0x7f31b8033435]
src/dmd/dsymbolsem.d:581 _Z15dsymbolSemanticP7DsymbolP5Scope [0x7f31b7f8d6d9]
src/dmd/typesem.d:4356 dmd.expression.Expression
dmd.typesem.dotExp(dmd.mtype.Type, dmd.dscope.Scope*,
dmd.expression.Expression, dmd.identifier.Identifier,
int).visitClass(dmd.mtype.TypeClass) [0x7f31b80ce89c]
src/dmd/typesem.d:4412 dmd.expression.Expression
dmd.typesem.dotExp(dmd.mtype.Type, dmd.dscope.Scope*,
dmd.expression.Expression, dmd.identifier.Identifier, int) [0x7f31b80c9ff6]
src/dmd/expressionsem.d:12057 dmd.expression.Expression
dmd.expressionsem.semanticY(dmd.expression.DotIdExp, dmd.dscope.Scope*, int)
[0x7f31b802b9fa]
src/dmd/expressionsem.d:6313 _ZN25ExpressionSemanticVisitor5visitEP8DotIdExp
[0x7f31b801a0fd]
src/dmd/expression.d:4720 _ZN8DotIdExp6acceptEP7Visitor [0x7f31b7fff651]
src/dmd/expressionsem.d:11655 _Z18expressionSemanticP10ExpressionP5Scope
[0x7f31b802a2b3]
src/dmd/traits.d:1041 dmd.expression.Expression
dmd.traits.semanticTraits(dmd.expression.TraitsExp, dmd.dscope.Scope*)
[0x7f31b80bc329]
src/dmd/typesem.d:1778 dmd.mtype.Type dmd.typesem.typeSemantic(dmd.mtype.Type,
ref const(dmd.globals.Loc),
dmd.dscope.Scope*).visitTraits(dmd.mtype.TypeTraits) [0x7f31b80c62d2]
src/dmd/typesem.d:2002 _Z12typeSemanticP4TypeRK3LocP5Scope [0x7f31b80c2d7b]
src/dmd/typesem.d:3114 void dmd.typesem.resolve(dmd.mtype.Type, ref
const(dmd.globals.Loc), dmd.dscope.Scope*, out dmd.expression.Expression, out
dmd.mtype.Type, out dmd.dsymbol.Dsymbol,
bool).visitTraits(dmd.mtype.TypeTraits) [0x7f31b80c9ebc]
src/dmd/typesem.d:3134 void dmd.typesem.resolve(dmd.mtype.Type, ref
const(dmd.globals.Loc), dmd.dscope.Scope*, out dmd.expression.Expression, out
dmd.mtype.Type, out dmd.dsymbol.Dsymbol, bool) [0x7f31b80c86e0]
src/dmd/mtype.d:5394 _ZN10TypeTraits9toDsymbolEP5Scope [0x7f31b80638e8]
src/dmd/dsymbolsem.d:6661 void
dmd.dsymbolsem.aliasSemantic(dmd.declaration.AliasDeclaration,
dmd.dscope.Scope*) [0x7f31b7fa0266]
src/dmd/dsymbolsem.d:771 _ZN22DsymbolSemanticVisitor5visitEP16AliasDeclaration
[0x7f31b7f8df31]
src/dmd/declaration.d:989 _ZN16AliasDeclaration6acceptEP7Visitor
[0x7f31b7f4a41d]
src/dmd/dsymbolsem.d:581 _Z15dsymbolSemanticP7DsymbolP5Scope [0x7f31b7f8d6d9]
src/dmd/dsymbolsem.d:5379 void
dmd.dsymbolsem.DsymbolSemanticVisitor.visit(dmd.dclass.ClassDeclaration).__lambda9!(dmd.dsymbol.Dsymbol).__lambda9(dmd.dsymbol.Dsymbol)
[0x7f31b7f9d793]
src/dmd/dsymbol.d:103 void
dmd.dsymbol.foreachDsymbol(dmd.root.array.Array!(dmd.dsymbol.Dsymbol).Array*,
void delegate(dmd.dsymbol.Dsymbol)) [0x7f31b7f856c3]
src/dmd/dsymbolsem.d:5379 _ZN22DsymbolSemanticVisitor5visitEP16ClassDeclaration
[0x7f31b7f9cc2d]
src/dmd/dclass.d:993 _ZN16ClassDeclaration6acceptEP7Visitor [0x7f31b7f47f61]
src/dmd/dsymbolsem.d:581 _Z15dsymbolSemanticP7DsymbolP5Scope [0x7f31b7f8d6d9]
src/dmd/dtemplate.d:7402 void
dmd.dtemplate.TemplateInstance.expandMembers(dmd.dscope.Scope*).symbolDg(dmd.dsymbol.Dsymbol)
[0x7f31b7fb3104]
src/dmd/dsymbol.d:103 void
dmd.dsymbol.foreachDsymbol(dmd.root.array.Array!(dmd.dsymbol.Dsymbol).Array*,
void delegate(dmd.dsymbol.Dsymbol)) [0x7f31b7f856c3]
src/dmd/dtemplate.d:7407 void
dmd.dtemplate.TemplateInstance.expandMembers(dmd.dscope.Scope*)
[0x7f31b7fb3091]
src/dmd/dtemplate.d:7422 void
dmd.dtemplate.TemplateInstance.tryExpandMembers(dmd.dscope.Scope*)
[0x7f31b7fb316e]
src/dmd/dsymbolsem.d:6262 void
dmd.dsymbolsem.templateInstanceSemantic(dmd.dtemplate.TemplateInstance,
dmd.dscope.Scope*, dmd.root.array.Array!(dmd.expression.Expression).Array*)
[0x7f31b7f9f258]
src/dmd/dsymbolsem.d:2870 _ZN22DsymbolSemanticVisitor5visitEP16TemplateInstance
[0x7f31b7f94e5e]
src/dmd/dtemplate.d:7451 _ZN16TemplateInstance6acceptEP7Visitor
[0x7f31b7fb3229]
src/dmd/dsymbolsem.d:581 _Z15dsymbolSemanticP7DsymbolP5Scope [0x7f31b7f8d6d9]
src/dmd/typesem.d:2854 void dmd.typesem.resolve(dmd.mtype.Type, ref
const(dmd.globals.Loc), dmd.dscope.Scope*, out dmd.expression.Expression, out
dmd.mtype.Type, out dmd.dsymbol.Dsymbol,
bool).visitInstance(dmd.mtype.TypeInstance) [0x7f31b80c922f]
src/dmd/typesem.d:3129 void dmd.typesem.resolve(dmd.mtype.Type, ref
const(dmd.globals.Loc), dmd.dscope.Scope*, out dmd.expression.Expression, out
dmd.mtype.Type, out dmd.dsymbol.Dsymbol, bool) [0x7f31b80c869a]
src/dmd/typesem.d:1712 dmd.mtype.Type dmd.typesem.typeSemantic(dmd.mtype.Type,
ref const(dmd.globals.Loc),
dmd.dscope.Scope*).visitInstance(dmd.mtype.TypeInstance) [0x7f31b80c5fc8]
src/dmd/typesem.d:2000 _Z12typeSemanticP4TypeRK3LocP5Scope [0x7f31b80c2d57]
src/dmd/dsymbolsem.d:5020 pure @nogc @safe dmd.mtype.Type
dmd.dsymbolsem.DsymbolSemanticVisitor.visit(dmd.dclass.ClassDeclaration).__dgliteral2()
[0x7f31b7f9d679]
src/dmd/dsymbolsem.d:5002 dmd.mtype.Type
dmd.dsymbolsem.DsymbolSemanticVisitor.visit(dmd.dclass.ClassDeclaration).resolveBase!(dmd.mtype.Type).resolveBase(lazy
dmd.mtype.Type) [0x7f31b7fa0e6c]
src/dmd/dsymbolsem.d:5020 _ZN22DsymbolSemanticVisitor5visitEP16ClassDeclaration
[0x7f31b7f9b7b5]
src/dmd/dclass.d:993 _ZN16ClassDeclaration6acceptEP7Visitor [0x7f31b7f47f61]
src/dmd/dsymbolsem.d:581 _Z15dsymbolSemanticP7DsymbolP5Scope [0x7f31b7f8d6d9]
src/dmd/dsymbolsem.d:2289 void
dmd.dsymbolsem.DsymbolSemanticVisitor.visit(dmd.dmodule.Module).__lambda2!(dmd.dsymbol.Dsymbol).__lambda2(dmd.dsymbol.Dsymbol)
[0x7f31b7f931a0]
src/dmd/dsymbol.d:103 void
dmd.dsymbol.foreachDsymbol(dmd.root.array.Array!(dmd.dsymbol.Dsymbol).Array*,
void delegate(dmd.dsymbol.Dsymbol)) [0x7f31b7f856c3]
src/dmd/dsymbolsem.d:2286 _ZN22DsymbolSemanticVisitor5visitEP6Module
[0x7f31b7f93132]
src/dmd/dmodule.d:1498 _ZN6Module6acceptEP7Visitor [0x7f31b7f6d5fd]
src/dmd/dsymbolsem.d:581 _Z15dsymbolSemanticP7DsymbolP5Scope [0x7f31b7f8d6d9]
src/dmd/mars.d:573 int dmd.mars.tryMain(ulong, const(char)**, ref
dmd.globals.Param) [0x7f31b7efbee5]
src/dmd/mars.d:1005 _Dmain [0x7f31b7efd8f0]

--