-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[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
Conversation
@llvm/pr-subscribers-clang Author: Krystian Stasiowski (sdkrystian) ChangesClang currently allows the following: auto x = requires (this int) { true; }; This patch addresses that. Full diff: https://github.com/llvm/llvm-project/pull/88974.diff 4 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 76701dc723b6c3..8a90c45a9b3638 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -366,6 +366,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
----------------------------------
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index bb9ca2a50cc06c..217ea9380f3b99 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -373,6 +373,8 @@ def err_requires_clause_must_appear_after_trailing_return : Error<
"trailing return type must appear before trailing requires clause">;
def err_requires_clause_on_declarator_not_declaring_a_function : Error<
"trailing requires clause can only be used when declaring a function">;
+def err_requires_clause_explicit_object_parameter: Error<
+ "a requires clause cannot have an explicit object parameter">;
def err_requires_clause_inside_parens : Error<
"trailing requires clause should be placed outside parentheses">;
def ext_auto_storage_class : ExtWarn<
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index 2b934234b7cf5d..f9e2ab073e9719 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -7659,8 +7659,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_clause_explicit_object_parameter);
+ }
ParseDeclarationSpecifiers(DS, /*TemplateInfo=*/ParsedTemplateInfo(),
AS_none, DeclSpecContext::DSC_normal,
diff --git a/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-cxx23.cpp b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-cxx23.cpp
new file mode 100644
index 00000000000000..d31e68acb64ac5
--- /dev/null
+++ b/clang/test/CXX/dcl.decl/dcl.meaning/dcl.fct/p6-cxx23.cpp
@@ -0,0 +1,4 @@
+// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s
+
+auto x0 = requires (this int) { true; }; // expected-error {{a requires clause cannot have an explicit object parameter}}
+auto x1 = requires (int, this int) { true; }; // expected-error {{a requires clause cannot have an explicit object parameter}}
|
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.
Thanks for spotting that. I think there are further cases that I'm not sure we protect against
// RUN: %clang_cc1 -std=c++23 -fsyntax-only -verify %s | ||
|
||
auto x0 = requires (this int) { true; }; // expected-error {{a requires clause cannot have an explicit object parameter}} | ||
auto x1 = requires (int, this int) { true; }; // expected-error {{a requires clause cannot have an explicit object parameter}} |
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.
Can you add a test for template <this auto> void f()
?
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.
@cor3ntin sure, but non-type template parameter parsing doesn't use ParseParameterDeclarationClause
-- it is done in ParseNonTypeTemplateParameter
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.
LGTM
5872c1b
to
2fd0a7c
Compare
2fd0a7c
to
586649a
Compare
…ssion parameter. (#110773) This patch is a follow-up to #109831. In the discussion, we agreed that having parameter checks scattered across different areas isn't ideal. Therefore, I suggest merging the check from #88974 into the void parameter check. This change won't impact functionality and will enhance maintainability.
…ssion parameter. (llvm#110773) This patch is a follow-up to llvm#109831. In the discussion, we agreed that having parameter checks scattered across different areas isn't ideal. Therefore, I suggest merging the check from llvm#88974 into the void parameter check. This change won't impact functionality and will enhance maintainability.
Clang currently allows the following:
This patch addresses that.