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 assert when transforming a pack indexing type. #82234

Merged
merged 2 commits into from
Feb 21, 2024

Conversation

cor3ntin
Copy link
Contributor

When a pack in a pack indexing specifier cannot be immediately expanded, we were creating an incomplete TypeLoc
(causing assertion failure).

As we do not keep track of typelocs of expanded elements, we create a trivial typeloc

Fixes #81697

When a pack in a pack indexing specifier cannot be immediately
expanded, we were creating an incomplete TypeLoc
(causing assertion failure).

As we do not keep track of typelocs of expanded elements,
we create a trivial typeloc

Fixes llvm#81697
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Feb 19, 2024
@llvmbot
Copy link
Member

llvmbot commented Feb 19, 2024

@llvm/pr-subscribers-clang

Author: cor3ntin (cor3ntin)

Changes

When a pack in a pack indexing specifier cannot be immediately expanded, we were creating an incomplete TypeLoc
(causing assertion failure).

As we do not keep track of typelocs of expanded elements, we create a trivial typeloc

Fixes #81697


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

2 Files Affected:

  • (modified) clang/lib/Sema/TreeTransform.h (+3-1)
  • (modified) clang/test/SemaCXX/cxx2c-pack-indexing.cpp (+19)
diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h
index a32a585531873a..964ddeefc5a088 100644
--- a/clang/lib/Sema/TreeTransform.h
+++ b/clang/lib/Sema/TreeTransform.h
@@ -6561,7 +6561,9 @@ TreeTransform<Derived>::TransformPackIndexingType(TypeLocBuilder &TLB,
       return QualType();
     if (!ShouldExpand) {
       Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1);
-      QualType Pack = getDerived().TransformType(T);
+      // FIXME: should we keep TypeLoc for individual expansions in PackIndexingTypeLoc?
+      TypeSourceInfo* TI = SemaRef.getASTContext().getTrivialTypeSourceInfo(T, TL.getBeginLoc());
+      QualType Pack = getDerived().TransformType(TLB, TI->getTypeLoc());
       if (Pack.isNull())
         return QualType();
       if (NotYetExpanded) {
diff --git a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
index 625a56031598b7..e13635383b6ca6 100644
--- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
+++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
@@ -135,3 +135,22 @@ using Splice = typename SpliceImpl<Tl, Il>::type;
 using type = Splice<TL<char, short, long, double>, IL<1, 2>>;
 static_assert(is_same<type, TL<short, long>>);
 }
+
+
+namespace GH81697 {
+
+template<class... Ts> struct tuple {
+    int __x0;
+};
+
+template<auto I, class... Ts>
+Ts...[I]& get(tuple<Ts...>& t) {
+  return t.__x0;
+}
+
+void f() {
+  tuple<int> x;
+  get<0>(x);
+}
+
+}

Copy link

github-actions bot commented Feb 19, 2024

✅ With the latest revision this PR passed the C/C++ code formatter.

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.

LGTM

@cor3ntin cor3ntin merged commit 351e4fa into llvm:main Feb 21, 2024
4 checks passed
@cor3ntin cor3ntin deleted the gh81697 branch February 21, 2024 07:46
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.

Pack index as a return type crashes Clang
3 participants