Skip to content

Commit

Permalink
[clang] Do not substitute parameter pack while retaining the pack exp…
Browse files Browse the repository at this point in the history
…ansion (#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
  • Loading branch information
usx95 authored Sep 12, 2024
1 parent 89c10e2 commit 849d1b8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,9 @@ Bug Fixes to C++ Support
- Fix a crash when using ``source_location`` in the trailing return type of a lambda expression. (#GH67134)
- A follow-up fix was added for (#GH61460), as the previous fix was not entirely correct. (#GH86361)
- Fixed a crash in the typo correction of an invalid CTAD guide. (#GH107887)
- Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
pack. #GH63819, #GH107560


Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,13 @@ class TreeTransform {
class ForgetPartiallySubstitutedPackRAII {
Derived &Self;
TemplateArgument Old;
// Set the pack expansion index to -1 to avoid pack substitution and
// indicate that parameter packs should be instantiated as themselves.
Sema::ArgumentPackSubstitutionIndexRAII ResetPackSubstIndex;

public:
ForgetPartiallySubstitutedPackRAII(Derived &Self) : Self(Self) {
ForgetPartiallySubstitutedPackRAII(Derived &Self)
: Self(Self), ResetPackSubstIndex(Self.getSema(), -1) {
Old = Self.ForgetPartiallySubstitutedPack();
}

Expand Down
14 changes: 14 additions & 0 deletions clang/test/SemaTemplate/pack-deduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,17 @@ void Run() {
Outer<void>::Inner<0>().Test(1,1);
}
}

namespace GH107560 {
int bar(...);

template <int> struct Int {};

template <class ...T>
constexpr auto foo(T... x) -> decltype(bar(T(x)...)) { return 10; }

template <class ...T>
constexpr auto baz(Int<foo<T>(T())>... x) -> int { return 1; }

static_assert(baz<Int<1>, Int<2>, Int<3>>(Int<10>(), Int<10>(), Int<10>()) == 1, "");
}

0 comments on commit 849d1b8

Please sign in to comment.