@@ -7727,6 +7727,12 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
7727
7727
assert (isa<clang::FunctionTemplateDecl>(decl->getClangDecl ()) &&
7728
7728
" This API should only be used with function templates." );
7729
7729
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
+
7730
7736
auto *newFn =
7731
7737
decl->getASTContext ()
7732
7738
.getClangModuleLoader ()
@@ -7735,31 +7741,28 @@ ClangImporter::getCXXFunctionTemplateSpecialization(SubstitutionMap subst,
7735
7741
const_cast <clang::FunctionTemplateDecl *>(
7736
7742
cast<clang::FunctionTemplateDecl>(decl->getClangDecl ())),
7737
7743
subst);
7738
- // We failed to specialize this function template. The compiler is going to
7739
- // exit soon. Return something valid in the meantime.
7740
7744
if (!newFn)
7741
- return ConcreteDeclRef (decl );
7745
+ return failurePlaceholder ( );
7742
7746
7743
7747
auto [fnIt, inserted] =
7744
7748
Impl.specializedFunctionTemplates .try_emplace (newFn, nullptr );
7745
7749
if (!inserted)
7746
7750
return ConcreteDeclRef (fnIt->second );
7747
7751
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 ();
7751
7756
7752
- if (auto fn = dyn_cast<AbstractFunctionDecl>(newDecl)) {
7757
+ if (auto * fn = dyn_cast<AbstractFunctionDecl>(newDecl)) {
7753
7758
if (!subst.empty ()) {
7754
7759
newDecl = rewriteIntegerTypes (subst, decl, fn);
7755
7760
}
7756
7761
}
7757
7762
7758
- if (auto fn = dyn_cast<FuncDecl>(decl)) {
7763
+ if (auto * fn = dyn_cast<FuncDecl>(decl)) {
7759
7764
newDecl = addThunkForDependentTypes (fn, cast<FuncDecl>(newDecl));
7760
- }
7761
7765
7762
- if (auto fn = dyn_cast<FuncDecl>(decl)) {
7763
7766
if (newFn->getNumParams () != fn->getParameters ()->size ()) {
7764
7767
newDecl = generateThunkForExtraMetatypes (subst, fn,
7765
7768
cast<FuncDecl>(newDecl));
0 commit comments