Skip to content

[Clang][Parse] Diagnose requires expressions with explicit object parameters #88974

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 3 commits into from
Apr 17, 2024
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,8 @@ Improvements to Clang's diagnostics
- Clang now uses the correct type-parameter-key (``class`` or ``typename``) when printing
template template parameter declarations.

- Clang now diagnoses requires expressions with explicit object parameters.

Improvements to Clang's time-trace
----------------------------------

Expand Down
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/DiagnosticParseKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,8 @@ def err_empty_requires_expr : Error<
"a requires expression must contain at least one requirement">;
def err_requires_expr_parameter_list_ellipsis : Error<
"varargs not allowed in requires expression">;
def err_requires_expr_explicit_object_parameter: Error<
"a requires expression cannot have an explicit object parameter">;
def err_expected_semi_requirement : Error<
"expected ';' at end of requirement">;
def err_requires_expr_missing_arrow : Error<
Expand Down
15 changes: 14 additions & 1 deletion clang/lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7660,8 +7660,21 @@ void Parser::ParseParameterDeclarationClause(
// Parse a C++23 Explicit Object Parameter
// We do that in all language modes to produce a better diagnostic.
SourceLocation ThisLoc;
if (getLangOpts().CPlusPlus && Tok.is(tok::kw_this))
if (getLangOpts().CPlusPlus && Tok.is(tok::kw_this)) {
ThisLoc = ConsumeToken();
// C++23 [dcl.fct]p6:
// An explicit-object-parameter-declaration is a parameter-declaration
// with a this specifier. An explicit-object-parameter-declaration
// shall appear only as the first parameter-declaration of a
// parameter-declaration-list of either:
// - a member-declarator that declares a member function, or
// - a lambda-declarator.
//
// The parameter-declaration-list of a requires-expression is not such
// a context.
if (DeclaratorCtx == DeclaratorContext::RequiresExpr)
Diag(ThisLoc, diag::err_requires_expr_explicit_object_parameter);
}

ParseDeclarationSpecifiers(DS, /*TemplateInfo=*/ParsedTemplateInfo(),
AS_none, DeclSpecContext::DSC_normal,
Expand Down
7 changes: 7 additions & 0 deletions clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-cxx23.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s

auto x0 = requires (this int) { true; }; // expected-error {{a requires expression cannot have an explicit object parameter}}
auto x1 = requires (int, this int) { true; }; // expected-error {{a requires expression cannot have an explicit object parameter}}

template<this auto> // expected-error {{expected template parameter}}
void f(); // expected-error {{no function template matches function template specialization 'f'}}