[clang] [Clang][Parser] Have the depth of the abbreviated generic lambdas inside a requires clause differ from the surrounding generic lambda (PR #80656)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Mon Feb 5 05:00:55 PST 2024


https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/80656

>From 48211eb7778db8fb8af144d59adb2e0941957c4c Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Mon, 5 Feb 2024 18:01:34 +0800
Subject: [PATCH 1/2] GH78524

---
 clang/docs/ReleaseNotes.rst                   |  4 +++
 clang/lib/Parse/ParseExprCXX.cpp              |  6 ++++-
 .../Parser/cxx-concepts-requires-clause.cpp   | 27 +++++++++++++++++++
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index e634db3c718c9d..42348512326953 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -193,6 +193,10 @@ Bug Fixes to C++ Support
 - Fixed an out-of-bounds error caused by building a recovery expression for ill-formed
   function calls while substituting into constraints.
   (`#58548 <https://github.com/llvm/llvm-project/issues/58548>`_)
+- Fixed an issue where template parameters of the nested abbreviated generic lambda within
+  a requires-clause lie at the same depth as those of the surrounding lambda. This,
+  in turn, results in the wrong template argument substitution during the constraint checking.
+  (`#78524 <https://github.com/llvm/llvm-project/issues/78524>`_) 
 - Fix incorrect code generation caused by the object argument of ``static operator()`` and ``static operator[]`` calls not being evaluated.
   Fixes (`#67976 <https://github.com/llvm/llvm-project/issues/67976>`_)
 - Fix crash and diagnostic with const qualified member operator new.
diff --git a/clang/lib/Parse/ParseExprCXX.cpp b/clang/lib/Parse/ParseExprCXX.cpp
index fd262ff31e661a..5c374d2eb452dc 100644
--- a/clang/lib/Parse/ParseExprCXX.cpp
+++ b/clang/lib/Parse/ParseExprCXX.cpp
@@ -1385,6 +1385,11 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
       Diag(RAngleLoc,
            diag::err_lambda_template_parameter_list_empty);
     } else {
+      // We increase the template depth before recursing into a requires-clause.
+      // The abbreviated generic lambdas thereof could have different template
+      // depths, avoiding substituting into wrong template parameters during the
+      // satisfaction check.
+      ++CurTemplateDepthTracker;
       ExprResult RequiresClause;
       if (TryConsumeToken(tok::kw_requires)) {
         RequiresClause =
@@ -1396,7 +1401,6 @@ ExprResult Parser::ParseLambdaExpressionAfterIntroducer(
 
       Actions.ActOnLambdaExplicitTemplateParameterList(
           Intro, LAngleLoc, TemplateParams, RAngleLoc, RequiresClause);
-      ++CurTemplateDepthTracker;
     }
   }
 
diff --git a/clang/test/Parser/cxx-concepts-requires-clause.cpp b/clang/test/Parser/cxx-concepts-requires-clause.cpp
index 1ec1eefa128653..f268092728fdfe 100644
--- a/clang/test/Parser/cxx-concepts-requires-clause.cpp
+++ b/clang/test/Parser/cxx-concepts-requires-clause.cpp
@@ -168,3 +168,30 @@ auto lambda4 = [] requires(sizeof(char) == 1){}; // expected-error {{expected bo
 #if __cplusplus <= 202002L
 // expected-warning at -2{{lambda without a parameter clause is a C++23 extension}}
 #endif
+
+namespace GH78524 {
+
+template <typename T> T Foo;
+
+template <typename T> auto C(Foo<T>);
+
+template <typename T> struct D {
+  decltype(T()(C<T>)) Type;
+};
+
+template <typename T, typename U> D<T> G(T, U) { return {}; }
+
+struct E {};
+
+void F() {
+  G([]<typename T>
+//     ~~~~~~~~~~ T: 0,0
+      requires requires { [](auto...) {}; }(T)
+//                           ~~~~ auto: 1,0
+    { return T(); },
+    E{});
+}
+
+int a = []<int=0> requires requires { [](auto){}; } { return 0; }();
+
+} // namespace GH78524

>From c550f3a5f519f11634e106cb40a3c53fa22b345e Mon Sep 17 00:00:00 2001
From: Younan Zhang <zyn7109 at gmail.com>
Date: Mon, 5 Feb 2024 21:00:40 +0800
Subject: [PATCH 2/2] Remove a trailing whitespace

---
 clang/docs/ReleaseNotes.rst | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 42348512326953..1c427618ce44bd 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -196,7 +196,7 @@ Bug Fixes to C++ Support
 - Fixed an issue where template parameters of the nested abbreviated generic lambda within
   a requires-clause lie at the same depth as those of the surrounding lambda. This,
   in turn, results in the wrong template argument substitution during the constraint checking.
-  (`#78524 <https://github.com/llvm/llvm-project/issues/78524>`_) 
+  (`#78524 <https://github.com/llvm/llvm-project/issues/78524>`_)
 - Fix incorrect code generation caused by the object argument of ``static operator()`` and ``static operator[]`` calls not being evaluated.
   Fixes (`#67976 <https://github.com/llvm/llvm-project/issues/67976>`_)
 - Fix crash and diagnostic with const qualified member operator new.



More information about the cfe-commits mailing list