ahatanak updated this revision to Diff 495615. ahatanak marked an inline comment as done. ahatanak added a comment.
Instead of pushing an empty lambda scope, switch to the enclosing context if the variable is used in a default argument expression of a lambda call operator. Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D143109/new/ https://reviews.llvm.org/D143109 Files: clang/lib/Sema/SemaExpr.cpp clang/test/SemaCXX/lambda-default-arg.cpp Index: clang/test/SemaCXX/lambda-default-arg.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/lambda-default-arg.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only %s + +template <class T> bool test0() { + constexpr float a = 1e-5f; + return [=](float b = a) -> bool { + return a < 0; + }(); +} + +bool b0 = test0<int>(); + +template <class T> bool test1() { + float a = 1e-5f; + return [=](float b = a) -> bool { // expected-error {{default argument references local variable 'a'}} + return a < 0; + }(); +} + +bool b1 = test1<int>(); + +template <class T> bool test2(float a = 1e-5f) { + return [=](int b = sizeof(a)) -> bool { + return b; + }(); +} + +bool b2 = test2<int>(); Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -19082,6 +19082,14 @@ } } + // If the variable is used in a default argument expression of a lambda call + // operator, switch to the enclosing context to sync up with the function + // scope. + if (isLambdaCallOperator(DC)) + if (auto *PVD = dyn_cast_or_null<ParmVarDecl>( + ExprEvalContexts.back().ManglingContextDecl)) + if (PVD->getDeclContext() == DC) + DC = getLambdaAwareParentOfDeclContext(DC); // If the variable is declared in the current context, there is no need to // capture it.
Index: clang/test/SemaCXX/lambda-default-arg.cpp =================================================================== --- /dev/null +++ clang/test/SemaCXX/lambda-default-arg.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only %s + +template <class T> bool test0() { + constexpr float a = 1e-5f; + return [=](float b = a) -> bool { + return a < 0; + }(); +} + +bool b0 = test0<int>(); + +template <class T> bool test1() { + float a = 1e-5f; + return [=](float b = a) -> bool { // expected-error {{default argument references local variable 'a'}} + return a < 0; + }(); +} + +bool b1 = test1<int>(); + +template <class T> bool test2(float a = 1e-5f) { + return [=](int b = sizeof(a)) -> bool { + return b; + }(); +} + +bool b2 = test2<int>(); Index: clang/lib/Sema/SemaExpr.cpp =================================================================== --- clang/lib/Sema/SemaExpr.cpp +++ clang/lib/Sema/SemaExpr.cpp @@ -19082,6 +19082,14 @@ } } + // If the variable is used in a default argument expression of a lambda call + // operator, switch to the enclosing context to sync up with the function + // scope. + if (isLambdaCallOperator(DC)) + if (auto *PVD = dyn_cast_or_null<ParmVarDecl>( + ExprEvalContexts.back().ManglingContextDecl)) + if (PVD->getDeclContext() == DC) + DC = getLambdaAwareParentOfDeclContext(DC); // If the variable is declared in the current context, there is no need to // capture it.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits