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] Fix crash when recovering from an invalid pack indexing type. #80652

Merged
merged 1 commit into from
Feb 5, 2024

Conversation

cor3ntin
Copy link
Contributor

@cor3ntin cor3ntin commented Feb 5, 2024

If the pattern of a pack indexing type did not contain a pack, we would still construct a pack indexing type (to improve error messages) but we would fail to make the type as dependent, leading to infinite recursion when trying to extract a canonical type.

If the pattern of a pack indexing type did not contain a pack,
we would still construct a pack indexing type (to improve error messages)
but we would fail to make the type as dependent, leading to infinite
recursion when trying to extract a canonical type.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 5, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 5, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

If the pattern of a pack indexing type did not contain a pack, we would still construct a pack indexing type (to improve error messages) but we would fail to make the type as dependent, leading to infinite recursion when trying to extract a canonical type.


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

2 Files Affected:

  • (modified) clang/lib/AST/Type.cpp (+6)
  • (modified) clang/test/SemaCXX/cxx2c-pack-indexing.cpp (+15)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 11ca02be13ab4..c68254a459ccc 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -3854,6 +3854,12 @@ PackIndexingType::computeDependence(QualType Pattern, Expr *IndexExpr,
 
   if (!(IndexD & TypeDependence::UnexpandedPack))
     TD &= ~TypeDependence::UnexpandedPack;
+
+  // If the pattern does not contain an unexpended pack,
+  // the type is still dependent, and invalid
+  if (!Pattern->containsUnexpandedParameterPack())
+    TD |= TypeDependence::Error | TypeDependence::DependentInstantiation;
+
   return TD;
 }
 
diff --git a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
index bd75c1180a1c1..625a56031598b 100644
--- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
+++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
@@ -11,6 +11,21 @@ void not_pack() {
     Tp...[0] c; // expected-error{{'Tp' does not refer to the name of a parameter pack}}
 }
 
+template <typename T, auto V, template<typename> typename Tp>
+void not_pack_arrays() {
+    NotAPack...[0] a[1]; // expected-error{{'NotAPack' does not refer to the name of a parameter pack}}
+    T...[0] b[1];   // expected-error{{'T' does not refer to the name of a parameter pack}}
+    Tp...[0] c[1]; // expected-error{{'Tp' does not refer to the name of a parameter pack}}
+}
+
+template <typename T>
+struct TTP;
+
+void test_errors() {
+    not_pack<int, 0, TTP>();
+    not_pack_arrays<int, 0, TTP>();
+}
+
 namespace invalid_indexes {
 
 int non_constant_index(); // expected-note 2{{declared here}}

@bjope
Copy link
Collaborator

bjope commented Feb 5, 2024

@cor3ntin : I've verified that this would solve the problem that I noticed downstream. Thanks!

Copy link
Collaborator

@erichkeane erichkeane left a comment

Choose a reason for hiding this comment

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

Seems reasonable.

@cor3ntin cor3ntin merged commit 41ea022 into llvm:main Feb 5, 2024
7 checks passed
@cor3ntin cor3ntin deleted the corentin/fix_pack_indexing branch February 5, 2024 15:28
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
…llvm#80652)

If the pattern of a pack indexing type did not contain a pack, we would
still construct a pack indexing type (to improve error messages) but we
would fail to make the type as dependent, leading to infinite recursion
when trying to extract a canonical type.
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.

None yet

4 participants