[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 closed https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/90195 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH 1/6] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} >From 7b0ae16b6777c6e98df64cd2366434972fc68164 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Apr 2024 12:07:30 +0800 Subject: [PATCH 2/6] Clarify comments --- clang/lib/Sema/TreeTransform.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index abc4a16c004a9f..b50fdab8bfd05e 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,8 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } - // We may be doing this in the context of expanding the Pattern. Forget that - // because it has been handled above. + // 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()); >From 1dbfe522a1b4efd92023f0ff3c41cc296bf53299 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sun, 28 Apr 2024 13:29:37 +0800 Subject: [PATCH 3/6] Make dependent PackIndexingExpr always an LValue --- clang/lib/AST/ExprClassification.cpp | 8 +++- clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 23 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index 7026fca8554ce9..abfc3a51f95a8d 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -216,8 +216,14 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { +// A dependent pack-index-expression is now supposed to denote a function +// parameter pack, an NTTP pack, or the pack introduced by a structured +// binding. 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/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp index 2fd0dbfed294a5..a3e5a0931491b5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -164,18 +164,33 @@ namespace GH88929 { namespace GH88925 { template struct S {}; +template struct W {}; + template struct sequence {}; -template auto f(sequence) { - return S(); // #use +template auto f(sequence) { + return S(); // #use } -void g() { +template auto g(sequence) { + return W(); // #nttp-use +} +
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
github-actions[bot] wrote: :warning: C/C++ code formatter, clang-format found issues in your code. :warning: You can test this locally with the following command: ``bash git-clang-format --diff 3ea9ed471c8dca8f703d7f3ce93d274a718b54bb 7a087f06240ce09a64fe679df14380e2fa538701 -- clang/lib/AST/ExprClassification.cpp clang/lib/Sema/TreeTransform.h clang/test/SemaCXX/cxx2c-pack-indexing.cpp `` View the diff from clang-format here. ``diff diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index f943a6ba0d..45e05970df 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -217,7 +217,7 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { cast(E)->getReplacement()); case Expr::PackIndexingExprClass: { -// A pack-index-expression always expand to an id-expression. +// A pack-index-expression always expand to an id-expression. // Consider it as an LValue expression. if (cast(E)->isInstantiationDependent()) return Cl::CL_LValue; `` https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/90195 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH 1/5] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} >From 7b0ae16b6777c6e98df64cd2366434972fc68164 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Apr 2024 12:07:30 +0800 Subject: [PATCH 2/5] Clarify comments --- clang/lib/Sema/TreeTransform.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index abc4a16c004a9f..b50fdab8bfd05e 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,8 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } - // We may be doing this in the context of expanding the Pattern. Forget that - // because it has been handled above. + // 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()); >From 1dbfe522a1b4efd92023f0ff3c41cc296bf53299 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sun, 28 Apr 2024 13:29:37 +0800 Subject: [PATCH 3/5] Make dependent PackIndexingExpr always an LValue --- clang/lib/AST/ExprClassification.cpp | 8 +++- clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 23 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index 7026fca8554ce9..abfc3a51f95a8d 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -216,8 +216,14 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { +// A dependent pack-index-expression is now supposed to denote a function +// parameter pack, an NTTP pack, or the pack introduced by a structured +// binding. 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/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp index 2fd0dbfed294a5..a3e5a0931491b5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -164,18 +164,33 @@ namespace GH88929 { namespace GH88925 { template struct S {}; +template struct W {}; + template struct sequence {}; -template auto f(sequence) { - return S(); // #use +template auto f(sequence) { + return S(); // #use } -void g() { +template auto g(sequence) { + return W(); // #nttp-use +} +
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/90195 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH 1/4] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} >From 7b0ae16b6777c6e98df64cd2366434972fc68164 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Apr 2024 12:07:30 +0800 Subject: [PATCH 2/4] Clarify comments --- clang/lib/Sema/TreeTransform.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index abc4a16c004a9f..b50fdab8bfd05e 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,8 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } - // We may be doing this in the context of expanding the Pattern. Forget that - // because it has been handled above. + // 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()); >From 1dbfe522a1b4efd92023f0ff3c41cc296bf53299 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sun, 28 Apr 2024 13:29:37 +0800 Subject: [PATCH 3/4] Make dependent PackIndexingExpr always an LValue --- clang/lib/AST/ExprClassification.cpp | 8 +++- clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 23 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index 7026fca8554ce9..abfc3a51f95a8d 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -216,8 +216,14 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { +// A dependent pack-index-expression is now supposed to denote a function +// parameter pack, an NTTP pack, or the pack introduced by a structured +// binding. 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/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp index 2fd0dbfed294a5..a3e5a0931491b5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -164,18 +164,33 @@ namespace GH88929 { namespace GH88925 { template struct S {}; +template struct W {}; + template struct sequence {}; -template auto f(sequence) { - return S(); // #use +template auto f(sequence) { + return S(); // #use } -void g() { +template auto g(sequence) { + return W(); // #nttp-use +} +
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -216,8 +216,14 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { +// A dependent pack-index-expression is now supposed to denote a function +// parameter pack, an NTTP pack, or the pack introduced by a structured +// binding. Consider it as an LValue expression. +if (cast(E)->isInstantiationDependent()) + return Cl::CL_LValue; cor3ntin wrote: ```suggestion case Expr::PackIndexingExprClass: { // A pack-index-expression always expand to an id-expression. Consider it as an LValue expression. if (cast(E)->isInstantiationDependent()) return Cl::CL_LValue; ``` https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/cor3ntin approved this pull request. LGTM, thanks! https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
zyn0217 wrote: @cor3ntin Mind looking at it again? Thanks so much! https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 edited https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/90195 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH 1/3] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} >From 7b0ae16b6777c6e98df64cd2366434972fc68164 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Apr 2024 12:07:30 +0800 Subject: [PATCH 2/3] Clarify comments --- clang/lib/Sema/TreeTransform.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index abc4a16c004a9f..b50fdab8bfd05e 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,8 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } - // We may be doing this in the context of expanding the Pattern. Forget that - // because it has been handled above. + // 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()); >From 1dbfe522a1b4efd92023f0ff3c41cc296bf53299 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sun, 28 Apr 2024 13:29:37 +0800 Subject: [PATCH 3/3] Make dependent PackIndexingExpr always an LValue --- clang/lib/AST/ExprClassification.cpp | 8 +++- clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 23 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index 7026fca8554ce9..abfc3a51f95a8d 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -216,8 +216,14 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { +// A dependent pack-index-expression is now supposed to denote a function +// parameter pack, an NTTP pack, or the pack introduced by a structured +// binding. 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/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp index 2fd0dbfed294a5..a3e5a0931491b5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -164,18 +164,33 @@ namespace GH88929 { namespace GH88925 { template struct S {}; +template struct W {}; + template struct sequence {}; -template auto f(sequence) { - return S(); // #use +template auto f(sequence) { + return S(); // #use } -void g() { +template auto g(sequence) { + return W(); // #nttp-use +} +
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + zyn0217 wrote: I realized those expressions could be PRValues after an instantiation because in `PackIndexingExpr` we have: ```cpp if (!isInstantiationDependent()) setValueKind(getSelectedExpr()->getValueKind()); ``` So, I'm going to make it always an LValue iff it's dependent. https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/90195 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH 1/3] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} >From 7b0ae16b6777c6e98df64cd2366434972fc68164 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Apr 2024 12:07:30 +0800 Subject: [PATCH 2/3] Clarify comments --- clang/lib/Sema/TreeTransform.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index abc4a16c004a9f..b50fdab8bfd05e 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,8 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } - // We may be doing this in the context of expanding the Pattern. Forget that - // because it has been handled above. + // 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()); >From d4a2f1266114b2bc96c17f4d0065338bb0040fb1 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sun, 28 Apr 2024 13:29:37 +0800 Subject: [PATCH 3/3] Make dependnt PackIndexingExpr always an LValue --- clang/lib/AST/ExprClassification.cpp | 8 +++- clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 23 ++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/clang/lib/AST/ExprClassification.cpp b/clang/lib/AST/ExprClassification.cpp index 7026fca8554ce9..1c4be91b0ce80f 100644 --- a/clang/lib/AST/ExprClassification.cpp +++ b/clang/lib/AST/ExprClassification.cpp @@ -216,8 +216,14 @@ static Cl::Kinds ClassifyInternal(ASTContext , const Expr *E) { return ClassifyInternal(Ctx, cast(E)->getReplacement()); - case Expr::PackIndexingExprClass: + case Expr::PackIndexingExprClass: { +// A dependent pack-index-expression now is supposed to denote a function +// parameter pack, an NTTP pack, or the pack introduced by a structured +// binding. 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/test/SemaCXX/cxx2c-pack-indexing.cpp b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp index 2fd0dbfed294a5..a3e5a0931491b5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -164,18 +164,33 @@ namespace GH88929 { namespace GH88925 { template struct S {}; +template struct W {}; + template struct sequence {}; -template auto f(sequence) { - return S(); // #use +template auto f(sequence) { + return S(); // #use } -void g() { +template auto g(sequence) { + return W(); // #nttp-use +} +
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + cor3ntin wrote: > I guess we can perceive dependent PackIndexingExprs as LValues in such cases, > like what we did for UnresolvedLookupExpr. Additionally, P2662 now limits > them to id-expressions only, and hence we can probably always treat them as > LValues? Yes, they are always LValues for now. So we might want to do that indeed https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 updated https://github.com/llvm/llvm-project/pull/90195 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH 1/2] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} >From 7b0ae16b6777c6e98df64cd2366434972fc68164 Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Sat, 27 Apr 2024 12:07:30 +0800 Subject: [PATCH 2/2] Clarify comments --- clang/lib/Sema/TreeTransform.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index abc4a16c004a9f..b50fdab8bfd05e 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,8 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } - // We may be doing this in the context of expanding the Pattern. Forget that - // because it has been handled above. + // 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()); ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + zyn0217 wrote: > ```cpp > template struct S2 {}; > template auto f(sequence) { > return S2(); // #use > } > ``` OK, so the problem here is *not* the same: we end up [classifying the `PackIndexingExpr`'s category](https://github.com/llvm/llvm-project/blob/338561657685c1831a53563b1bc36ffc7470239e/clang/lib/AST/ExprClassification.cpp#L219-L220) while deducing against the NTTP of `S2`. This happens before the instantiation of `f`, and therefore `Args...[indices]...` is still dependent. I *guess* we can perceive dependent `PackIndexingExprs` as LValues in such cases, like what we did for `UnresolvedLookupExpr`. Additionally, P2662 now limits them to id-expressions only, and hence we can probably always treat them as LValues? https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + zyn0217 wrote: Oops, yeah, good catch! Honestly, I have tried one similar example (with a fold expression) but had no luck: ```cpp template auto f(sequence) { return (Args...[indices] + ...); } ``` Let me see what's happening here. https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/cor3ntin commented: Thanks for working on that! I suspect there is the same problem for pack indexing expressions, could you try to add a test? Thanks! https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + cor3ntin wrote: I suspect there is the same problem for expressions ```cpp template struct S2 {}; template auto f(sequence) { return S2(); // #use } ``` https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/cor3ntin edited https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
@@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(getSema(), -1); cor3ntin wrote: ```suggestion // 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); ``` https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
llvmbot wrote: @llvm/pr-subscribers-clang Author: Younan Zhang (zyn0217) Changes Fixes https://github.com/llvm/llvm-project/issues/88925 --- Full diff: https://github.com/llvm/llvm-project/pull/90195.diff 2 Files Affected: - (modified) clang/lib/Sema/TreeTransform.h (+3) - (modified) clang/test/SemaCXX/cxx2c-pack-indexing.cpp (+19) ``diff diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} `` https://github.com/llvm/llvm-project/pull/90195 ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
[clang] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType (PR #90195)
https://github.com/zyn0217 created https://github.com/llvm/llvm-project/pull/90195 Fixes https://github.com/llvm/llvm-project/issues/88925 >From f708694fc2686684589dca7b8f3738a117fc047e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Fri, 26 Apr 2024 19:06:57 +0800 Subject: [PATCH] [Sema] Avoid an undesired pack expansion while transforming PackIndexingType --- clang/lib/Sema/TreeTransform.h | 3 +++ clang/test/SemaCXX/cxx2c-pack-indexing.cpp | 19 +++ 2 files changed, 22 insertions(+) diff --git a/clang/lib/Sema/TreeTransform.h b/clang/lib/Sema/TreeTransform.h index 9404be5a46f3f7..abc4a16c004a9f 100644 --- a/clang/lib/Sema/TreeTransform.h +++ b/clang/lib/Sema/TreeTransform.h @@ -6649,6 +6649,9 @@ TreeTransform::TransformPackIndexingType(TypeLocBuilder , } } + // We may be doing this in the context of expanding the Pattern. Forget that + // because it has been handled above. + 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 606715e6aacffd..2fd0dbfed294a5 100644 --- a/clang/test/SemaCXX/cxx2c-pack-indexing.cpp +++ b/clang/test/SemaCXX/cxx2c-pack-indexing.cpp @@ -160,3 +160,22 @@ 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 sequence {}; + +template auto f(sequence) { + return S(); // #use +} + +void g() { + 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}} +} +} ___ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits