This is an archive of the discontinued LLVM Phabricator instance.

Improve requirement clause limitation on non templated function
ClosedPublic

Authored by erichkeane on Mar 28 2023, 11:16 AM.

Details

Summary

The current implementation 6da3d66f03f9162ef341cc67218be40e22fe9808
got a few things wrong, particularly that a template, or definition
or member in a templated entity is required to be allowed to have a
trailing requires clause.

This patch corrects this, as reproted by #61748

Fixes: #61748

Diff Detail

Event Timeline

erichkeane created this revision.Mar 28 2023, 11:16 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 28 2023, 11:16 AM
erichkeane requested review of this revision.Mar 28 2023, 11:16 AM
erichkeane added inline comments.Mar 28 2023, 11:19 AM
clang/lib/Sema/SemaDecl.cpp
11904

Note we're using isTemplated here as a proxy for 'in a templated entity', since isTemplated is effectively `is a template OR in a templated entity'.

LGTM except the arguably missing tests.

clang/lib/Sema/SemaDecl.cpp
11892–11893

I think we have no test for this case, might be worth adding.

cor3ntin accepted this revision.Mar 28 2023, 11:52 AM
This revision is now accepted and ready to land.Mar 28 2023, 11:52 AM
erichkeane added inline comments.Mar 28 2023, 11:55 AM
clang/lib/Sema/SemaDecl.cpp
11892–11893

Interestingly, we permit that, but do NOT diagnose the lambda with a requires clause in a non-template function. Hrm....

Took Corentin's advice and tried to test the lambda condition. THIS made me discover that we didn't properly implement this for lambdas at all! So this patch NOW also implements the restriction for lambdas.

@cor3ntin : Please double check on this for me? I had to touch some lambda tests you worked on.

erichkeane added inline comments.Mar 28 2023, 12:42 PM
clang/test/Parser/cxx2b-lambdas.cpp
21

This still parses correctly, so I consider the error here to be 'correct' and still testing what we wanted.

clang/test/SemaCXX/lambda-capture-type-deduction.cpp
52

When this isn't a template, all of the requires clauses below are ill-formed. So made this a function template that hopefully @cor3ntin agrees matches his intent when he wrote this test.

Took Corentin's advice and tried to test the lambda condition. THIS made me discover that we didn't properly implement this for lambdas at all! So this patch NOW also implements the restriction for lambdas.

@cor3ntin : Please double check on this for me? I had to touch some lambda tests you worked on.

Wow!
I think the patch still makes sense, I'm certainly glad we tested the lambda closure case!

clang/test/SemaCXX/lambda-capture-type-deduction.cpp
52

It does, thanks!
It's weird that i didn't notice the bug when writing those tests - at the same time i always felt this restriction to be a bit artificial.

Thanks for the double check on the review! I'm going to wait for the libcxx bot to come back (and for it to not be a commit at end of day!), but then will commit (unless there is new info).

clang/test/SemaCXX/lambda-capture-type-deduction.cpp
52

Oh, absolutely. Some of the limitations (on function defined inside of a function template that cannot be defined) are sensible in that they are otherwise unusable.

I suspect the reasoning is that else we have to start mangling requires clauses even on non templates, AND it makes extern "C" not work for functions with requires clauses.

shafik accepted this revision.Mar 28 2023, 2:38 PM

LGTM

clang/lib/Sema/SemaDecl.cpp
11883

The cross reference was fixed: https://github.com/cplusplus/draft/pull/6215

shafik added inline comments.Mar 28 2023, 4:21 PM
clang/test/CXX/dcl.decl/dcl.decl.general/p4-20.cpp
27

Maybe a union case just for completeness:

template <typename T>
union U {
    void f() requires true;
};
This revision was landed with ongoing or failed builds.Mar 29 2023, 6:27 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2023, 6:27 AM