diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -4471,7 +4471,16 @@ // Note that we do not instantiate a definition until we see an odr-use // in DoMarkVarDeclReferenced(). // FIXME: LateAttrs et al.? - VarTemplateSpecializationDecl *Decl = BuildVarTemplateInstantiation( + // Search for the variable template specialization declaration that + // corresponds to these arguments again to get an up-to-date InsertPos. + // The template argument deduction in between may update the underlying + // specialization container and invalidate previous iterators. + VarTemplateSpecializationDecl *Decl = + Template->findSpecialization(Converted, InsertPos); + // Assert we should not have a variable template specialization, otherwise we + // should have returned early. + assert(!Decl); + Decl = BuildVarTemplateInstantiation( Template, InstantiationPattern, *InstantiationArgs, TemplateArgs, Converted, TemplateNameLoc, InsertPos /*, LateAttrs, StartingScope*/); if (!Decl)