Skip to content

Commit 4fe7180

Browse files
committed
[NFC] [cxx-interop] Slightly improve function instantiation logic
We were not handling nullptrs consistently. This shouldn't actually change the behavior for any known case but makes the error handling logic a little more explicit and robust.
1 parent 36ba535 commit 4fe7180

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

lib/ClangImporter/ClangImporter.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7727,6 +7727,12 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
77277727
assert(isa<clang::FunctionTemplateDecl>(decl->getClangDecl()) &&
77287728
"This API should only be used with function templates.");
77297729

7730+
// If we hit some instantiation failure and expect the compiler to imminently
7731+
// terminate (with an error), return some reasonable-looking placeholder value
7732+
// in the meantime because callers expect this function to return some
7733+
// non-empty ConcreteDeclRef.
7734+
auto failurePlaceholder = [&]() { return ConcreteDeclRef(decl); };
7735+
77307736
auto *newFn =
77317737
decl->getASTContext()
77327738
.getClangModuleLoader()
@@ -7735,31 +7741,28 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
77357741
const_cast<clang::FunctionTemplateDecl *>(
77367742
cast<clang::FunctionTemplateDecl>(decl->getClangDecl())),
77377743
subst);
7738-
// We failed to specialize this function template. The compiler is going to
7739-
// exit soon. Return something valid in the meantime.
77407744
if (!newFn)
7741-
return ConcreteDeclRef(decl);
7745+
return failurePlaceholder();
77427746

77437747
auto [fnIt, inserted] =
77447748
Impl.specializedFunctionTemplates.try_emplace(newFn, nullptr);
77457749
if (!inserted)
77467750
return ConcreteDeclRef(fnIt->second);
77477751

7748-
auto newDecl = cast_or_null<ValueDecl>(
7749-
decl->getASTContext().getClangModuleLoader()->importDeclDirectly(
7750-
newFn));
7752+
auto *newDecl = cast_or_null<ValueDecl>(
7753+
decl->getASTContext().getClangModuleLoader()->importDeclDirectly(newFn));
7754+
if (!newDecl)
7755+
return failurePlaceholder();
77517756

7752-
if (auto fn = dyn_cast<AbstractFunctionDecl>(newDecl)) {
7757+
if (auto *fn = dyn_cast<AbstractFunctionDecl>(newDecl)) {
77537758
if (!subst.empty()) {
77547759
newDecl = rewriteIntegerTypes(subst, decl, fn);
77557760
}
77567761
}
77577762

7758-
if (auto fn = dyn_cast<FuncDecl>(decl)) {
7763+
if (auto *fn = dyn_cast<FuncDecl>(decl)) {
77597764
newDecl = addThunkForDependentTypes(fn, cast<FuncDecl>(newDecl));
7760-
}
77617765

7762-
if (auto fn = dyn_cast<FuncDecl>(decl)) {
77637766
if (newFn->getNumParams() != fn->getParameters()->size()) {
77647767
newDecl = generateThunkForExtraMetatypes(subst, fn,
77657768
cast<FuncDecl>(newDecl));

0 commit comments

Comments
 (0)