-
Notifications
You must be signed in to change notification settings - Fork 262
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
fix(parse): requires clause on one-liner and add error when used on non-template #528
Conversation
Add handling of lack of empty line after oneliner. Add parsing error when requires is used on non-template
source/parse.h
Outdated
if (!n->template_parameters) { | ||
error("'requires' only valid for a template"); | ||
return {}; | ||
} |
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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> = { }
}
There was a problem hiding this comment.
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)> = { }
There was a problem hiding this comment.
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 sayserror 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 nakedrequires
at global scope before the type which leads to a harder-to-understand Cpp1 diagnostic (e.g., MSVC sayserror 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
Thanks! |
…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]>
All regression tests pass.
Closes #472