diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp --- a/clang/lib/Sema/SemaExpr.cpp +++ b/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( + 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. diff --git a/clang/test/SemaCXX/lambda-default-arg.cpp b/clang/test/SemaCXX/lambda-default-arg.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/lambda-default-arg.cpp @@ -0,0 +1,27 @@ +// RUN: %clang_cc1 -std=c++17 -verify -fsyntax-only %s + +template bool test0() { + constexpr float a = 1e-5f; + return [=](float b = a) -> bool { + return a < 0; + }(); +} + +bool b0 = test0(); + +template 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(); + +template bool test2(float a = 1e-5f) { + return [=](int b = sizeof(a)) -> bool { + return b; + }(); +} + +bool b2 = test2();