On 8/10/26 5:29 PM, feedable via Sourceware Forge wrote:
From: feedable <[email protected]>

This patch rewrites old unexpandable pack detection logic based on
current lambda to a one based on scopes.

The new logic tries to answer the following question: "Will the soonest
potential pack expansion have a pack that is declared within its own
pattern?".  There aren't a lot of scopes over which we can directly
expand (only lambda and stmt-expr).  We can take advantage of that and
assert that any expansion that hasn't occured yet must occur beyond that
scope, so any pack that's declared within that scope, cannot be expanded
over, as it would be within the pattern itself.
For example:

   struct X { int x,y,z; };
   template<class ...T>
   void foo() {
     ([]{auto [...xs] = X{}; xs + T{};}, ...);
   }
When `check_for_bare_parameter_packs` is called for the expr-stmt
`xs + T{};`, any possible expansion over that pack will contain the
lambda-scope, and as `xs` is declared within that scope, it will also be
contained within the pattern of that expansion, while `T` is declared
outside of that scope, so there are expansions which can expand it
without containing its declaration within the pattern.

Also, do actually check for bare parameter packs in the final statement
of a stmt-expr.

        PR c++/126124

gcc/cp/ChangeLog:

        * pt.cc (nearest_expandable_scope): New.
        (filter_packs): New.
        (check_for_bare_parameter_packs): Use scopes for bare pack
        detection.
        * semantics.cc (finish_stmt_expr_expr): Check for bare packs.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp26/decomp32.C: New test.
        * g++.dg/cpp2a/lambda-generic-variadic-22.C: New test.
        * g++.dg/expr/stmt-expr-2.C: New test.
---
This patch rewrites old unexpandable pack detection logic based on
current lambda to a one based on scopes.

The new logic tries to answer the following question: "Will the soonest
potential pack expansion have a pack that is declared within its own
pattern?".  There aren't a lot of scopes over which we can directly
expand (only lambda and stmt-expr).  We can take advantage of that and
assert that any expansion that hasn't occured yet must occur beyond that
scope, so any pack that's declared within that scope, cannot be expanded
over, as it would be within the pattern itself.
For example:

   struct X { int x,y,z; };
   template<class ...T>
   void foo() {
     ([]{auto [...xs] = X{}; xs + T{};}, ...);
   }
When `check_for_bare_parameter_packs` is called for the expr-stmt
`xs + T{};`, any possible expansion over that pack will contain the
lambda-scope, and as `xs` is declared within that scope, it will also be
contained within the pattern of that expansion, while `T` is declared
outside of that scope, so there are expansions which can expand it
without containing its declaration within the pattern.

Also, do actually check for bare parameter packs in the final statement
of a stmt-expr.

        PR c++/126124

gcc/cp/ChangeLog:

        * pt.cc (nearest_expandable_scope): New.
        (filter_packs): New.
        (check_for_bare_parameter_packs): Use scopes for bare pack
        detection.
        * semantics.cc (finish_stmt_expr_expr): Check for bare packs.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp26/decomp31.C: New test.
        * g++.dg/cpp2a/lambda-generic-variadic-22.C: New test.
        * g++.dg/expr/stmt-expr-2.C: New test.

Changes since v1:
- Removed `constexpr` from the variable for the lambda in
   `filter_packs`.



This is a forge pull request published on the gcc-patches mailing list mailing 
list
as requested by feedable via Sourceware Forge 
<[email protected]>.
Forge discussion: https://forge.sourceware.org/gcc/gcc/pulls/213

Get it locally using:
```
git fetch forge-upstream "+refs/versioned_pull/213/*:refs/versioned_pull/213/*"
git switch -c "pr-213-v2" "refs/versioned_pull/213/2/head"
```
Or, download the patch at: https://forge.sourceware.org/gcc/gcc/pulls/213.diff
Created on: 2026-08-08 16:43:31+00:00
Latest update: 2026-08-10 15:29:53+00:00
Changes: 5 changed files, 205 additions, 11 deletions
Head revision: feedable/gcc-TEST ref pr126124 commit 
66cadb90dd11330a04d1801f1a3597eba719129f
Base revision: gcc/gcc ref trunk commit 
2791799fe11f886736869e3aff093e0c14fb10b8 r17-3183-g2791799fe11f88
Merge base: 2791799fe11f886736869e3aff093e0c14fb10b8
Requested Reviewers:

Changed files:
- A: gcc/testsuite/g++.dg/cpp26/decomp32.C
- A: gcc/testsuite/g++.dg/cpp2a/lambda-generic-variadic-22.C
- A: gcc/testsuite/g++.dg/expr/stmt-expr-2.C
- M: gcc/cp/pt.cc
- M: gcc/cp/semantics.cc

  gcc/cp/pt.cc                                  | 106 ++++++++++++++++--
  gcc/cp/semantics.cc                           |   6 +
  gcc/testsuite/g++.dg/cpp26/decomp32.C         |  18 +++
  .../g++.dg/cpp2a/lambda-generic-variadic-22.C |  26 +++++
  gcc/testsuite/g++.dg/expr/stmt-expr-2.C       |  60 ++++++++++
  5 files changed, 205 insertions(+), 11 deletions(-)
  create mode 100644 gcc/testsuite/g++.dg/cpp26/decomp32.C
  create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-generic-variadic-22.C
  create mode 100644 gcc/testsuite/g++.dg/expr/stmt-expr-2.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index ab78bbb87c0ae..0f8b2fb43089b 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -4495,6 +4495,98 @@ make_pack_index (tree pack, tree index)
    return t;
  }
+/* Determine the nearest scope below which packs will be a part of the pattern.
+   Return NULL if no pack can be expanded without being part of the pattern.
+   For example:

I might phrase this as

Return the nearest scope which could be part of an outer pack expansion, or NULL if there is no such scope. Packs from outside this scope are ignored by check_for_bare_parameter_packs.

+     template<class ...U>
+     void foo ()
+     {
+       ([]<class ...T>{T{} + U{};}, ...);
+     }
+
+   When check_for_bare_parameter_packs is called on the full-expr `T{} + U{}`,
+   the nearest expandable scope is the lambda scope, so the pack `T`, which
+   resides within that scope, must be a part of the pattern, and therefore
+   should be rejected, while the pack `U`, which resides outside the lambda
+   scope, can still be expanded by the fold-expr later on, so it should be
+   allowed.  */

+static cp_binding_level *
+nearest_expandable_scope ()
+{
+  cp_binding_level *it = current_binding_level;
+
+  /* If we are inside a lambda-introducer, we may still be expanded over.  */
+  tree lambda = current_lambda_expr ();
+  if (it->kind == sk_class && lambda && TREE_TYPE (lambda) == it->this_entity)
+    return it;

This seems to be needed because of the bare check in finish_member_declaration for capture fields. Perhaps we should instead skip that check for lambda fields?

+  while (true)
+    {
+      /* These scopes are expandable, so packs beyond these scopes can still
+        be expanded later.  */
+      if (it->kind == sk_lambda || it->kind == sk_stmt_expr)
+       break;
+      /* Nothing can possibly expand any pack from this point on.  */
+      if (it->kind == sk_namespace)
+       return NULL;
+      /* This is a normal scope.  It can be contained within an expandable one
+        to be expanded over, so we have to keep searching.  */
+      it = it->level_chain;
+    }
+  /* We need to adjust the lambda scope to include its template params, since
+     these also can't be expanded at this point.  */
+  if (it->kind == sk_lambda && it->level_chain->kind == sk_template_parms)
+    it = it->level_chain;
+  return it;
+}
+
+/* Scan the bindings for packs in PACKS that are declared within the pattern,
+   and remove the rest.  DISREGARD_SINCE is the nearest expandable scope.  */
+static tree
+filter_packs (cp_binding_level *disregard_since, tree packs)
+{
+  if (!disregard_since)
+    return packs;
+
+  auto to_decl = [](tree pack)
+    {
+      if (TREE_CODE (pack) == TEMPLATE_TYPE_PARM)
+       pack = TEMPLATE_TYPE_DECL (pack);
+      if (TREE_CODE (pack) == TEMPLATE_PARM_INDEX)
+       pack = TEMPLATE_PARM_DECL (pack);
+      if (is_capture_proxy (pack) && !DECL_PACK_P (pack))
+       pack = DECL_CAPTURED_VARIABLE (pack);
+      return pack;
+    };
+
+  hash_set<tree> expandable_packs;
+  for (tree it = packs; it; it = TREE_CHAIN (it))
+    expandable_packs.add (to_decl (TREE_VALUE (it)));
+
+  for (cp_binding_level *it = current_binding_level; ; it = it->level_chain)
+    {
+      for (tree n = it->names; n; n = TREE_CHAIN (n))
+       if (expandable_packs.contains (n))
+         expandable_packs.remove (n);
+      if (it == disregard_since)
+       break;
+    }
+
+  for (; packs && expandable_packs.contains (to_decl (TREE_VALUE (packs)));
+       packs = TREE_CHAIN (packs))
+    ;
+  if (!packs) return packs;
+  tree new_tail = packs;
+  for (tree tail = TREE_CHAIN (packs); tail; tail = TREE_CHAIN (tail))
+    if (!expandable_packs.contains (TREE_VALUE (tail)))
+      {
+       TREE_CHAIN (new_tail) = tail;
+       new_tail = tail;
+      }

We usually do this sort of thing with a tree* iterator rather than two loops.

+  return packs;
+}
+
  /* Checks T for any "bare" parameter packs, which have not yet been
     expanded, and issues an error if any are found. This operation can
     only be done on full expressions or types (e.g., an expression
@@ -4546,17 +4638,9 @@ check_for_bare_parameter_packs (tree t, location_t loc 
/* = UNKNOWN_LOCATION */)
        return true;
      }
- if (lam && CLASSTYPE_TEMPLATE_INFO (lam))
-    for (; parameter_packs;
-        parameter_packs = TREE_CHAIN (parameter_packs))
-      {
-       tree pack = TREE_VALUE (parameter_packs);
-       if (is_capture_proxy (pack)
-           || (TREE_CODE (pack) == PARM_DECL
-               && DECL_CONTEXT (pack)
-               && DECL_CONTEXT (DECL_CONTEXT (pack)) == lam))
-         break;
-      }
+  cp_binding_level *scope = nearest_expandable_scope ();

I notice that you don't touch the code just above this:

/* It's OK for a lambda to have an unexpanded parameter pack from the containing context, but do complain about unexpanded capture packs. */
  tree lam = current_lambda_expr ();
  if (lam)
    lam = TREE_TYPE (lam);

  if (lam && lam != current_class_type)
    {
/* We're in a lambda, but it isn't the innermost class. This should work, but currently doesn't. */
      sorry_at (loc, "unexpanded parameter pack in local class in lambda");
      return true;
    }

Can we remove this sorry (and the lam variable) now that you're generalizing the filtering?

Jason

Reply via email to