Skip to content

fix(parse): requires clause on one-liner and add error when used on non-template #528

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

Conversation

filipsajdak
Copy link
Contributor

  • Add handling of lack of empty line after oneliner.
  • Add parsing error when requires is used on non-template

All regression tests pass.

Closes #472

Add handling of lack of empty line after oneliner.
Add parsing error when requires is used on non-template
source/parse.h Outdated
Comment on lines 6470 to 6473
if (!n->template_parameters) {
error("'requires' only valid for a template");
return {};
}
Copy link
Contributor

Choose a reason for hiding this comment

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

It's also valid for templated entities (non-templates within templates).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Could you provide an example?

Copy link
Contributor

Choose a reason for hiding this comment

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

t: <T> type = {
  f: () requires std::regular<T> = { }
}

Copy link
Owner

Choose a reason for hiding this comment

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

Also, I think the above won't handle the case of generic parameters that use wildcards instead of an explicit template parameter list? Such as

f: (x) requires std::regular<decltype(x)> = { }

Copy link
Owner

@hsutter hsutter Aug 11, 2023

Choose a reason for hiding this comment

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

I do think there's value in diagnosing a requires on a non-templated type for type declarations.

Rationale:

  • writing a stray requires on a function already gives a nice Cpp1 diagnostic (e.g., MSVC says error C7599: 't::f': a trailing requires clause is only allowed on a templated function)
  • writing a stray requires on a type currently lowers it to a naked requires at global scope before the type which leads to a harder-to-understand Cpp1 diagnostic (e.g., MSVC says error C2059: syntax error: 'requires', which is correct and indicates the rough issue, but it's not as specific)

So diagnosing the type case in Cpp2 delivers the primary diagnostic usability improvement, plus I think it's easy to code, I think it just needs adding n->is_type() && in the condition Filip already wrote.

I'll modify the PR to try that out...

Restrict new check to types
@hsutter
Copy link
Owner

hsutter commented Aug 11, 2023

Thanks!

@hsutter hsutter merged commit 0f427e9 into hsutter:main Aug 11, 2023
zaucy pushed a commit to zaucy/cppfront that referenced this pull request Dec 5, 2023
…on-template (hsutter#528)

* Fix parsing error of requires clause

Add handling of lack of empty line after oneliner.
Add parsing error when requires is used on non-template

* Update parse.h

Restrict new check to types

---------

Co-authored-by: Herb Sutter <[email protected]>
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.

[BUG] Non-empty line after one-liner type with requires clause fails assertion
3 participants