When adding an imported C++ name, make sure that its clang::Decl is mapped if import failed (#5769)

When mapping parameter types, we assume that if the `clang::Decl` isn't
mapped, the name wasn't added, so this fixes a bug that triggers a crash
otherwise.

Part of #5533.
This commit is contained in:
Boaz Brickner
2025-07-08 13:12:32 +00:00
committed by GitHub
parent cd14dca749
commit 9d0aaa740b
4 changed files with 221 additions and 0 deletions
+12
View File
@@ -453,6 +453,12 @@ static auto BuildClassDefinition(Context& context,
return {class_id, class_inst_id};
}
// Mark the given `Decl` as failed in `clang_decls`.
static auto MarkFailedDecl(Context& context, clang::Decl* clang_decl) {
context.sem_ir().clang_decls().Add(
{.decl = clang_decl, .inst_id = SemIR::ErrorInst::InstId});
}
// Imports a record declaration from Clang to Carbon. If successful, returns
// the new Carbon class declaration `InstId`.
// TODO: Change `clang_decl` to `const &` when lookup is using `clang::DeclID`
@@ -466,11 +472,13 @@ static auto ImportCXXRecordDecl(Context& context, SemIR::LocId loc_id,
if (!clang_def) {
context.TODO(loc_id,
"Unsupported: Record declarations without a definition");
MarkFailedDecl(context, clang_decl);
return SemIR::ErrorInst::InstId;
}
if (clang_def->isDynamicClass()) {
context.TODO(loc_id, "Unsupported: Dynamic Class");
MarkFailedDecl(context, clang_decl);
return SemIR::ErrorInst::InstId;
}
@@ -717,14 +725,17 @@ static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
-> SemIR::InstId {
if (clang_decl->isVariadic()) {
context.TODO(loc_id, "Unsupported: Variadic function");
MarkFailedDecl(context, clang_decl);
return SemIR::ErrorInst::InstId;
}
if (!clang_decl->isGlobal()) {
context.TODO(loc_id, "Unsupported: Non-global function");
MarkFailedDecl(context, clang_decl);
return SemIR::ErrorInst::InstId;
}
if (clang_decl->getTemplatedKind() != clang::FunctionDecl::TK_NonTemplate) {
context.TODO(loc_id, "Unsupported: Template function");
MarkFailedDecl(context, clang_decl);
return SemIR::ErrorInst::InstId;
}
@@ -740,6 +751,7 @@ static auto ImportFunctionDecl(Context& context, SemIR::LocId loc_id,
context.scope_stack().Pop();
if (!function_params_insts.has_value()) {
MarkFailedDecl(context, clang_decl);
return SemIR::ErrorInst::InstId;
}