Skip to content
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

Added support for requires() constraints on methods that are not directly templated #86

Closed
wants to merge 2 commits into from

Conversation

justinboswell
Copy link
Contributor

Currently, cxxheaderparser can only parse requires() constraints on methods which are also templated. However, under C++20, requires statements can also exist on un-templated methods in a templated class as well, this is usually used to replace std::enable_if<> function toggling.

@justinboswell
Copy link
Contributor Author

pytest output:

(.venv) PS C:\dev\cxxheaderparser> python -m pytest
====================================================================================== test session starts =======================================================================================
platform win32 -- Python 3.10.11, pytest-7.4.3, pluggy-1.3.0
rootdir: C:\dev\cxxheaderparser
collected 292 items

tests\test_abv_template.py .....                                                                                                                                                            [  1%]
tests\test_attributes.py ....                                                                                                                                                               [  3%]
tests\test_class.py ..............................................                                                                                                                          [ 18%]
tests\test_class_base.py ....                                                                                                                                                               [ 20%]
tests\test_concepts.py .............                                                                                                                                                        [ 24%]
tests\test_doxygen.py ............                                                                                                                                                          [ 28%]
tests\test_enum.py ...............                                                                                                                                                          [ 33%]
tests\test_fn.py ............................                                                                                                                                               [ 43%]
tests\test_friends.py ....                                                                                                                                                                  [ 44%]
tests\test_misc.py .............                                                                                                                                                            [ 49%]
tests\test_namespaces.py ......                                                                                                                                                             [ 51%]
tests\test_operators.py ....                                                                                                                                                                [ 52%]
tests\test_preprocessor.py sssssssssssss                                                                                                                                                    [ 57%]
tests\test_skip.py ...                                                                                                                                                                      [ 58%]
tests\test_template.py ...................                                                                                                                                                  [ 64%]
tests\test_tokfmt.py ..........................                                                                                                                                             [ 73%]
tests\test_typedef.py .......................                                                                                                                                               [ 81%]
tests\test_typefmt.py .................                                                                                                                                                     [ 87%]
tests\test_union.py ..                                                                                                                                                                      [ 88%] 
tests\test_using.py ...........                                                                                                                                                             [ 91%]
tests\test_var.py ........................                                                                                                                                                  [100%]

================================================================================ 279 passed, 13 skipped in 2.05s ================================================================================= 

struct Payload
{
constexpr Payload(T v)
requires(std::is_pod_v<T>)
Copy link
Member

Choose a reason for hiding this comment

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

The requires statement gets completely discarded here, whereas in other contexts the requires is retained as a raw Value? Even though we aren't doing anything with the data at the moment, this is inconsistent and could be a problem if we ever decide to do something with the data.

It seems that the best way to deal with this would be to move raw_requires_post into Function instead of being in TemplateDecl? Even though that would be a breaking change, since this is mostly unparsed anyways I don't think it's that big of a deal.

Copy link
Contributor Author

@justinboswell justinboswell Dec 1, 2023

Choose a reason for hiding this comment

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

It's not entirely discarded, right? It ends up as the raw_requires_post on a TemplateDecl with no template args, which was the sanest way I could think to represent it at a glance.

That said, it probably does make sense to move raw_requires_post to Function. I actually looked at doing that first before I realized what the parsing issue was. I've only taken a cursory dive into the parser but it seemed initially like it might be a much bigger/far-reaching change to parsing.

Now that I've spent more time in there (working on #87), It might be doable right around where trailing return types are parsed in _parse_fn_end and _parse_method_end. Would it be preferable if I put a peek in those spots looking for requires, parse it, and store the result on Function?

If so, just let me know, and I'll take a run at it!

Copy link
Member

Choose a reason for hiding this comment

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

It isn't discarded immediately, but eventually the TemplateDecl isn't used so it is discarded. If it wasn't discarded, then the requires(std::is_pod_v<T>) would be in the testcase's ParsedData somewhere, and it isn't.

When I was looking at this I noticed a simpler way to do this, so I'm going to close this in favor of doing it that way.

Copy link
Member

Choose a reason for hiding this comment

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

... which was exactly what you suggested, ha

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh sweet, thank you!

@virtuald
Copy link
Member

virtuald commented Dec 2, 2023

Closed in favor of #89, please take a look and let me know if that works for you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants