Skip to content

Commit

Permalink
[Clang] Fix dependency computation for pack indexing expression (#91933)
Browse files Browse the repository at this point in the history
Given `foo...[idx]` if idx is value dependent, the expression is type
dependent.

Fixes #91885
Fixes #91884
  • Loading branch information
cor3ntin committed May 14, 2024
1 parent 725014d commit 312f83f
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 7 deletions.
13 changes: 10 additions & 3 deletions clang/lib/AST/ComputeDependence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,19 @@ ExprDependence clang::computeDependence(PackExpansionExpr *E) {
}

ExprDependence clang::computeDependence(PackIndexingExpr *E) {

ExprDependence PatternDep = E->getPackIdExpression()->getDependence() &
~ExprDependence::UnexpandedPack;

ExprDependence D = E->getIndexExpr()->getDependence();
if (D & ExprDependence::TypeValueInstantiation)
D |= E->getIndexExpr()->getDependence() | PatternDep |
ExprDependence::Instantiation;

ArrayRef<Expr *> Exprs = E->getExpressions();
if (Exprs.empty())
D |= (E->getPackIdExpression()->getDependence() |
ExprDependence::TypeValueInstantiation) &
~ExprDependence::UnexpandedPack;
D |= PatternDep | ExprDependence::Instantiation;

else if (!E->getIndexExpr()->isInstantiationDependent()) {
std::optional<unsigned> Index = E->getSelectedIndex();
assert(Index && *Index < Exprs.size() && "pack index out of bound");
Expand Down
13 changes: 9 additions & 4 deletions clang/lib/Sema/SemaType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9351,15 +9351,20 @@ QualType Sema::BuildCountAttributedArrayType(QualType WrappedTy,
/// that expression, according to the rules in C++11
/// [dcl.type.simple]p4 and C++11 [expr.lambda.prim]p18.
QualType Sema::getDecltypeForExpr(Expr *E) {
if (E->isTypeDependent())
return Context.DependentTy;

Expr *IDExpr = E;
if (auto *ImplCastExpr = dyn_cast<ImplicitCastExpr>(E))
IDExpr = ImplCastExpr->getSubExpr();

if (auto *PackExpr = dyn_cast<PackIndexingExpr>(E))
IDExpr = PackExpr->getSelectedExpr();
if (auto *PackExpr = dyn_cast<PackIndexingExpr>(E)) {
if (E->isInstantiationDependent())
IDExpr = PackExpr->getPackIdExpression();
else
IDExpr = PackExpr->getSelectedExpr();
}

if (E->isTypeDependent())
return Context.DependentTy;

// C++11 [dcl.type.simple]p4:
// The type denoted by decltype(e) is defined as follows:
Expand Down
23 changes: 23 additions & 0 deletions clang/test/SemaCXX/cxx2c-pack-indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,26 @@ void h() {
// expected-note-re@-2 {{function template specialization '{{.*}}' requested here}}
}
}

namespace GH91885 {

void test(auto...args){
[&]<int idx>(){
using R = decltype( args...[idx] ) ;
}.template operator()<0>();
}

template<int... args>
void test2(){
[&]<int idx>(){
using R = decltype( args...[idx] ) ;
}.template operator()<0>();
}

void f( ) {
test(1);
test2<1>();
}


}

0 comments on commit 312f83f

Please sign in to comment.