[clang] [clang] Fix desugaring when pack indexing selects a dependent type (PR #249454)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Dec 6 09:22:00 PST 2026
https://github.com/nipunkalsotra created https://github.com/llvm/llvm-project/pull/249454
Fixes #249289
`PackIndexingType::getSelectedIndex()` could report no selected index even after `ASTContext` had canonicalized the node to the selected type. This happened when the pack was fully substituted and the index was known, but the selected type still depended on a template parameter.
The fix checks the pack’s substitution state and whether the index expression is instantiation-dependent. This keeps desugaring consistent with canonicalization.
Testing
- Added a regression test in `clang/test/SemaCXX/cxx2c-pack-indexing.cpp`. It fails without the fix and passes with it. It also checks that the cv/ref-qualified function type diagnostic still fires.
- All 21 focused tests passed. The broader test run had the same 63 environment-related failures before and after the fix.
AI assistance: Claude Code helped implement and test the patch. ChatGPT helped draft this description.
>From b127e0c9c80717e5c9c0c7fb1a81782287342137 Mon Sep 17 00:00:00 2001
From: Nipun Kalsotra <kalsotranipun at gmail.com>
Date: Sun, 6 Dec 2026 22:36:39 +0530
Subject: [PATCH] [clang] Fix desugaring when pack indexing selects a dependent
type
---
clang/lib/AST/Type.cpp | 3 ++-
clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 11 +++++++++++
2 files changed, 13 insertions(+), 1 deletion(-)
diff --git a/clang/lib/AST/Type.cpp b/clang/lib/AST/Type.cpp
index 4e4c4a37ecff71..c6a75aefeb22bd 100644
--- a/clang/lib/AST/Type.cpp
+++ b/clang/lib/AST/Type.cpp
@@ -4533,7 +4533,8 @@ PackIndexingType::PackIndexingType(QualType Canonical, QualType Pattern,
}
UnsignedOrNone PackIndexingType::getSelectedIndex() const {
- if (isInstantiationDependentType())
+ // The selected type can still be dependent even when the index is known.
+ if (!isFullySubstituted() || getIndexExpr()->isInstantiationDependent())
return std::nullopt;
// Should only be not a constant for error recovery.
ConstantExpr *CE = dyn_cast<ConstantExpr>(getIndexExpr());
diff --git a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
index 910160dbf683d9..44ada7e642dd33 100644
--- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
+++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp
@@ -404,3 +404,14 @@ void test() {
// expected-note@#GH232220-f {{candidate template ignored: substitution failure [with T = <int>]: invalid index 1 for pack 'T' of size 1}}
}
} // namespace GH232220
+
+namespace GH249289 {
+template <class... Ts> using First = Ts...[0];
+template <class T> using Function = First<auto() -> T>;
+template <class T> using QualifiedFunction = First<auto() const & -> T>;
+
+static_assert(is_same<Function<int>, int()>);
+static_assert(is_same<QualifiedFunction<int>, int() const &>);
+
+QualifiedFunction<int> f; // expected-error {{cannot have 'const &' qualifier}}
+} // namespace GH249289
More information about the cfe-commits
mailing list