Index: lib/Sema/SemaExprCXX.cpp =================================================================== --- lib/Sema/SemaExprCXX.cpp +++ lib/Sema/SemaExprCXX.cpp @@ -15,6 +15,7 @@ #include "clang/Sema/SemaInternal.h" #include "TypeLocBuilder.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/ASTLambda.h" #include "clang/AST/CXXInheritance.h" #include "clang/AST/CharUnits.h" #include "clang/AST/DeclObjC.h" @@ -736,7 +737,20 @@ if (method && method->isInstance()) ThisTy = method->getThisType(Context); } - + if (ThisTy.isNull()) { + if (isGenericLambdaCallOperatorSpecialization(CurContext) && + CurContext->getParent()->getParent()->isRecord()) { + // This is a generic lambda call operator that is being instantiated + // within an NSDMI - use the enclosing class as 'this'. There is no + // enclosing member function to retrieve the 'this' pointer from. + QualType ClassTy = Context.getTypeDeclType( + cast(CurContext->getParent()->getParent())); + // Unlike for within methods, we don't have to worry about adding + // CVR qualifications in this context. Just get the pointer to + // the enclosing class. + return Context.getPointerType(ClassTy); + } + } return ThisTy; } Index: test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp =================================================================== --- test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp +++ test/SemaCXX/cxx1y-generic-lambdas-capturing.cpp @@ -1358,6 +1358,21 @@ int run_char = X{}.foo('a'); int run_int = X{}.foo(4); } +namespace nsdmi_capturing_this { +struct X { + int m = 10; + int n = [this](auto) { return m; }(20); +}; +template +struct XT { + T m = 10; + T n = [this](auto) { return m; }(20); +}; + +XT xt{}; + + +} #endif // MS_EXTENSIONS