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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions regression-tests/test-results/pure2-requires-clauses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
template<typename T, typename U>

requires( std::is_same_v<T,int>
&& std::is_same_v<U,int> )
class X;
&& std::is_same_v<U,int> )class X;

//=== Cpp2 type definitions and function declarations ===========================

Expand All @@ -21,8 +20,8 @@ class X;
template<typename T, typename U>

requires( std::is_same_v<T,int>
&& std::is_same_v<U,int> )
class X {
&& std::is_same_v<U,int> )class X
{
public: explicit X();

public: X(X const&) = delete; /* No 'that' constructor, suppress copy */
Expand Down
2 changes: 1 addition & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5094,7 +5094,7 @@ class cppfront
if (n.requires_clause_expression) {
printer.print_cpp2("requires( ", n.requires_pos);
emit(*n.requires_clause_expression);
printer.print_cpp2(" )\n\n", n.requires_pos);
printer.print_cpp2(" )\n", n.requires_pos);
}

printer.print_cpp2("class ", n.position());
Expand Down
14 changes: 12 additions & 2 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -6466,10 +6466,20 @@ class parser
}

// Next is optionally a requires clause
if (curr() == "requires") {
if (curr() == "requires")
{
if (
n->is_type()
&& !n->template_parameters
)
{
error("'requires' is only valid for a type with a template parameter list");
return {};
}

n->requires_pos = curr().position();
next();
auto e = expression();
auto e = logical_or_expression();
if (!e) {
error("'requires' must be followed by an expression");
return {};
Expand Down