diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp index c20033bbc258..9477192e2352 100644 --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -862,9 +862,16 @@ Decl *TemplateDeclInstantiator::VisitClassTemplateDecl(ClassTemplateDecl *D) { // Finish handling of friends. if (isFriend) { DC->makeDeclVisibleInContext(Inst, /*Recoverable*/ false); + Inst->setLexicalDeclContext(Owner); + RecordInst->setLexicalDeclContext(Owner); return Inst; } + if (D->isOutOfLine()) { + Inst->setLexicalDeclContext(D->getLexicalDeclContext()); + RecordInst->setLexicalDeclContext(D->getLexicalDeclContext()); + } + Owner->addDecl(Inst); if (!PrevClassTemplate) {