diff mbox series

[pushed] c++: constexpr if and nested generic lambda [PR99201]

Message ID 20210405122610.959952-1-jason@redhat.com
State New
Headers show
Series [pushed] c++: constexpr if and nested generic lambda [PR99201] | expand

Commit Message

Jason Merrill April 5, 2021, 12:26 p.m. UTC
When building up *_EXTRA_ARGS for a constexpr if or pack expansion, we need
to walk into the body of a lambda to find all the local_specializations that
we need to remember, like we do in find_parameter_packs_r.

Tested x86_64-pc-linux-gnu, applying to trunk.

gcc/cp/ChangeLog:

	PR c++/99201
	* pt.c (class el_data): Add visited field.
	(extract_local_specs): Pass it to cp_walk_tree.
	(extract_locals_r): Walk into the body of a lambda.

gcc/testsuite/ChangeLog:

	PR c++/99201
	* g++.dg/cpp1z/constexpr-if-lambda4.C: New test.
---
 gcc/cp/pt.c                                   | 15 ++++++++++++-
 .../g++.dg/cpp1z/constexpr-if-lambda4.C       | 22 +++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda4.C


base-commit: a44a753a35542f86e82e198595ce3553f6d718f6
diff mbox series

Patch

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 2763aa15f1f..1d19a59dd62 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -12757,7 +12757,11 @@  tsubst_binary_right_fold (tree t, tree args, tsubst_flags_t complain,
 class el_data
 {
 public:
+  /* Set of variables declared within the pattern.  */
   hash_set<tree> internal;
+  /* Set of AST nodes that have been visited by the traversal.  */
+  hash_set<tree> visited;
+  /* List of local_specializations used within the pattern.  */
   tree extra;
   tsubst_flags_t complain;
 
@@ -12777,6 +12781,15 @@  extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
 
   if (TREE_CODE (*tp) == DECL_EXPR)
     data.internal.add (DECL_EXPR_DECL (*tp));
+  else if (TREE_CODE (*tp) == LAMBDA_EXPR)
+    {
+      /* Since we defer implicit capture, look in the parms and body.  */
+      tree fn = lambda_function (*tp);
+      cp_walk_tree (&TREE_TYPE (fn), &extract_locals_r, &data,
+		    &data.visited);
+      cp_walk_tree (&DECL_SAVED_TREE (fn), &extract_locals_r, &data,
+		    &data.visited);
+    }
   else if (tree spec = retrieve_local_specialization (*tp))
     {
       if (data.internal.contains (*tp))
@@ -12833,7 +12846,7 @@  static tree
 extract_local_specs (tree pattern, tsubst_flags_t complain)
 {
   el_data data (complain);
-  cp_walk_tree_without_duplicates (&pattern, extract_locals_r, &data);
+  cp_walk_tree (&pattern, extract_locals_r, &data, &data.visited);
   return data.extra;
 }
 
diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda4.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda4.C
new file mode 100644
index 00000000000..99408025629
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda4.C
@@ -0,0 +1,22 @@ 
+// PR c++/99201
+// { dg-do compile { target c++17 } }
+
+template <typename RefF>
+  auto
+  make_tester(const RefF& reffun)
+  {
+    return [=](auto in) {
+      auto&& expected = [&](const auto&... vs) {
+        if constexpr (sizeof(in) > 0)
+          return [&](int i) { return reffun(vs[i]...); }(0);
+        else
+          return [&](int i) { return reffun(vs[i]...); }(0);
+      };
+    };
+  }
+
+int main()
+{
+  make_tester([](int x) { return x; })(0);
+  return 0;
+}