[clang] 030e4d0 - [Clang] Treat default template argument as constant expressions (#107073)

via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 4 01:38:21 PDT 2024


Author: cor3ntin
Date: 2024-09-04T10:38:18+02:00
New Revision: 030e4d0cdf4c43a6ec1ca301b5a358991fa2ac4f

URL: https://github.com/llvm/llvm-project/commit/030e4d0cdf4c43a6ec1ca301b5a358991fa2ac4f
DIFF: https://github.com/llvm/llvm-project/commit/030e4d0cdf4c43a6ec1ca301b5a358991fa2ac4f.diff

LOG: [Clang] Treat default template argument as constant expressions (#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

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.rst
    clang/lib/Parse/ParseTemplate.cpp
    clang/test/SemaCXX/cxx2a-template-lambdas.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4128ca78ce3960..251eb4c1c4559c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -346,6 +346,7 @@ Bug Fixes to C++ Support
 - Fix handling of ``_`` as the name of a lambda's init capture variable. (#GH107024)
 - Fix an issue with dependent source location expressions (#GH106428), (#GH81155), (#GH80210), (#GH85373)
 - Fixed a bug in the substitution of empty pack indexing types. (#GH105903)
+- Clang no longer tries to capture non-odr used default arguments of template parameters 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..de29652abbfd95 100644
--- a/clang/lib/Parse/ParseTemplate.cpp
+++ b/clang/lib/Parse/ParseTemplate.cpp
@@ -959,7 +959,7 @@ Parser::ParseNonTypeTemplateParameter(unsigned Depth, unsigned Position) {
       ++CurTemplateDepthTracker;
       EnterExpressionEvaluationContext ConstantEvaluated(
           Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
-      DefaultArg = Actions.CorrectDelayedTyposInExpr(ParseInitializer());
+      DefaultArg = Actions.ActOnConstantExpression(ParseInitializer());
       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..00ba291fbd1981 100644
--- a/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ b/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -97,3 +97,37 @@ void foo() {
 
 }
 #endif
+
+#if __cplusplus >= 202002L
+namespace {
+struct S {};
+constexpr S gs;
+void f() {
+  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;
+  }();
+
+  auto class_type_global = []<S=gs>{};
+
+  static constexpr S static_s;
+  auto class_type_static = []<S=static_s>{};
+
+  constexpr S s;  // expected-note {{'s' declared here}}
+  auto class_type = []<S=s>{};
+  // expected-error at -1 {{variable 's' 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}}
+}
+}
+#endif


        


More information about the cfe-commits mailing list