]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fix crash with pack indexing in noexcept [PR121325]
authorMarek Polacek <polacek@redhat.com>
Mon, 24 Nov 2025 22:31:22 +0000 (17:31 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 26 Nov 2025 19:20:19 +0000 (14:20 -0500)
In my r15-6792 patch I added a call to tsubst in tsubst_pack_index
to fully instantiate args#N in the pack.

Here we are in an unevaluated context, but since the pack is
a TREE_VEC, we call tsubst_template_args which has cp_evaluated
at the beginning.  That causes a crash because we trip on the
assert in tsubst_expr/PARM_DECL:

  gcc_assert (cp_unevaluated_operand);

because retrieve_local_specialization didn't find anything (becase
there are no local_specializations yet).

We can avoid the cp_evaluated by calling the new tsubst_tree_vec,
which creates a new TREE_VEC and substitutes each element.

PR c++/121325

gcc/cp/ChangeLog:

* pt.cc (tsubst_tree_vec): New.
(tsubst_pack_index): Call it.

gcc/testsuite/ChangeLog:

* g++.dg/cpp26/pack-indexing18.C: New test.

Reviewed-by: Patrick Palka <ppalka@redhat.com>
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp26/pack-indexing18.C [new file with mode: 0644]

index e74e34d814990615f49f7821c8eb7dbe02501b2d..4dc8f980d0d0858ee767ba65f123666e723d767b 100644 (file)
@@ -14203,6 +14203,25 @@ tsubst_pack_expansion (tree t, tree args, tsubst_flags_t complain,
   return result;
 }
 
+/* Substitute ARGS into T, which is a TREE_VEC.  This function creates a new
+   TREE_VEC rather than substituting the elements in-place.  */
+
+static tree
+tsubst_tree_vec (tree t, tree args, tsubst_flags_t complain, tree in_decl)
+{
+  const int len = TREE_VEC_LENGTH (t);
+  tree r = make_tree_vec (len);
+  for (int i = 0; i < len; ++i)
+    {
+      tree arg = TREE_VEC_ELT (t, i);
+      if (TYPE_P (arg))
+       TREE_VEC_ELT (r, i) = tsubst (arg, args, complain, in_decl);
+      else
+       TREE_VEC_ELT (r, i) = tsubst_expr (arg, args, complain, in_decl);
+    }
+  return r;
+}
+
 /* Substitute ARGS into T, which is a pack index (i.e., PACK_INDEX_TYPE or
    PACK_INDEX_EXPR).  Returns a single type or expression, a PACK_INDEX_*
    node if only a partial substitution could be performed, or ERROR_MARK_NODE
@@ -14220,7 +14239,7 @@ tsubst_pack_index (tree t, tree args, tsubst_flags_t complain, tree in_decl)
         a partially instantiated closure.  Let tsubst find the
         fully-instantiated one.  */
       gcc_assert (TREE_CODE (pack) == TREE_VEC);
-      pack = tsubst (pack, args, complain, in_decl);
+      pack = tsubst_tree_vec (pack, args, complain, in_decl);
     }
   if (TREE_CODE (pack) == TREE_VEC && TREE_VEC_LENGTH (pack) == 0)
     {
diff --git a/gcc/testsuite/g++.dg/cpp26/pack-indexing18.C b/gcc/testsuite/g++.dg/cpp26/pack-indexing18.C
new file mode 100644 (file)
index 0000000..d3e3730
--- /dev/null
@@ -0,0 +1,32 @@
+// PR c++/121325
+// { dg-do compile { target c++26 } }
+
+void f(auto... a) requires requires { []<int i = 0> noexcept(noexcept(a...[i])) { }(); } {}
+void g(auto... a) requires requires { []<int i = 0> { static_assert(noexcept(a...[i])); }(); } {}
+
+void
+h ()
+{
+  f (0);
+  g (0);
+}
+
+void foo () {}
+void bar () noexcept {}
+template<bool B>
+void baz () noexcept(B) {}
+
+template<typename... Ts>
+void
+x (Ts... ts) noexcept (noexcept (ts...[0]()))
+{
+}
+
+void
+y ()
+{
+  static_assert (!noexcept (x (foo)));
+  static_assert (noexcept (x (bar)));
+  static_assert (noexcept (x (baz<true>)));
+  static_assert (!noexcept (x (baz<false>)));
+}