-
Notifications
You must be signed in to change notification settings - Fork 11.8k
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
Conversation
@llvm/pr-subscribers-clang Author: flyingcat (knightXun) ChangesCheck 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:
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
|
05588ae
to
4b5f4c6
Compare
Check that the number of non-concept template parameters is greater than zero during lambda template instantiation to aviod panic Fix issue: llvm#70601
db02522
to
12cc1fe
Compare
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.
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! |
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;
}
}
|
Check that the number of non-concept template parameters is greater than zero during lambda template instantiation to aviod panic Fix issue: #70601