[clang] [Clang] Diagnose unexpanded packs for NTTP type constraints (PR #121296)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Dec 30 01:54:27 PST 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Younan Zhang (zyn0217)
<details>
<summary>Changes</summary>
Otherwise, the invalid unexpanded type would be passed to getAutoType and lead to a crash.
Fixes https://github.com/llvm/llvm-project/issues/88866
---
Full diff: https://github.com/llvm/llvm-project/pull/121296.diff
3 Files Affected:
- (modified) clang/docs/ReleaseNotes.rst (+1)
- (modified) clang/lib/Sema/SemaTemplate.cpp (+10-2)
- (modified) clang/test/SemaCXX/cxx2c-fold-exprs.cpp (+42)
``````````diff
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 983c1da20ed4c8..db40a722ce1ed8 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -886,6 +886,7 @@ Bug Fixes to C++ Support
out of a module (which is the case e.g. in MSVC's implementation of ``std`` module). (#GH118218)
- Fixed a pack expansion issue in checking unexpanded parameter sizes. (#GH17042)
- Fixed a bug where captured structured bindings were modifiable inside non-mutable lambda (#GH95081)
+- Clang now identifies unexpanded parameter packs within the type constraint on a non-type template parameter. (#GH88866)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp
index 5e7a3c8484c88f..eac184d468ce9a 100644
--- a/clang/lib/Sema/SemaTemplate.cpp
+++ b/clang/lib/Sema/SemaTemplate.cpp
@@ -1530,9 +1530,17 @@ NamedDecl *Sema::ActOnNonTypeTemplateParameter(Scope *S, Declarator &D,
Param->setAccess(AS_public);
if (AutoTypeLoc TL = TInfo->getTypeLoc().getContainedAutoTypeLoc())
- if (TL.isConstrained())
- if (AttachTypeConstraint(TL, Param, Param, D.getEllipsisLoc()))
+ if (TL.isConstrained()) {
+ if (const ASTTemplateArgumentListInfo *ArgumentList =
+ TL.getConceptReference()->getTemplateArgsAsWritten())
+ for (const TemplateArgumentLoc &Loc : ArgumentList->arguments()) {
+ Invalid |= DiagnoseUnexpandedParameterPack(
+ Loc, UnexpandedParameterPackContext::UPPC_TypeConstraint);
+ }
+ if (!Invalid &&
+ AttachTypeConstraint(TL, Param, Param, D.getEllipsisLoc()))
Invalid = true;
+ }
if (Invalid)
Param->setInvalidDecl();
diff --git a/clang/test/SemaCXX/cxx2c-fold-exprs.cpp b/clang/test/SemaCXX/cxx2c-fold-exprs.cpp
index 0674135aac483f..509922c0a8f4fe 100644
--- a/clang/test/SemaCXX/cxx2c-fold-exprs.cpp
+++ b/clang/test/SemaCXX/cxx2c-fold-exprs.cpp
@@ -305,3 +305,45 @@ static_assert(__is_same_as(_Three_way_comparison_result_with_tuple_like<tuple<in
static_assert(__is_same_as(_Three_way_comparison_result_with_tuple_like<tuple<int>, 0>::type, long));
}
+
+namespace GH88866 {
+
+template <typename...Ts> struct index_by;
+
+template <typename T, typename Indices>
+concept InitFunc = true;
+
+namespace Invalid {
+
+template <typename Indices, InitFunc<Indices> auto... init>
+struct LazyLitMatrix;
+
+template <
+ typename...Indices,
+ InitFunc<index_by<Indices>> auto... init
+ // expected-error at -1 {{type constraint contains unexpanded parameter pack 'Indices'}}
+>
+struct LazyLitMatrix<index_by<Indices...>, init...> {
+};
+
+using T = LazyLitMatrix<index_by<int, char>, 42, 43>;
+
+}
+
+namespace Valid {
+
+template <typename Indices, InitFunc<Indices> auto... init>
+struct LazyLitMatrix;
+
+template <
+ typename...Indices,
+ InitFunc<index_by<Indices...>> auto... init
+>
+struct LazyLitMatrix<index_by<Indices...>, init...> {
+};
+
+using T = LazyLitMatrix<index_by<int, char>, 42, 43>;
+
+}
+
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/121296
More information about the cfe-commits
mailing list