On 4/14/21 3:10 PM, Jakub Jelinek wrote:
Hi!

The following testcase ICEs in tsubst_decomp_names because the assumptions
that the structured binding artificial var is followed in DECL_CHAIN by
the corresponding structured binding vars is violated.
I've tracked it to extract_locals* which is done for the constexpr
IF_STMT.  extract_locals_r when it sees a DECL_EXPR adds that decl
into a hash set so that such decls aren't returned from extract_locals*,
but in the case of a structured binding that just means the artificial var
and not the vars corresponding to structured binding identifiers.
The following patch fixes it by pushing not just the artificial var
for structured bindings but also the other vars.

Bootstrapped/regtested on x86_64-linux and i686-linux, ok for trunk?

OK.

2021-04-14  Jakub Jelinek  <ja...@redhat.com>

        PR c++/99833
        * pt.c (extract_locals_r): When handling DECL_EXPR of a structured
        binding, add to data.internal also all corresponding structured
        binding decls.

        * g++.dg/cpp1z/pr99833.C: New test.
        * g++.dg/cpp2a/pr99833.C: New test.

--- gcc/cp/pt.c.jj      2021-04-14 10:48:41.322103670 +0200
+++ gcc/cp/pt.c 2021-04-14 12:52:53.116896754 +0200
@@ -12811,7 +12811,27 @@ extract_locals_r (tree *tp, int */*walk_
      tp = &TYPE_NAME (*tp);
if (TREE_CODE (*tp) == DECL_EXPR)
-    data.internal.add (DECL_EXPR_DECL (*tp));
+    {
+      tree decl = DECL_EXPR_DECL (*tp);
+      data.internal.add (decl);
+      if (VAR_P (decl)
+         && DECL_DECOMPOSITION_P (decl)
+         && TREE_TYPE (decl) != error_mark_node)
+       {
+         gcc_assert (DECL_NAME (decl) == NULL_TREE);
+         for (tree decl2 = DECL_CHAIN (decl);
+              decl2
+              && VAR_P (decl2)
+              && DECL_DECOMPOSITION_P (decl2)
+              && DECL_NAME (decl2)
+              && TREE_TYPE (decl2) != error_mark_node;
+              decl2 = DECL_CHAIN (decl2))
+           {
+             gcc_assert (DECL_DECOMP_BASE (decl2) == decl);
+             data.internal.add (decl2);
+           }
+       }
+    }
    else if (TREE_CODE (*tp) == LAMBDA_EXPR)
      {
        /* Since we defer implicit capture, look in the parms and body.  */
--- gcc/testsuite/g++.dg/cpp1z/pr99833.C.jj     2021-04-14 13:03:14.654879632 
+0200
+++ gcc/testsuite/g++.dg/cpp1z/pr99833.C        2021-04-14 13:03:39.599598004 
+0200
@@ -0,0 +1,11 @@
+// PR c++/99833
+// { dg-do compile { target c++17 } }
+
+struct S { int a, b; };
+template <class>
+void
+foo ()
+{
+  [](auto d) { if constexpr (auto [a, b]{d}; sizeof (a) > 0) a++; } (S{});
+}
+template void foo<S> ();
--- gcc/testsuite/g++.dg/cpp2a/pr99833.C.jj     2021-04-14 13:04:08.975266383 
+0200
+++ gcc/testsuite/g++.dg/cpp2a/pr99833.C        2021-04-14 13:04:23.191105881 
+0200
@@ -0,0 +1,18 @@
+// PR c++/99833
+// { dg-do compile { target c++20 } }
+
+#include <tuple>
+
+auto f(auto&& x)
+{
+  [&](auto...) {
+    auto y = std::tuple{ "what's happening here?", x };
+    if constexpr (auto [_, z] = y; requires { z; })
+      return;
+  }();
+}
+
+int main()
+{
+  f(42);
+}

        Jakub


Reply via email to