Implements 'P1169R4: static operator()' from C++2b.
Details
- Reviewers
aaron.ballman erichkeane shafik cor3ntin lichray hubert.reinterpretcast - Group Reviewers
Restricted Project - Commits
- rG6523814c4e38: [Clang] P1169R4: static operator()
Diff Detail
- Repository
- rG LLVM Github Monorepo
Event Timeline
Thanks a lot for working on that!
I think this is the right direction but i would like to see more tests.
Notably:
- converting a lambda with a static operator to a pointer
- tests in dependant contexts
- classes having both static and non-static operator() . especially the example in https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1169r4.html#overload-resolution deserves a test.
- unevaluated lambda tests / constexpr tests
There is the question of whether we'd want the call operator to be implicitly static but I'm don't think it's currently allowed, it may have abi impact and it doesn't to be dealt with in this PR so that should not hold us.
The other question is whether we want to make static operator() an extension in older language modes. I don;t see a reason to prohibit that (with a warning)
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1057 | ||
clang/include/clang/Basic/DiagnosticSemaKinds.td | ||
9091 | For consistency and clarity, I'd say 'static overloaded %0' | |
9093 | ||
clang/lib/Parse/ParseExprCXX.cpp | ||
1265 | We might want to add a note showing where the capture list is. | |
clang/lib/Sema/SemaChecking.cpp | ||
5856 | ||
clang/lib/Sema/SemaLambda.cpp | ||
963–964 | Do we actually need that check ? It should be diagnosed already. | |
995–997 | Do we want an assert to make sure it's not something else than static or none? | |
clang/test/CXX/over/over.oper/over.literal/p7.cpp | ||
23–25 ↗ | (On Diff #459361) | This doesn't really seem to be the right file for this test - I'd expect that to be in a test file where we do sema check on operators, not literal operators. |
Wow, thanks for the quick review!
Turns out I had a bug in the conversion function generation, thanks for the suggestion! Fixed it and added tests.
- tests in dependant contexts
Added some tests for that, but I'm not sure how to judge if I have good coverage of all the possible edge cases, so more suggestions are welcome :)
- classes having both static and non-static operator() . especially the example in https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p1169r4.html#overload-resolution deserves a test.
I _think_ you can't do that - https://stackoverflow.com/questions/5365689/c-overload-static-function-with-non-static-function.
The example from the paper is about disambiguating the conversion-to-function-pointer call from the operator(), which is kind-of already tested because otherwise you wouldn't be able to call a static lambda at all. I added it as an explicit test, though.
- unevaluated lambda tests / constexpr tests
I'm not sure I understand what you mean by unevaluated lambdas, could you elaborate? I added some constexpr/eval tests.
There is the question of whether we'd want the call operator to be implicitly static but I'm don't think it's currently allowed, it may have abi impact and it doesn't to be dealt with in this PR so that should not hold us.
The other question is whether we want to make static operator() an extension in older language modes. I don;t see a reason to prohibit that (with a warning)
I thought I made it an extension, but apparently it was only for the static lambdas, just because that's how most of the other lambda extensions were implemented :)
I don't have a strong opinion about this. But I realized that without backporting the overload resolution changes, this will not be useful for lambdas.
So just to be consistent right now I made it an error for both - we can always change that to a warning pretty easily.
clang/lib/Sema/SemaLambda.cpp | ||
---|---|---|
963–964 | It's not about diagnosing, it's preventing declaring the operator 'const' if it's static. | |
clang/test/CXX/over/over.oper/over.literal/p7.cpp | ||
23–25 ↗ | (On Diff #459361) | Oops, this is the wrong p7 indeed :) thanks! |
clang/lib/Parse/ParseExprCXX.cpp | ||
---|---|---|
1265 | I added a note, but the captures list is always just before the static specifier, so I'm not sure how useful it is. WDYT? |
Beside a missing test, this LGTM
I'd like someone else (@erichkeane maybe) to review the codegen tests.
clang/test/Parser/cxx2b-lambdas.cpp | ||
---|---|---|
60 | For completeness, can you add a test for *this/this? |
Does anyone feel comfortable with looking at the CodeGen tests? They're checking that we aren't passing a this argument.
FWIW, you shouldn't merge something that has nobody signed on as the reviewer (and has only mild acceptance from people subscribing) -- a better approach is to add some reviewers, get them to accept, then land. Otherwise, coming back to this review in the future when doing code archeology leads to confusion about whether the changes should have even gone in at all, etc. I've added some more reviewers so we can make sure this gets the pretty green checkmark before landing. :-)
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1049 | This is a bit confusing -- we're saying it's an extension but then making it an error? I've reworded based on how I think you're intending it to be issued. | |
1053–1057 | These are semantic errors, not parsing ones. This means these will be diagnosed when parsing the lambda rather than when instantiating it. I don't think that matters for the cast of combining mutable and static, but I'm less certain about "have any captures" because of cases like: template <typename... Types> auto func(Types... Ts) { return [Ts...] { return 1; }; } int main() { auto lambda = func(); } I'm pretty sure that lambda has no captures for that call, but it could have captures depending on the instantiation. Actually, from some off-list discussion with @erichkeane, even mutable and static are a problem in code like: template <typename Ty> void func(T t) { if constexpr (Something<T>) { [](){}; } else { [t](){}; } where the lambda is in a discarded statement. So I think these might need to change to be Sema diagnostics (and we should add some additional test coverage). | |
clang/include/clang/Basic/DiagnosticSemaKinds.td | ||
9091 | ||
clang/lib/Sema/SemaDeclCXX.cpp | ||
15939–15945 | The diagnostic formatter knows how to format anything that inherits from NamedDecl. |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 | From https://eel.is/c++draft/expr.prim.lambda.general#4
So this should be a parsing error. Or maybe I don't understand what you're saying. There are no static lambdas in your examples so I'm not sure how they're related. |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 |
Parsing errors are where the grammar disallows something, generally. The rest are semantic diagnostics (e.g., we can parse the construct just fine, but we diagnose when turning it into an AST node because that's the point at which we have complete information about what we've parsed). That said, my concern was mostly around SFINAE situations. My recollection is that SFINAE traps do not cover parsing errors only type substitution errors. So for my first example, I would expect there to be no parsing error despite specifying a capture list because that capture list can be empty when the pack is empty, but we would get a SFINAE diagnostic when rebuilding declaration during template instantiation if the pack was not empty.
Sorry, I was being lazy with my examples and showing more about the capture list. Consider: template <typename... Types> auto func(Types... Ts) { return [Ts...] static { return 1; }; } int main() { auto lambda = func(); } |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 | Like I said,
Ts... is still a syntactic lambda-capture, even if it's instantiated as an empty pack. I don't see how that's not a parsing error. |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 | Ah, I think I maybe see where the confusion is coming in, now: you think my example should be diagnosed and I think my example should be accepted. Based on the standards wording, I think you're right. The standard specifically uses "lambda-capture" as a grammar term, so it *is* a parsing error at that point. Based on my understanding of the intent behind the feature, I think I'm right and there's a core issue here. I don't think we intended to prohibit empty packs from being captured as non-SFINAEable error (and in off-list talks with @erichkeane, he agrees). as that allows different specializations of the function containing the lambda, which could be of use. However, I'm not 100% sure on the intent. CC @hubert.reinterpretcast as the C++ standards conformance code owner to see if he has an opinion or other recollections here. |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 | I can see something like [Ts...] static(sizeof...(Types) == 0) {} being eventually useful, but as long as the static is non-conditional allowing a capture list with an empty pack doesn't make sense to me. I think you can always do something like []<typename = std::enable_if<sizeof...(Types) == 0>::value> {} and that should have the same functionality. Do you or Erich plan to open a CWG issue about this? |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 | FYI I agree with @royjacobson on the current wording interpretation. |
clang/include/clang/Basic/DiagnosticParseKinds.td | ||
---|---|---|
1053–1057 |
I was contemplating it, but after thinking on it more overnight, I think we should accept this patch as-is. I believe it would be safe and plausible to lift this restriction later if CWG or EWG decides to move in this space, but the amount of utility gained by supporting it now is pretty minimal. Thanks for the discussion around this! | |
clang/lib/Frontend/InitPreprocessor.cpp | ||
698 | Because we're exposing the feature as a language extension in older language modes, I think we probably want to define the macro in older language modes as well, right? | |
clang/lib/Parse/ParseExprCXX.cpp | ||
1174 | coding style nit. | |
1265 | FWIW, I'm not sure the note adds a whole lot of value. @cor3ntin, do you have a code example in mind where you think the note would be clarifying? | |
clang/lib/Sema/SemaDeclCXX.cpp | ||
15391–15393 | ||
clang/test/Parser/cxx2b-lambdas-ext-warns.cpp | ||
2–4 | You weren't using the expected prefix in the test. |
I think I didn't get the overload resolution changes quite right, so this will probably change a bit.
clang/lib/Frontend/InitPreprocessor.cpp | ||
---|---|---|
698 | Because supporting static lambdas requires backporting the overload resolution change as well, I decided not to support it as an extension, and for consistency no regular static operator() as well. I've asked now on the libcxx Discord and they seem to think that it would still be useful to provide the regular static operator() as an extension, especially for C++20 ranges. I think backporting the overload resolution change should be fine, but it seems I haven't implemented it quite right so I'm looking into it. |
- Fix overload resolution and apply it retroactively
- Allow static lambdas and static operator() as extensions
- Define the feature macros in earlier versions and fix the tests
GCC have implemented this yesterday, and they also apply the overload resolution change retroactively. So I've done it as well and downgraded the errors to ExtWarns.
I'm not sure that the change to the conversion sequences in the overload resolution is the best way to implement this but it was the cleanest way I found..
I only left a few comments because I'm not familiar enough with that part of the code to be certain the changes are correct but overall it looks fine to me.
clang/include/clang/Sema/Overload.h | ||
---|---|---|
524–526 | ||
clang/lib/Sema/SemaOverload.cpp | ||
7023 | ||
7026 | Can we move that outside of the scope to avoid duplication? |
@aaron.ballman Can you give one last pass at this? The new conversions rules is not something I feel confident blessing (ie I'm not qualified).
Double-checking my understanding of the overload resolution changes: we added a new conversion sequence, but we don't expect that conversion sequence to cause a change in overload resolution in practice? (I'm wondering if there's test coverage we should be adding for those changes.)
clang/lib/Parse/ParseExprCXX.cpp | ||
---|---|---|
1265 | I think we should remove the note here -- we can add a note back when we run into a use case where it would be enlightening. | |
clang/lib/Sema/SemaOverload.cpp | ||
7027–7029 | Can you replace this with: Candidate.Conversions[FirstConvIdx].setStaticObjectArgument(); and get the same behavior? | |
clang/test/Lexer/cxx-features.cpp | ||
42–48 | Unrelated changes? (Feel free to land as an NFC change separately though, they look like sensible changes.) | |
clang/test/SemaCXX/lambda-unevaluated.cpp | ||
125 | We can remove this now, right? |
clang/lib/Parse/ParseExprCXX.cpp | ||
---|---|---|
1265 | I was just thinking that with sufficient attributes, templates, require clauses and parameters, the lambda might be cluttered enough that a note would not hurt. I'm not strongly attached to it though |
Yeah. The change only allow previously ambiguous calls to now not be ambiguous. Add to that that static and non-static methods can't overload each other, and the fact that GCC have applied this retroactively as well, I think it's pretty safe to just use the usual regression tests. (https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=303976a6076f2839354702fd2caa049fa7cbbdc2)
This is a bit confusing -- we're saying it's an extension but then making it an error? I've reworded based on how I think you're intending it to be issued.