]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: build_extra_args recapturing local specs [PR114303]
authorPatrick Palka <ppalka@redhat.com>
Thu, 11 Apr 2024 14:16:41 +0000 (10:16 -0400)
committerPatrick Palka <ppalka@redhat.com>
Wed, 18 Jun 2025 14:51:29 +0000 (10:51 -0400)
r13-6452-g341e6cd8d603a3 made build_extra_args walk evaluated contexts
first so that we prefer processing a local specialization in an evaluated
context even if its first use is in an unevaluated context.  But this
means we need to avoid walking a tree that already has extra args/specs
saved because the list of saved specs appears to be an evaluated
context which we'll now walk first.  It seems then that we should be
calculating the saved specs from scratch each time, rather than
potentially walking the saved specs list from an earlier partial
instantiation when calling build_extra_args a second time around.

PR c++/114303

gcc/cp/ChangeLog:

* constraint.cc (tsubst_requires_expr): Clear
REQUIRES_EXPR_EXTRA_ARGS before calling build_extra_args.
* pt.cc (tree_extra_args): Define.
(extract_locals_r): Assert *_EXTRA_ARGS is empty.
(tsubst_stmt) <case IF_STMT>: Clear IF_SCOPE on the new
IF_STMT.  Call build_extra_args on the new IF_STMT instead
of t which might already have IF_STMT_EXTRA_ARGS.

gcc/testsuite/ChangeLog:

* g++.dg/cpp1z/constexpr-if-lambda6.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
(cherry picked from commit b262b17636e47ae969a74f16e86ccb00678d5e88)

gcc/cp/constraint.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda6.C [new file with mode: 0644]

index 5ac0ca2aadfa9db89a51b1eb5d706d87b4d39875..c542495171fabc741751ccc1dc4b3665c528c16a 100644 (file)
@@ -2268,6 +2268,7 @@ tsubst_requires_expr (tree t, tree args, sat_info info)
         checked out of order, so instead just remember the template
         arguments and wait until we can substitute them all at once.  */
       t = copy_node (t);
+      REQUIRES_EXPR_EXTRA_ARGS (t) = NULL_TREE;
       REQUIRES_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, info.complain);
       return t;
     }
index 60ae00ce0dcda7ab38222d92c253cef443a46c35..becc7305bfc646754f5cc600aec9fb5932970a0d 100644 (file)
@@ -3896,6 +3896,24 @@ has_extra_args_mechanism_p (const_tree t)
              && IF_STMT_CONSTEXPR_P (t))); /* IF_STMT_EXTRA_ARGS  */
 }
 
+/* Return *_EXTRA_ARGS of the given supported tree T.  */
+
+static tree&
+tree_extra_args (tree t)
+{
+  gcc_checking_assert (has_extra_args_mechanism_p (t));
+
+  if (PACK_EXPANSION_P (t))
+    return PACK_EXPANSION_EXTRA_ARGS (t);
+  else if (TREE_CODE (t) == REQUIRES_EXPR)
+    return REQUIRES_EXPR_EXTRA_ARGS (t);
+  else if (TREE_CODE (t) == IF_STMT
+          && IF_STMT_CONSTEXPR_P (t))
+    return IF_STMT_EXTRA_ARGS (t);
+
+  gcc_unreachable ();
+}
+
 /* Structure used to track the progress of find_parameter_packs_r.  */
 struct find_parameter_pack_data
 {
@@ -13051,6 +13069,16 @@ extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
     /* Remember local typedefs (85214).  */
     tp = &TYPE_NAME (*tp);
 
+  if (has_extra_args_mechanism_p (*tp))
+    /* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
+       potentially see a previously captured local in an evaluated context
+       that's really only used in an unevaluated context (PR114303).  This
+       means callers of build_extra_args need to clear *_EXTRA_ARGS of the
+       outermost tree.  Nested *_EXTRA_ARGS should naturally be empty since
+       the outermost (extra-args) tree will intercept any substitution before
+       a nested tree can.  */
+    gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE);
+
   if (TREE_CODE (*tp) == DECL_EXPR)
     {
       tree decl = DECL_EXPR_DECL (*tp);
@@ -18911,10 +18939,11 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl,
             of the constexpr if is still dependent.  Don't substitute into the
             branches now, just remember the template arguments.  */
          do_poplevel (IF_SCOPE (stmt));
+         IF_SCOPE (stmt) = NULL_TREE;
          IF_COND (stmt) = IF_COND (t);
          THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
          ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
-         IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
+         IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
          add_stmt (stmt);
          break;
        }
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda6.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda6.C
new file mode 100644 (file)
index 0000000..038c2a4
--- /dev/null
@@ -0,0 +1,16 @@
+// PR c++/114303
+// { dg-do compile { target c++17 } }
+
+struct A { static constexpr bool value = true; };
+
+int main() {
+  [](auto x1) {
+    return [&](auto) {
+      return [&](auto x3) {
+        if constexpr (decltype(x3)::value) {
+          static_assert(decltype(x1)::value);
+        }
+      }(A{});
+    }(0);
+  }(A{});
+}