The function Sema::CheckVarTemplateId tries to find a template specialization. If that fails it gets an InsertPos where the specialization should be added in a FoldingSet. Then it tries to resolve dependant arguments. This may add an item to the same FoldingSet. If this happens the set may be resized, which invalidates InsertPos.
It would be possible to send a nullptr to the call to BuildVarTemplateInstantiation, but that changes the behavior and fixing that seemed quite intrusive. Therefore the InsertPos is determined again before making the call.
Fixes PR43221: Assertion failure in translation unit with exactly 1 less than a power of 2 (starting at 127) operator== overloads combined with at least one operator== template with a substitution failure