Skip to content

[Clang] Fix handling of pack indexing types in constraints of redeclaration #139057

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ Bug Fixes to C++ Support
whose type depends on itself. (#GH51347), (#GH55872)
- Improved parser recovery of invalid requirement expressions. In turn, this
fixes crashes from follow-on processing of the invalid requirement. (#GH138820)
- Fixed the handling of pack indexing types in the constraints of a member function redeclaration. (#GH138255)

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion clang/include/clang/AST/ASTContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
mutable llvm::ContextualFoldingSet<DependentDecltypeType, ASTContext &>
DependentDecltypeTypes;

mutable llvm::FoldingSet<PackIndexingType> DependentPackIndexingTypes;
mutable llvm::ContextualFoldingSet<PackIndexingType, ASTContext &>
DependentPackIndexingTypes;

mutable llvm::FoldingSet<TemplateTypeParmType> TemplateTypeParmTypes;
mutable llvm::FoldingSet<ObjCTypeParamType> ObjCTypeParamTypes;
Expand Down
4 changes: 4 additions & 0 deletions clang/include/clang/AST/ExprCXX.h
Original file line number Diff line number Diff line change
Expand Up @@ -4549,6 +4549,10 @@ class PackIndexingExpr final

bool isFullySubstituted() const { return FullySubstituted; }

bool isPartiallySubstituted() const {
return isValueDependent() && TransformedExpressions;
};

/// Determine if the expression was expanded to empty.
bool expandsToEmptyPack() const {
return isFullySubstituted() && TransformedExpressions == 0;
Expand Down
13 changes: 3 additions & 10 deletions clang/include/clang/AST/Type.h
Original file line number Diff line number Diff line change
Expand Up @@ -5976,7 +5976,6 @@ class PackIndexingType final
private llvm::TrailingObjects<PackIndexingType, QualType> {
friend TrailingObjects;

const ASTContext &Context;
QualType Pattern;
Expr *IndexExpr;

Expand All @@ -5987,9 +5986,8 @@ class PackIndexingType final

protected:
friend class ASTContext; // ASTContext creates these.
PackIndexingType(const ASTContext &Context, QualType Canonical,
QualType Pattern, Expr *IndexExpr, bool FullySubstituted,
ArrayRef<QualType> Expansions = {});
PackIndexingType(QualType Canonical, QualType Pattern, Expr *IndexExpr,
bool FullySubstituted, ArrayRef<QualType> Expansions = {});

public:
Expr *getIndexExpr() const { return IndexExpr; }
Expand Down Expand Up @@ -6024,12 +6022,7 @@ class PackIndexingType final
return T->getTypeClass() == PackIndexing;
}

void Profile(llvm::FoldingSetNodeID &ID) {
if (hasSelectedType())
getSelectedType().Profile(ID);
else
Profile(ID, Context, getPattern(), getIndexExpr(), isFullySubstituted());
}
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context);
static void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Context,
QualType Pattern, Expr *E, bool FullySubstituted);

Expand Down
52 changes: 40 additions & 12 deletions clang/include/clang/Sema/Template.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "llvm/ADT/SmallVector.h"
#include <cassert>
#include <optional>
#include <tuple>
#include <utility>

namespace clang {
Expand Down Expand Up @@ -76,9 +77,17 @@ enum class TemplateSubstitutionKind : char {
class MultiLevelTemplateArgumentList {
/// The template argument list at a certain template depth

enum ListProperties {
/// A 'Final' substitution.
IsFinal = 0x1,
/// Track if the arguments are injected template parameters.
/// Injected template parameters should not be expanded.
ArgumentsAreInjectedTemplateParams = 0x02,
};

using ArgList = ArrayRef<TemplateArgument>;
struct ArgumentListLevel {
llvm::PointerIntPair<Decl *, 1, bool> AssociatedDeclAndFinal;
llvm::PointerIntPair<Decl *, 3, unsigned> AssociatedDeclAndProperties;
ArgList Args;
};
using ContainerType = SmallVector<ArgumentListLevel, 4>;
Expand Down Expand Up @@ -161,11 +170,19 @@ enum class TemplateSubstitutionKind : char {
/// A template-like entity which owns the whole pattern being substituted.
/// This will usually own a set of template parameters, or in some
/// cases might even be a template parameter itself.
std::pair<Decl *, bool> getAssociatedDecl(unsigned Depth) const {
std::tuple<Decl *, bool, bool> getAssociatedDecl(unsigned Depth) const {
assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
auto AD = TemplateArgumentLists[getNumLevels() - Depth - 1]
.AssociatedDeclAndProperties;
return {AD.getPointer(), AD.getInt() & IsFinal,
AD.getInt() & ArgumentsAreInjectedTemplateParams};
}

bool ArgumentsAreInjectedParameters(unsigned Depth) const {
assert(NumRetainedOuterLevels <= Depth && Depth < getNumLevels());
auto AD = TemplateArgumentLists[getNumLevels() - Depth - 1]
.AssociatedDeclAndFinal;
return {AD.getPointer(), AD.getInt()};
.AssociatedDeclAndProperties;
return AD.getInt() & ArgumentsAreInjectedTemplateParams;
}

/// Determine whether there is a non-NULL template argument at the
Expand Down Expand Up @@ -205,16 +222,15 @@ enum class TemplateSubstitutionKind : char {

/// Add a new outmost level to the multi-level template argument
/// list.
/// A 'Final' substitution means that Subst* nodes won't be built
/// for the replacements.
void addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args,
bool Final) {
void
addOuterTemplateArguments(Decl *AssociatedDecl, ArgList Args, bool Final,
bool ArgumentsAreInjectedTemplateParams = false) {
assert(!NumRetainedOuterLevels &&
"substituted args outside retained args?");
assert(getKind() == TemplateSubstitutionKind::Specialization);
TemplateArgumentLists.push_back(
{{AssociatedDecl ? AssociatedDecl->getCanonicalDecl() : nullptr,
Final},
getProperties(Final, ArgumentsAreInjectedTemplateParams)},
Args});
}

Expand All @@ -239,15 +255,17 @@ enum class TemplateSubstitutionKind : char {
"Replacing in an empty list?");

if (!TemplateArgumentLists.empty()) {
assert((TemplateArgumentLists[0].AssociatedDeclAndFinal.getPointer() ||
TemplateArgumentLists[0].AssociatedDeclAndFinal.getPointer() ==
assert((TemplateArgumentLists[0]
.AssociatedDeclAndProperties.getPointer() ||
TemplateArgumentLists[0]
.AssociatedDeclAndProperties.getPointer() ==
AssociatedDecl) &&
"Trying to change incorrect declaration?");
TemplateArgumentLists[0].Args = Args;
} else {
--NumRetainedOuterLevels;
TemplateArgumentLists.push_back(
{{AssociatedDecl, /*Final=*/false}, Args});
{{AssociatedDecl, /*Properties=*/0}, Args});
}
}

Expand Down Expand Up @@ -292,6 +310,16 @@ enum class TemplateSubstitutionKind : char {
llvm::errs() << "\n";
}
}

static unsigned getProperties(bool IsFinal, bool IsInjected) {
unsigned Props = 0;
if (IsFinal)
Props |= MultiLevelTemplateArgumentList::IsFinal;
if (IsInjected)
Props |=
MultiLevelTemplateArgumentList::ArgumentsAreInjectedTemplateParams;
return Props;
}
};

/// The context in which partial ordering of function templates occurs.
Expand Down
43 changes: 19 additions & 24 deletions clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
DependentSizedMatrixTypes(this_()),
FunctionProtoTypes(this_(), FunctionProtoTypesLog2InitSize),
DependentTypeOfExprTypes(this_()), DependentDecltypeTypes(this_()),
TemplateSpecializationTypes(this_()),
DependentPackIndexingTypes(this_()), TemplateSpecializationTypes(this_()),
DependentTemplateSpecializationTypes(this_()),
DependentBitIntTypes(this_()), SubstTemplateTemplateParmPacks(this_()),
DeducedTemplates(this_()), ArrayParameterTypes(this_()),
Expand Down Expand Up @@ -6433,34 +6433,29 @@ QualType ASTContext::getPackIndexingType(QualType Pattern, Expr *IndexExpr,
ArrayRef<QualType> Expansions,
UnsignedOrNone Index) const {
QualType Canonical;
if (FullySubstituted && Index) {
if (FullySubstituted && Index)
Canonical = getCanonicalType(Expansions[*Index]);
} else {
llvm::FoldingSetNodeID ID;
PackIndexingType::Profile(ID, *this, Pattern.getCanonicalType(), IndexExpr,
FullySubstituted);
void *InsertPos = nullptr;
PackIndexingType *Canon =
DependentPackIndexingTypes.FindNodeOrInsertPos(ID, InsertPos);
if (!Canon) {
void *Mem = Allocate(
PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()),
TypeAlignment);
Canon = new (Mem)
PackIndexingType(*this, QualType(), Pattern.getCanonicalType(),
IndexExpr, FullySubstituted, Expansions);
DependentPackIndexingTypes.InsertNode(Canon, InsertPos);
}
Canonical = QualType(Canon, 0);
}
else if (!Pattern.isCanonical())
Canonical = getPackIndexingType(Pattern.getCanonicalType(), IndexExpr,
FullySubstituted, Expansions, Index);

llvm::FoldingSetNodeID ID;
PackIndexingType::Profile(ID, *this, Pattern, IndexExpr, FullySubstituted);
void *InsertPos = nullptr;
PackIndexingType *Canon =
DependentPackIndexingTypes.FindNodeOrInsertPos(ID, InsertPos);
if (Canon)
return QualType(Canon, 0);

void *Mem =
Allocate(PackIndexingType::totalSizeToAlloc<QualType>(Expansions.size()),
TypeAlignment);
auto *T = new (Mem) PackIndexingType(*this, Canonical, Pattern, IndexExpr,
FullySubstituted, Expansions);
Types.push_back(T);
return QualType(T, 0);

Canon = new (Mem) PackIndexingType(Canonical, Pattern, IndexExpr,
FullySubstituted, Expansions);
DependentPackIndexingTypes.InsertNode(Canon, InsertPos);
Types.push_back(Canon);
return QualType(Canon, 0);
}

/// getUnaryTransformationType - We don't unique these, since the memory
Expand Down
10 changes: 8 additions & 2 deletions clang/lib/AST/StmtProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2291,9 +2291,15 @@ void StmtProfiler::VisitSizeOfPackExpr(const SizeOfPackExpr *S) {
}

void StmtProfiler::VisitPackIndexingExpr(const PackIndexingExpr *E) {
VisitExpr(E);
VisitExpr(E->getPackIdExpression());
VisitExpr(E->getIndexExpr());

if (E->isPartiallySubstituted()) {
ID.AddInteger(E->getExpressions().size());
for (const Expr *Sub : E->getExpressions())
Visit(Sub);
} else {
VisitExpr(E->getPackIdExpression());
}
}

void StmtProfiler::VisitSubstNonTypeTemplateParmPackExpr(
Expand Down
16 changes: 12 additions & 4 deletions clang/lib/AST/Type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4126,14 +4126,14 @@ void DependentDecltypeType::Profile(llvm::FoldingSetNodeID &ID,
E->Profile(ID, Context, true);
}

PackIndexingType::PackIndexingType(const ASTContext &Context,
QualType Canonical, QualType Pattern,
PackIndexingType::PackIndexingType(QualType Canonical, QualType Pattern,
Expr *IndexExpr, bool FullySubstituted,
ArrayRef<QualType> Expansions)
: Type(PackIndexing, Canonical,
computeDependence(Pattern, IndexExpr, Expansions)),
Context(Context), Pattern(Pattern), IndexExpr(IndexExpr),
Size(Expansions.size()), FullySubstituted(FullySubstituted) {
Pattern(Pattern), IndexExpr(IndexExpr), Size(Expansions.size()),
FullySubstituted(FullySubstituted) {

llvm::uninitialized_copy(Expansions, getTrailingObjects<QualType>());
}

Expand Down Expand Up @@ -4174,6 +4174,14 @@ PackIndexingType::computeDependence(QualType Pattern, Expr *IndexExpr,
return TD;
}

void PackIndexingType::Profile(llvm::FoldingSetNodeID &ID,
const ASTContext &Context) {
if (hasSelectedType() && isFullySubstituted())
getSelectedType().Profile(ID);
else
Profile(ID, Context, getPattern(), getIndexExpr(), isFullySubstituted());
}
Comment on lines +4179 to +4183
Copy link
Contributor

@mizvekov mizvekov May 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-existing issue, but this confuses identity with canonicalization.

This is basically saying that all PackIndexTypes which are fully substituted and have identical selected types are also identical, which can't be right as they can also differ in other ways, such as the Index expression.


void PackIndexingType::Profile(llvm::FoldingSetNodeID &ID,
const ASTContext &Context, QualType Pattern,
Expr *E, bool FullySubstituted) {
Expand Down
Loading