[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
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 4128ca7..251eb4c 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -346,6 +346,7 @@
- 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 6ecfc15..de29652 100644
--- a/clang/lib/Parse/ParseTemplate.cpp
+++ b/clang/lib/Parse/ParseTemplate.cpp
@@ -959,7 +959,7 @@
++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 fff524e..00ba291 100644
--- a/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
+++ b/clang/test/SemaCXX/cxx2a-template-lambdas.cpp
@@ -97,3 +97,37 @@
}
#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@-1 {{no matching function for call to object of type}} \
+ // expected-error@-1 {{variable 'z' cannot be implicitly captured in a lambda with no capture-default specified}} \
+ // expected-note@-1 {{lambda expression begins here}} \
+ // expected-note@-1 4{{capture}} \
+ // expected-note@-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@-1 {{variable 's' cannot be implicitly captured in a lambda with no capture-default specified}} \
+ // expected-note@-1 {{lambda expression begins here}} \
+ // expected-note@-1 4{{capture}}
+}
+}
+#endif