Skip to content

Commit 5a288b9

Browse files
authored
[Clang] Evaluate dependent indexes of pack indexing in a constant context (#106054)
Fixes #105900
1 parent bc695f5 commit 5a288b9

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ Bug Fixes to C++ Support
309309
template depth than the friend function template. (#GH98258)
310310
- Clang now rebuilds the template parameters of out-of-line declarations and specializations in the context
311311
of the current instantiation in all cases.
312+
- Fix evaluation of the index of dependent pack indexing expressions/types specifiers (#GH105900)
313+
312314

313315
Bug Fixes to AST Handling
314316
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/TreeTransform.h

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6669,9 +6669,15 @@ QualType
66696669
TreeTransform<Derived>::TransformPackIndexingType(TypeLocBuilder &TLB,
66706670
PackIndexingTypeLoc TL) {
66716671
// Transform the index
6672-
ExprResult IndexExpr = getDerived().TransformExpr(TL.getIndexExpr());
6673-
if (IndexExpr.isInvalid())
6674-
return QualType();
6672+
ExprResult IndexExpr;
6673+
{
6674+
EnterExpressionEvaluationContext ConstantContext(
6675+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
6676+
6677+
IndexExpr = getDerived().TransformExpr(TL.getIndexExpr());
6678+
if (IndexExpr.isInvalid())
6679+
return QualType();
6680+
}
66756681
QualType Pattern = TL.getPattern();
66766682

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

1530115307
// Transform the index
15302-
ExprResult IndexExpr = getDerived().TransformExpr(E->getIndexExpr());
15303-
if (IndexExpr.isInvalid())
15304-
return ExprError();
15308+
ExprResult IndexExpr;
15309+
{
15310+
EnterExpressionEvaluationContext ConstantContext(
15311+
SemaRef, Sema::ExpressionEvaluationContext::ConstantEvaluated);
15312+
IndexExpr = getDerived().TransformExpr(E->getIndexExpr());
15313+
if (IndexExpr.isInvalid())
15314+
return ExprError();
15315+
}
1530515316

1530615317
SmallVector<Expr *, 5> ExpandedExprs;
1530715318
if (!E->expandsToEmptyPack() && E->getExpressions().empty()) {

clang/test/SemaCXX/cxx2c-pack-indexing.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,31 @@ struct type_info {
231231
namespace GH93650 {
232232
auto func(auto... inputArgs) { return typeid(inputArgs...[0]); }
233233
} // namespace GH93650
234+
235+
236+
namespace GH105900 {
237+
238+
template <typename... opts>
239+
struct types {
240+
template <unsigned idx>
241+
static constexpr __SIZE_TYPE__ get_index() { return idx; }
242+
243+
template <unsigned s>
244+
static auto x() -> opts...[get_index<s>()] {}
245+
};
246+
247+
template <auto... opts>
248+
struct vars {
249+
template <unsigned idx>
250+
static constexpr __SIZE_TYPE__ get_index() { return idx; }
251+
252+
template <unsigned s>
253+
static auto x() -> decltype(opts...[get_index<s>()]) {return 0;}
254+
};
255+
256+
void f() {
257+
types<void>::x<0>();
258+
vars<0>::x<0>();
259+
}
260+
261+
}

0 commit comments

Comments
 (0)