[clang] [Clang][Sema][TemplateDeduction] Skip pack expansion type at the end of default template argument list if unneeded (PR #94659)

via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 6 11:59:30 PDT 2024


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (yozhu)

<details>
<summary>Changes</summary>

If default argument list has one more element than actual argument list and the last default argument is a pack expansion, we can ignore it.  This is to fix a failed assertion:

clang: llvm-project/clang/lib/Sema/SemaTemplateDeduction.cpp:596: TemplateDeductionResult DeduceTemplateArguments(Sema &, TemplateParameterList *, TemplateName, TemplateName, TemplateDeductionInfo &, ArrayRef<TemplateArgument>, SmallVectorImpl<DeducedTemplateArgument> &): Assertion `DefaultArguments.size() <= As->size()' failed.

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


1 Files Affected:

- (modified) clang/lib/Sema/SemaTemplateDeduction.cpp (+8-2) 


``````````diff
diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 1011db2d2830d..e66f35c38dfda 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -593,9 +593,15 @@ DeduceTemplateArguments(Sema &S, TemplateParameterList *TemplateParams,
 
       TemplateParameterList *As = TempArg->getTemplateParameters();
       if (DefaultArguments.size() != 0) {
-        assert(DefaultArguments.size() <= As->size());
+        if ((DefaultArguments.size() > As->size()) &&
+            (DefaultArguments.size() != As->size() + 1 ||
+             DefaultArguments.back().getKind() != TemplateArgument::Type ||
+             !isa<PackExpansionType>(DefaultArguments.back().getAsType()))) {
+          return TemplateDeductionResult::TooFewArguments;
+        }
         SmallVector<NamedDecl *, 4> Params(As->size());
-        for (unsigned I = 0; I < DefaultArguments.size(); ++I)
+        for (unsigned I = 0;
+             I < std::min((size_t)As->size(), DefaultArguments.size()); ++I)
           Params[I] = getTemplateParameterWithDefault(S, As->getParam(I),
                                                       DefaultArguments[I]);
         for (unsigned I = DefaultArguments.size(); I < As->size(); ++I)

``````````

</details>


https://github.com/llvm/llvm-project/pull/94659


More information about the cfe-commits mailing list