Skip to content

Commit b320ea7

Browse files
zygoloidchandlerc
andauthored
Improve source location in an import error. (#5887)
When attempting to import a definition of a class with virtual bases, diagnose the point of use instead of the point of definition of the class. --------- Co-authored-by: Chandler Carruth <[email protected]>
1 parent 7f96069 commit b320ea7

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

toolchain/check/import_cpp.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -620,15 +620,8 @@ static auto ImportClassObjectRepr(Context& context, SemIR::ClassId class_id,
620620

621621
// Import bases.
622622
for (const auto& base : clang_def->bases()) {
623-
if (base.isVirtual()) {
624-
// TODO: Handle virtual bases. We don't actually know where they go in the
625-
// layout. We may also want to use a different size in the layout for
626-
// `partial C`, excluding the virtual base. It's also not entirely safe to
627-
// just skip over the virtual base, as the type we would construct would
628-
// have a misleading size.
629-
context.TODO(import_ir_inst_id, "class with virtual bases");
630-
return SemIR::ErrorInst::TypeInstId;
631-
}
623+
CARBON_CHECK(!base.isVirtual(),
624+
"Should not import definition for class with a virtual base");
632625

633626
auto [base_type_inst_id, base_type_id] =
634627
MapType(context, import_ir_inst_id, base.getType());
@@ -810,6 +803,17 @@ auto ImportCppClassDefinition(Context& context, SemIR::LocId loc_id,
810803
clang::CXXRecordDecl* clang_def = clang_decl->getDefinition();
811804
CARBON_CHECK(clang_def, "Complete type has no definition");
812805

806+
if (clang_def->getNumVBases()) {
807+
// TODO: Handle virtual bases. We don't actually know where they go in the
808+
// layout. We may also want to use a different size in the layout for
809+
// `partial C`, excluding the virtual base. It's also not entirely safe to
810+
// just skip over the virtual base, as the type we would construct would
811+
// have a misleading size. For now, treat a C++ class with vbases as
812+
// incomplete in Carbon.
813+
context.TODO(loc_id, "class with virtual bases");
814+
return false;
815+
}
816+
813817
auto import_ir_inst_id =
814818
context.insts().GetCanonicalLocId(class_inst_id).import_ir_inst_id();
815819
BuildClassDefinition(context, import_ir_inst_id, class_id, class_inst_id,

toolchain/check/testdata/interop/cpp/class/base.carbon

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,11 +175,12 @@ fn Convert(p: Cpp.B*) {
175175

176176
library "[[@TEST_NAME]]";
177177

178-
// CHECK:STDERR: fail_todo_use_virtual_inheritance.carbon:[[@LINE+2]]:1: in import [InImport]
179-
// CHECK:STDERR: ./virtual_inheritance.h:3: error: semantics TODO: `class with virtual bases` [SemanticsTodo]
180178
import Cpp library "virtual_inheritance.h";
181179

182180
fn Convert(p: Cpp.B*) -> Cpp.A* {
181+
// CHECK:STDERR: fail_todo_use_virtual_inheritance.carbon:[[@LINE+14]]:3: error: semantics TODO: `class with virtual bases` [SemanticsTodo]
182+
// CHECK:STDERR: return p;
183+
// CHECK:STDERR: ^~~~~~~~~
183184
// CHECK:STDERR: fail_todo_use_virtual_inheritance.carbon:[[@LINE+11]]:3: note: while completing C++ class type `Cpp.B` [InCppTypeCompletion]
184185
// CHECK:STDERR: return p;
185186
// CHECK:STDERR: ^~~~~~~~~

0 commit comments

Comments
 (0)