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 all 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 @@ -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
^^^^^^^^^^^^^^^^^^^^^^^^^
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
34 changes: 34 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,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@-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}}
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