Fix crash when lowering Carbon derived class with C++ virtual base class (#7745)

If a Carbon class overrides virtual functions from a C++ base class but
is never referenced from C++, it is never exported to Clang. During
lowering, `BuildVtable` then fails to find a `CXXRecordDecl` and crashes
when attempting to get the vtable from Clang's code generator.

Ensure dynamic classes with foreign vtables are exported to Clang when
completing the class definition in `CheckCompleteClassType`, and look up
`first_decl_id()` in `BuildVtable`.

Fixes #7721

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
This commit is contained in:
ATHARVA
2026-09-18 18:08:53 +00:00
committed by GitHub
co-authored by Dana Jansens
parent d84387f8b8
commit 804359dce0
9 changed files with 416 additions and 130 deletions
+10
View File
@@ -293,6 +293,16 @@ auto ExportClassToCpp(Context& context, SemIR::ClassType class_type)
return record_decl;
}
auto ExportAndCompleteClassToCpp(Context& context, SemIR::ClassType class_type)
-> clang::TagDecl* {
auto* tag_decl = ExportClassToCpp(context, class_type);
if (tag_decl && context.cpp_context() &&
context.ast_context().getExternalSource()) {
context.ast_context().getExternalSource()->CompleteType(tag_decl);
}
return tag_decl;
}
// Export the bindings in a generic as a `clang::TemplateParameterList`.
static auto ExportGenericBindings(Context& context, SemIR::LocId loc_id,
SemIR::GenericId generic_id,