Skip to content

[Clang] Evaluate dependent indexes of pack indexing in a constant context #106054

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

Merged
merged 3 commits into from
Aug 26, 2024
Merged
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
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@ Bug Fixes to C++ Support
template depth than the friend function template. (#GH98258)
- Clang now rebuilds the template parameters of out-of-line declarations and specializations in the context
of the current instantiation in all cases.
- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)


Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
23 changes: 17 additions & 6 deletions clang/lib/Sema/TreeTransform.h
Original file line number Diff line number Diff line change
Expand Up @@ -6669,9 +6669,15 @@ QualType
TreeTransform<Derived>::TransformPackIndexingType(TypeLocBuilder &TLB,
PackIndexingTypeLoc TL) {
// Transform the index
ExprResult IndexExpr = getDerived().TransformExpr(TL.getIndexExpr());
if (IndexExpr.isInvalid())
return QualType();
ExprResult IndexExpr;
{
EnterExpressionEvaluationContext ConstantContext(
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);

IndexExpr = getDerived().TransformExpr(TL.getIndexExpr());
if (IndexExpr.isInvalid())
return QualType();
}
QualType Pattern = TL.getPattern();

const PackIndexingType *PIT = TL.getTypePtr();
Expand Down Expand Up @@ -15299,9 +15305,14 @@ TreeTransform<Derived>::TransformPackIndexingExpr(PackIndexingExpr *E) {
return E;

// Transform the index
ExprResult IndexExpr = getDerived().TransformExpr(E->getIndexExpr());
if (IndexExpr.isInvalid())
return ExprError();
ExprResult IndexExpr;
{
EnterExpressionEvaluationContext ConstantContext(
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
IndexExpr = getDerived().TransformExpr(E->getIndexExpr());
if (IndexExpr.isInvalid())
return ExprError();
}

SmallVector<Expr *, 5> ExpandedExprs;
if (!E->expandsToEmptyPack() && E->getExpressions().empty()) {
Expand Down
28 changes: 28 additions & 0 deletions clang/test/SemaCXX/cxx2c-pack-indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,3 +231,31 @@ struct type_info {
namespace GH93650 {
auto func(auto... inputArgs) { return typeid(inputArgs...[0]); }
} // namespace GH93650


namespace GH105900 {

template <typename... opts>
struct types {
template <unsigned idx>
static constexpr __SIZE_TYPE__ get_index() { return idx; }

template <unsigned s>
static auto x() -> opts...[get_index<s>()] {}
};

template <auto... opts>
struct vars {
template <unsigned idx>
static constexpr __SIZE_TYPE__ get_index() { return idx; }

template <unsigned s>
static auto x() -> decltype(opts...[get_index<s>()]) {return 0;}
};

void f() {
types<void>::x<0>();
vars<0>::x<0>();
}

}
Loading