Skip to content
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

[Clang][Sema] Check the number of lambda non-concept tempate parameters #74885

Closed
wants to merge 19 commits into from

Conversation

knightXun
Copy link
Contributor

Check that the number of non-concept template parameters is greater than zero during lambda template instantiation to aviod panic Fix issue: #70601

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 8, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 8, 2023

@llvm/pr-subscribers-clang

Author: flyingcat (knightXun)

Changes

Check that the number of non-concept template parameters is greater than zero during lambda template instantiation to aviod panic Fix issue: #70601


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

2 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticParseKinds.td (+2)
  • (modified) clang/lib/Sema/TreeTransform.h (+14)
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 6150fc36430ab1..e46fa69d013b61 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -851,6 +851,8 @@ def err_friend_explicit_instantiation : Error<
 def err_explicit_instantiation_enum : Error<
   "enumerations cannot be explicitly instantiated">;
 def err_expected_template_parameter : Error<"expected template parameter">;
+def err_expected_non_concept_template_parameter : Error<
+  "expected non-concept template parameter">;
 def err_empty_requires_expr : Error<
   "a requires expression must contain at least one requirement">;
 def err_requires_expr_parameter_list_ellipsis : Error<
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index 1ad843d0bf4e0c..110024a377128b 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -13594,6 +13594,20 @@ TreeTransform<Derived>::TransformLambdaExpr(LambdaExpr *E) {
     getSema().AddTemplateParametersToLambdaCallOperator(NewCallOperator, Class,
                                                         TPL);
 
+  // Check the number of the Concept template parameters
+  size_t conceptParams = 0;
+  for (auto P : *E->getTemplateParameterList()) {
+    const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
+    if (CD && CD->hasTypeConstraint()) {
+      conceptParams++;
+    }
+  }
+
+  if (conceptParams == E->getTemplateParameterList()->size()) {
+    getSema().Diag(E->getTemplateParameterList()->getLAngleLoc(),
+                   diag::err_expected_non_concept_template_parameter);
+    return ExprError();
+  }
   // Transform the type of the original lambda's call operator.
   // The transformation MUST be done in the CurrentInstantiationScope since
   // it introduces a mapping of the original to the newly created

Check that the number of non-concept template parameters is greater than zero during lambda template instantiation to aviod panic
Fix issue: llvm#70601
@knightXun knightXun force-pushed the fix-issue-70601 branch 2 times, most recently from db02522 to 12cc1fe Compare December 8, 2023 23:06
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

This needs at least one test and a Release note.

I expect the code form the issue to be well-formed: https://godbolt.org/z/bhdfG34xc

So I am curious why you are adding the diagnostic for?

@knightXun
Copy link
Contributor Author

This needs at least one test and a Release note.

I expect the code form the issue to be well-formed: https://godbolt.org/z/bhdfG34xc

So I am curious why you are adding the diagnostic for?

thanks for the hit!

Copy link

github-actions bot commented Dec 11, 2023

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 44dc1e0baae7c4b8a02ba06dcf396d3d452aa873 c3eecf5b35da2acb44bc06d2e6663222c18c867f -- clang/lib/Sema/SemaConcept.cpp clang/lib/Sema/SemaTemplateDeduction.cpp clang/lib/Sema/TreeTransform.h
View the diff from clang-format here.
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 954ca488a0..20e7ea3dcc 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -4352,17 +4352,17 @@ Sema::TemplateDeductionResult Sema::DeduceTemplateArguments(
   const auto *Proto = Function->getType()->castAs<FunctionProtoType>();
   if (!Proto->isTemplateVariadic()) {
     size_t params = 0, deducedParams = 0;
-    for(auto P : *TemplateParams) {
+    for (auto P : *TemplateParams) {
       const TemplateTypeParmDecl *CD = dyn_cast<TemplateTypeParmDecl>(P);
-      if(CD && CD->hasTypeConstraint()) {
+      if (CD && CD->hasTypeConstraint()) {
         params++;
       }
     }
-    for( auto P : Deduced ) {
+    for (auto P : Deduced) {
       deducedParams++;
     }
 
-    if( params > deducedParams ) {
+    if (params > deducedParams) {
       return TDK_Invalid;
     }
   }

@knightXun knightXun closed this Dec 12, 2023
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.

3 participants