[PATCH] D143109: [Sema] Push a LambdaScopeInfo before calling SubstDefaultArgument

Akira Hatanaka via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 7 12:44:35 PST 2023


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.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143109.495615.patch
Type: text/x-patch
Size: 1508 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230207/cbc6a7f4/attachment.bin>


More information about the cfe-commits mailing list