diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp index 023aaa7f0572b..0036e506b6365 100644 --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -6504,6 +6504,11 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( return D2; } + // Update InsertPos, because preceding import calls may have invalidated + // it by adding new specializations. + if (!VarTemplate->findSpecialization(TemplateArgs, InsertPos)) + VarTemplate->AddSpecialization(D2, InsertPos); + QualType T; if (Error Err = importInto(T, D->getType())) return std::move(Err); @@ -6540,8 +6545,6 @@ ExpectedDecl ASTNodeImporter::VisitVarTemplateSpecializationDecl( if (FoundSpecialization) D2->setPreviousDecl(FoundSpecialization->getMostRecentDecl()); - VarTemplate->AddSpecialization(D2, InsertPos); - addDeclToContexts(D, D2); // Import the rest of the chain. I.e. import all subsequent declarations.