https://gcc.gnu.org/g:933f0c20d4ce1dba85e85d9d117cfd9f5376a945

commit r15-6759-g933f0c20d4ce1dba85e85d9d117cfd9f5376a945
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Fri Jan 10 10:31:12 2025 +0100

    c++: Fix up modules handling of namespace scope structured bindings
    
    With the following patch I actually get a simple namespace scope structured
    binding working with modules.
    
    The core_vals change ensure we actually save/restore DECL_VALUE_EXPR even
    for namespace scope vars, the get_merge_kind is based on the assumption
    that structured bindings are always unique, one can't redeclare them and
    without it we really ICE because their base vars have no name.
    
    2025-01-10  Jakub Jelinek  <ja...@redhat.com>
    
            * module.cc (trees_out::core_vals): Note DECL_VALUE_EXPR even for
            vars outside of functions.
            (trees_in::core_vals): Read in DECL_VALUE_EXPR even for vars outside
            of functions.
            (trees_out::get_merge_kind): Make DECL_DECOMPOSITION_P MK_unique.
    
            * g++.dg/modules/decomp-2_b.C: New test.
            * g++.dg/modules/decomp-2_a.H: New file.

Diff:
---
 gcc/cp/module.cc                          | 21 +++++++++++++++++++--
 gcc/testsuite/g++.dg/modules/decomp-2_a.H | 11 +++++++++++
 gcc/testsuite/g++.dg/modules/decomp-2_b.C | 11 +++++++++++
 3 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index fec820603521..7288c46a7baa 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -6318,7 +6318,11 @@ trees_out::core_vals (tree t)
     case VAR_DECL:
       if (DECL_CONTEXT (t)
          && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
-       break;
+       {
+         if (DECL_HAS_VALUE_EXPR_P (t))
+           WT (DECL_VALUE_EXPR (t));
+         break;
+       }
       /* FALLTHROUGH  */
 
     case RESULT_DECL:
@@ -6848,7 +6852,14 @@ trees_in::core_vals (tree t)
     case VAR_DECL:
       if (DECL_CONTEXT (t)
          && TREE_CODE (DECL_CONTEXT (t)) != FUNCTION_DECL)
-       break;
+       {
+         if (DECL_HAS_VALUE_EXPR_P (t))
+           {
+             tree val = tree_node ();
+             SET_DECL_VALUE_EXPR (t, val);
+           }
+         break;
+       }
       /* FALLTHROUGH  */
 
     case RESULT_DECL:
@@ -10990,6 +11001,12 @@ trees_out::get_merge_kind (tree decl, depset *dep)
                break;
              }
 
+           if (DECL_DECOMPOSITION_P (decl))
+             {
+               mk = MK_unique;
+               break;
+             }
+
            if (IDENTIFIER_ANON_P (DECL_NAME (decl)))
              {
                if (RECORD_OR_UNION_TYPE_P (ctx))
diff --git a/gcc/testsuite/g++.dg/modules/decomp-2_a.H 
b/gcc/testsuite/g++.dg/modules/decomp-2_a.H
new file mode 100644
index 000000000000..df2d82abcbc1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/decomp-2_a.H
@@ -0,0 +1,11 @@
+// { dg-additional-options -fmodule-header }
+// { dg-module-cmi {} }
+
+struct A {
+  int a, b, c;
+};
+
+namespace {
+A d = { 1, 2, 3 };
+auto [a, b, c] = d;
+}
diff --git a/gcc/testsuite/g++.dg/modules/decomp-2_b.C 
b/gcc/testsuite/g++.dg/modules/decomp-2_b.C
new file mode 100644
index 000000000000..0353c8e87c93
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/decomp-2_b.C
@@ -0,0 +1,11 @@
+// { dg-do run }
+// { dg-additional-options "-fmodules-ts" }
+
+import "decomp-2_a.H";
+
+int
+main ()
+{
+  if (a != 1 || b != 2 || c != 3)
+    __builtin_abort ();
+}

Reply via email to