Skip to content

[Clang] Treat default template argument as constant expressions #107073

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Parse/ParseTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
26 changes: 26 additions & 0 deletions clang/test/SemaCXX/cxx2a-template-lambdas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,29 @@ 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@-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;
}();

struct 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}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious about these cases: https://godbolt.org/z/xWbWYfnf3

struct S {};
constexpr S s; 

void f() {
  auto class_type = []<S=s>{};
}

void h() {
  static constexpr S s;
  auto class_type = []<S=s>{};
}

template <S=s>
void g() {}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All of these work (because they are not local entities so don't need capturing https://eel.is/c++draft/basic#pre-7)

}
#endif
Loading