Skip to content

[Clang][NFC] Consolidate the parameter check for the requires expression parameter. #110773

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
Oct 2, 2024

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Oct 2, 2024

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.

@c8ef c8ef changed the title Draft [Clang][NFC] Consolidate the parameter check for the 'requires' expression parameter. Oct 2, 2024
@c8ef c8ef changed the title [Clang][NFC] Consolidate the parameter check for the 'requires' expression parameter. [Clang][NFC] Consolidate the parameter check for the requires expression parameter. Oct 2, 2024
@c8ef c8ef marked this pull request as ready for review October 2, 2024 02:24
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 2, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2024

@llvm/pr-subscribers-clang

Author: None (c8ef)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/110773.diff

2 Files Affected:

  • (modified) clang/lib/Parse/ParseDecl.cpp (+1-14)
  • (modified) clang/lib/Sema/SemaExprCXX.cpp (+17-4)
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index a04eed9873c0d4..122a05be1c039a 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -7950,21 +7950,8 @@ 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);
-    }
 
     ParsedTemplateInfo TemplateInfo;
     ParseDeclarationSpecifiers(DS, TemplateInfo, AS_none,
diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp
index b30414a8a8277a..d490452e91c3bb 100644
--- a/clang/lib/Sema/SemaExprCXX.cpp
+++ b/clang/lib/Sema/SemaExprCXX.cpp
@@ -9519,15 +9519,28 @@ Sema::ActOnStartRequiresExpr(SourceLocation RequiresKWLoc,
       } else if (Param->getType().hasQualifiers()) {
         Diag(Param->getBeginLoc(), diag::err_void_param_qualified);
       }
-    }
-
-    if (Param->hasDefaultArg())
+    } else if (Param->hasDefaultArg()) {
       // C++2a [expr.prim.req] p4
       //     [...] A local parameter of a requires-expression shall not have a
       //     default argument. [...]
       Diag(Param->getDefaultArgRange().getBegin(),
            diag::err_requires_expr_local_parameter_default_argument);
-    // Ignore default argument and move on
+      // Ignore default argument and move on
+    } else if (Param->isExplicitObjectParameter()) {
+      // 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.
+      Diag(Param->getExplicitObjectParamThisLoc(),
+           diag::err_requires_expr_explicit_object_parameter);
+      Param->setExplicitObjectParameterLoc(SourceLocation());
+    }
 
     Param->setDeclContext(Body);
     // If this has an identifier, add it to the scope stack.

@c8ef
Copy link
Contributor Author

c8ef commented Oct 2, 2024

CC @zyn0217 @mizvekov @cor3ntin

@c8ef
Copy link
Contributor Author

c8ef commented Oct 2, 2024

Thank you so much for your quick review! However, it seems the current CI is blocked by the issues mentioned in #110783 :(

@mizvekov
Copy link
Contributor

mizvekov commented Oct 2, 2024

Thank you so much for your quick review! However, it seems the current CI is blocked by the issues mentioned in #110783 :(

I wouldn't be worried about that for a simple change like this, you can go ahead and merge.

@zyn0217 zyn0217 merged commit 679be52 into llvm:main Oct 2, 2024
10 of 12 checks passed
@c8ef c8ef deleted the require branch October 2, 2024 04:02
@c8ef
Copy link
Contributor Author

c8ef commented Oct 2, 2024

Thanks! 🥰

// a context.
Diag(Param->getExplicitObjectParamThisLoc(),
diag::err_requires_expr_explicit_object_parameter);
Param->setExplicitObjectParameterLoc(SourceLocation());
Copy link
Contributor

Choose a reason for hiding this comment

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

Why clear the source location? Wouldn't it somehow break the source fidelity?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

if (Param->isExplicitObjectParameter()) {
S.Diag(Param->getLocation(),
diag::err_void_explicit_object_param);
Param->setExplicitObjectParameterLoc(SourceLocation());
}

I'm not entirely sure, but the check in function parameters do indeed perform this action.

Sterling-Augustine pushed a commit to Sterling-Augustine/llvm-project that referenced this pull request Oct 3, 2024
…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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants