diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -6001,7 +6001,9 @@ (ParentDependsOnArgs && (ParentDC->isFunctionOrMethod() || isa(ParentDC) || isa(ParentDC))) || - (isa(D) && cast(D)->isLambda())) { + (isa(D) && cast(D)->isLambda() && + cast(D)->getTemplateDepth() > + TemplateArgs.getNumRetainedOuterLevels())) { // D is a local of some kind. Look into the map of local // declarations to their instantiations. if (CurrentInstantiationScope) { diff --git a/clang/test/SemaCXX/lambdas-implicit-explicit-template.cpp b/clang/test/SemaCXX/lambdas-implicit-explicit-template.cpp --- a/clang/test/SemaCXX/lambdas-implicit-explicit-template.cpp +++ b/clang/test/SemaCXX/lambdas-implicit-explicit-template.cpp @@ -39,3 +39,10 @@ const auto lambda = [&](auto arg1) {}; [&](auto arg2) { lambda.operator()(arg2); }(0); } + +auto d = [](auto) {}; + +template +void d1(T x) { d.operator()(x); } + +void d2() { d1(0); }