public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r10-9844] c++: constexpr if and nested generic lambda [PR99201]
@ 2021-05-20 21:34 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-05-20 21:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:7c365bb1462a7a9dadf9b8f8668a0c832b18056c

commit r10-9844-g7c365bb1462a7a9dadf9b8f8668a0c832b18056c
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Apr 4 01:01:56 2021 -0400

    c++: constexpr if and nested generic lambda [PR99201]
    
    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.
    
    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.

Diff:
---
 gcc/cp/pt.c                                       | 15 ++++++++++++++-
 gcc/testsuite/g++.dg/cpp1z/constexpr-if-lambda4.C | 22 ++++++++++++++++++++++
 2 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 8616118228c..986c8207a49 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -12672,7 +12672,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;
 
@@ -12712,6 +12716,15 @@ extract_locals_r (tree *tp, int */*walk_subtrees*/, void *data_)
 	    }
 	}
     }
+  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))
@@ -12768,7 +12781,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;
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-05-20 21:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-05-20 21:34 [gcc r10-9844] c++: constexpr if and nested generic lambda [PR99201] Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).