[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:46 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: cor3ntin (cor3ntin)
<details>
<summary>Changes</summary>
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
---
Full diff: https://github.com/llvm/llvm-project/pull/107073.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Parse/ParseTemplate.cpp (+2)
- (modified) clang/test/SemaCXX/cxx2a-template-lambdas.cpp (+19)
``````````diff
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
``````````
</details>
https://github.com/llvm/llvm-project/pull/107073
More information about the cfe-commits
mailing list