Skip to content

Commit 27c9173

Browse files
authored
[Clang] Remove unnecessary Decl transform & profiles for SizeOfPackExpr (#124533)
We used to always transform the pattern declaration for SizeOfPackExpr to ensure the constraint expression's profile produced the desired result. However, this approach failed to handle pack expansions when the pack referred to function parameters. In such cases, the function parameters were formerly expanded to 1 to avoid building Subst* nodes (see e6974da). That workaround caused us to transform a pack without a proper ArgumentPackSubstitutionIndex, leading to crashes when transforming the pattern. It turns out that profiling the pattern for partially substituted SizeOfPackExprs is unnecessary because their transformed forms are also profiled within the partial arguments. Fixes #124161
1 parent 1f5335c commit 27c9173

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

clang/include/clang/AST/ExprCXX.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4326,8 +4326,6 @@ class SizeOfPackExpr final
43264326
/// Retrieve the parameter pack.
43274327
NamedDecl *getPack() const { return Pack; }
43284328

4329-
void setPack(NamedDecl *NewPack) { Pack = NewPack; }
4330-
43314329
/// Retrieve the length of the parameter pack.
43324330
///
43334331
/// This routine may only be invoked when the expression is not

clang/lib/AST/StmtProfile.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2270,13 +2270,13 @@ void StmtProfiler::VisitPackExpansionExpr(const PackExpansionExpr *S) {
22702270

22712271
void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
22722272
VisitExpr(S);
2273-
VisitDecl(S->getPack());
22742273
if (S->isPartiallySubstituted()) {
22752274
auto Args = S->getPartialArguments();
22762275
ID.AddInteger(Args.size());
22772276
for (const auto &TA : Args)
22782277
VisitTemplateArgument(TA);
22792278
} else {
2279+
VisitDecl(S->getPack());
22802280
ID.AddInteger(0);
22812281
}
22822282
}

clang/lib/Sema/SemaTemplateInstantiate.cpp

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1762,23 +1762,6 @@ namespace {
17621762
return inherited::TransformLambdaBody(E, Body);
17631763
}
17641764

1765-
ExprResult TransformSizeOfPackExpr(SizeOfPackExpr *E) {
1766-
ExprResult Transformed = inherited::TransformSizeOfPackExpr(E);
1767-
if (!Transformed.isUsable())
1768-
return Transformed;
1769-
auto *TransformedExpr = cast<SizeOfPackExpr>(Transformed.get());
1770-
if (SemaRef.CodeSynthesisContexts.back().Kind ==
1771-
Sema::CodeSynthesisContext::ConstraintNormalization &&
1772-
TransformedExpr->getPack() == E->getPack()) {
1773-
Decl *NewPack =
1774-
TransformDecl(E->getPackLoc(), TransformedExpr->getPack());
1775-
if (!NewPack)
1776-
return ExprError();
1777-
TransformedExpr->setPack(cast<NamedDecl>(NewPack));
1778-
}
1779-
return TransformedExpr;
1780-
}
1781-
17821765
ExprResult TransformRequiresExpr(RequiresExpr *E) {
17831766
LocalInstantiationScope Scope(SemaRef, /*CombineWithOuterScope=*/true);
17841767
ExprResult TransReq = inherited::TransformRequiresExpr(E);
@@ -1902,15 +1885,6 @@ Decl *TemplateInstantiator::TransformDecl(SourceLocation Loc, Decl *D) {
19021885
TemplateArgument Arg = TemplateArgs(TTP->getDepth(), TTP->getPosition());
19031886

19041887
if (TTP->isParameterPack()) {
1905-
// We might not have an index for pack expansion when normalizing
1906-
// constraint expressions. In that case, resort to instantiation scopes
1907-
// for the transformed declarations.
1908-
if (SemaRef.ArgumentPackSubstitutionIndex == -1 &&
1909-
SemaRef.CodeSynthesisContexts.back().Kind ==
1910-
Sema::CodeSynthesisContext::ConstraintNormalization) {
1911-
return SemaRef.FindInstantiatedDecl(Loc, cast<NamedDecl>(D),
1912-
TemplateArgs);
1913-
}
19141888
assert(Arg.getKind() == TemplateArgument::Pack &&
19151889
"Missing argument pack");
19161890
Arg = getPackSubstitutedTemplateArgument(getSema(), Arg);

clang/test/SemaTemplate/concepts-out-of-line-def.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,34 @@ template struct d<int, int>;
722722

723723
} // namespace GH115098
724724

725+
namespace GH123441 {
726+
727+
struct buf {
728+
constexpr buf(auto&&... initList) requires (sizeof...(initList) <= 8);
729+
};
730+
731+
constexpr buf::buf(auto&&... initList) requires (sizeof...(initList) <= 8) {}
732+
733+
template <class>
734+
struct buffer {
735+
constexpr buffer(auto&&... initList) requires (sizeof...(initList) <= 8);
736+
};
737+
738+
template <class T>
739+
constexpr buffer<T>::buffer(auto&&... initList) requires (sizeof...(initList) <= 8) {}
740+
741+
template <class...>
742+
struct foo { // expected-note {{foo defined here}}
743+
constexpr foo(auto&&... initList)
744+
requires (sizeof...(initList) <= 8);
745+
};
746+
747+
template <class... T>
748+
constexpr foo<T...>::foo(auto&&... initList) // expected-error {{does not match any declaration}}
749+
requires (sizeof...(T) <= 8) {}
750+
751+
} // namespace GH123441
752+
725753
namespace GH114685 {
726754

727755
template <typename T> struct ptr {

0 commit comments

Comments
 (0)