Skip to content

Commit 849d1b8

Browse files
authored
[clang] Do not substitute parameter pack while retaining the pack expansion (#108197)
(In reference to 5901d82) Consider when `Input[I]` is a `VarDecl` with parameter pack. We would have already expanded the pack before the code change in the loop`for (unsigned I = 0; I != *NumExpansions; ++I) {`. Now in `if (RetainExpansion) {`, without this change, we continue to substitute the pack in the pattern even when we do not have meaningful `ArgumentPackSubstitutionIndex` set. This leads to use of an invalid pack substitution index in `TemplateInstantiator::TransformFunctionParmPackRefExpr` in `TransformedDecl = (*Pack)[getSema().ArgumentPackSubstitutionIndex];` This change sets `ArgumentPackSubstitutionIndex` to `-1` while retaining expansion to instruct `TransformFunctionParmPackRefExpr` to build `FunctionParmPackExpr` instead of substituting the param pack. --- There are other instances of `RetainExpansion` and IIUC, they should also unset the `ArgumentPackSubstitutionIndex`. It would be great if someone can verify my understanding. If this is correct then we could instead have a `ArgumentPackSubstitutionIndexRAII` as part of `ForgetPartiallySubstitutedPackRAII`. EDIT: I have moved this to `ForgetPartiallySubstitutedPackRAII`. Fixes #63819 Fixes #107560
1 parent 89c10e2 commit 849d1b8

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,9 @@ Bug Fixes to C++ Support
382382
- Fix a crash when using ``source_location`` in the trailing return type of a lambda expression. (#GH67134)
383383
- A follow-up fix was added for (#GH61460), as the previous fix was not entirely correct. (#GH86361)
384384
- Fixed a crash in the typo correction of an invalid CTAD guide. (#GH107887)
385+
- Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
386+
pack. #GH63819, #GH107560
387+
385388

386389
Bug Fixes to AST Handling
387390
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,13 @@ class TreeTransform {
113113
class ForgetPartiallySubstitutedPackRAII {
114114
Derived &Self;
115115
TemplateArgument Old;
116+
// Set the pack expansion index to -1 to avoid pack substitution and
117+
// indicate that parameter packs should be instantiated as themselves.
118+
Sema::ArgumentPackSubstitutionIndexRAII ResetPackSubstIndex;
116119

117120
public:
118-
ForgetPartiallySubstitutedPackRAII(Derived &Self) : Self(Self) {
121+
ForgetPartiallySubstitutedPackRAII(Derived &Self)
122+
: Self(Self), ResetPackSubstIndex(Self.getSema(), -1) {
119123
Old = Self.ForgetPartiallySubstitutedPack();
120124
}
121125

clang/test/SemaTemplate/pack-deduction.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,17 @@ void Run() {
185185
Outer<void>::Inner<0>().Test(1,1);
186186
}
187187
}
188+
189+
namespace GH107560 {
190+
int bar(...);
191+
192+
template <int> struct Int {};
193+
194+
template <class ...T>
195+
constexpr auto foo(T... x) -> decltype(bar(T(x)...)) { return 10; }
196+
197+
template <class ...T>
198+
constexpr auto baz(Int<foo<T>(T())>... x) -> int { return 1; }
199+
200+
static_assert(baz<Int<1>, Int<2>, Int<3>>(Int<10>(), Int<10>(), Int<10>()) == 1, "");
201+
}

0 commit comments

Comments
 (0)