[clang] [Clang] Treat default template argument as constant expressions (PR #107073)

via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 3 02:49:14 PDT 2024


https://github.com/cor3ntin created https://github.com/llvm/llvm-project/pull/107073

We only check that a default argument is a converted constant expression when using the default argument.

However, when parsing a default argument, we need to make sure to parse it as a constant expression such as not ODR-use variables. (otherwise, we would try to capture default template arguments of generic lambdas)

Fixes #107048

>From 5edd9a9abe019af2a14ccf9fbf43858f4141071b Mon Sep 17 00:00:00 2001
From: Corentin Jabot <corentinjabot at gmail.com>
Date: Tue, 3 Sep 2024 11:25:27 +0200
Subject: [PATCH] [Clang] Treat default template argument as constant
 expressions

We only check that a default argument is a converted constant
expression when using the default argument.

However, when parsing a default argument, we need to make sure
to parse it as a constant expression such as not ODR-use variables.
(otherwise, we would try to capture default template arguments
of generic lambdas)

Fixes #107048
---
 clang/docs/ReleaseNotes.rst                   |  1 +
 clang/lib/Parse/ParseTemplate.cpp             |  2 ++
 clang/test/SemaCXX/cxx2a-template-lambdas.cpp | 19 +++++++++++++++++++
 3 files changed, 22 insertions(+)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index fc940db4813948..e15414b994a36b 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -339,6 +339,7 @@ Bug Fixes to C++ Support
 - Template parameter names are considered in the name lookup of out-of-line class template
   specialization right before its declaration context. (#GH64082)
 - Fixed a constraint comparison bug for friend declarations. (#GH78101)
+- Clang no longer tries to capture default argument of template arguments of generic lambdas (#GH107048)
 
 Bug Fixes to AST Handling
 ^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Parse/ParseTemplate.cpp b/clang/lib/Parse/ParseTemplate.cpp
index 6ecfc15757f3d4..b80999f1b92e7f 100644
--- a/clang/lib/Parse/ParseTemplate.cpp
+++ b/clang/lib/Parse/ParseTemplate.cpp
@@ -960,6 +960,8 @@ Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
       EnterExpressionEvaluationContext ConstantEvaluated(
           Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
       DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseInitializer());
+      if (DefaultArg.isUsable())
+        DefaultArg = Actions.ActOnConstantExpression(DefaultArg);
       if (DefaultArg.isInvalid())
         SkipUntil(tok::comma, tok::greater, StopAtSemi | StopBeforeMatch);
     }
diff --git a/clang/test/SemaCXX/cxx2a-template-lambdas.cpp b/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
index fff524e77d3bf4..ec373f30876295 100644
--- a/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ b/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -97,3 +97,22 @@ void foo() {
 
 }
 #endif
+
+#if __cplusplus >= 202002L
+void GH107048() {
+  constexpr int x{};
+  const int y{};
+  auto b = []<int=x, int=y>{};
+  using A = decltype([]<int=x>{});
+
+  int z; // expected-note {{'z' declared here}}
+    auto c = []<int t=z>{
+    // expected-error at -1 {{no matching function for call to object of type}} \
+    // expected-error at -1 {{variable 'z' cannot be implicitly captured in a lambda with no capture-default specified}} \
+    // expected-note at -1 {{lambda expression begins here}} \
+    // expected-note at -1 4{{capture}} \
+    // expected-note at -1 {{candidate template ignored: substitution failure: reference to local variable 'z' declared in enclosing function}}
+    return t;
+  }();
+}
+#endif



More information about the cfe-commits mailing list