From bb2bfdb2048aed18ef7dc01b51816a800e83ce54 Mon Sep 17 00:00:00 2001
From: Patrick Palka <ppalka@redhat.com>
Date: Tue, 15 Oct 2024 13:13:15 -0400
Subject: [PATCH] c++: checking ICE w/ constexpr if and lambda as def targ
 [PR117054]

Here we're tripping over the assert in extract_locals_r which enforces
that an extra-args tree appearing inside another extra-args tree doesn't
actually have extra args.  This invariant doesn't always hold for lambdas
(which recently gained the extra-args mechanism) but that should be
harmless since cp_walk_subtrees doesn't walk LAMBDA_EXPR_EXTRA_ARGS and
so should be immune to the PR114303 issue for now.  So let's just disable
this assert for lambdas.

	PR c++/117054

gcc/cp/ChangeLog:

	* pt.cc (extract_locals_r): Disable tree_extra_args assert
	for LAMBDA_EXPR.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/lambda-targ9.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
---
 gcc/cp/pt.cc                              |  7 ++++++-
 gcc/testsuite/g++.dg/cpp2a/lambda-targ9.C | 16 ++++++++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/lambda-targ9.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index c0a37a51cba3..c9219d5b3a5a 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -13480,7 +13480,12 @@ extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
        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);
+    gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE
+			/* Except a lambda nested inside an extra-args tree
+			   can have extra args if we deferred partial
+			   substitution into it at template parse time.  But
+			   we don't walk LAMBDA_EXPR_EXTRA_ARGS anyway.  */
+			 || TREE_CODE (*tp) == LAMBDA_EXPR);
 
   if (TREE_CODE (*tp) == DECL_EXPR)
     {
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-targ9.C b/gcc/testsuite/g++.dg/cpp2a/lambda-targ9.C
new file mode 100644
index 000000000000..41f8526184a8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/lambda-targ9.C
@@ -0,0 +1,16 @@
+// PR c++/117054
+// { dg-do compile { target c++20 } }
+
+template<auto = []{}>
+constexpr bool v = true;
+
+template<typename>
+void f() {
+  [](auto) {
+    if constexpr (v<>) { }
+  }(0);
+}
+
+int main() {
+  f<int>();
+}
-- 
GitLab