diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index 7026fca8554ce..79b7b2bd48aef 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -216,8 +216,13 @@ static Cl::Kinds ClassifyInternal(ASTContext &Ctx, const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { + // A pack-index-expression always expands to an id-expression. + // Consider it as an LValue expression. + if (cast(E)->isInstantiationDependent()) + return Cl::CL_LValue; return ClassifyInternal(Ctx, cast(E)->getSelectedExpr()); + } // C, C++98 [expr.sub]p1: The result is an lvalue of type "T". // C++11 (DR1213): in the case of an array operand, the result is an lvalue diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f..b50fdab8bfd05 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,10 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder &TLB, } } + // A pack indexing type can appear in a larger pack expansion, + // e.g. `Pack...[pack_of_indexes]...` + // so we need to temporarily disable substitution of pack elements + Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); QualType Result = getDerived().TransformType(TLB, TL.getPatternLoc()); QualType Out = getDerived().RebuildPackIndexingType( diff --git a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp index 606715e6aacff..a3e5a0931491b 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,37 @@ namespace GH88929 { using E = P...[0]; // expected-error {{unknown type name 'P'}} \ // expected-error {{expected ';' after alias declaration}} } + +namespace GH88925 { +template struct S {}; + +template struct W {}; + +template struct sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +template auto g(sequence) { + return W(); // #nttp-use +} + +void h() { + static_assert(__is_same(decltype(f(sequence<0, 0>())), S)); + static_assert(__is_same(decltype(f(sequence<0, 0>())), S)); + static_assert(__is_same(decltype(f(sequence<0, 1>())), S)); + f(sequence<3>()); + // expected-error@#use {{invalid index 3 for pack 'args' of size 2}}} + // expected-note-re@-2 {{function template specialization '{{.*}}' requested here}} + + struct foo {}; + struct bar {}; + struct baz {}; + + static_assert(__is_same(decltype(g(sequence<0, 2, 1>())), W)); + g(sequence<4>()); + // expected-error@#nttp-use {{invalid index 4 for pack args of size 1}} + // expected-note-re@-2 {{function template specialization '{{.*}}' requested here}} +} +}