https://gcc.gnu.org/g:be81c5c01c243013c4bac0718e63e0fdc132d384

commit r16-2459-gbe81c5c01c243013c4bac0718e63e0fdc132d384
Author: Nathaniel Shead <nathanielosh...@gmail.com>
Date:   Sat May 24 10:56:22 2025 +1000

    c++/modules: Support re-streaming TU_LOCAL_ENTITYs [PR120412]
    
    When emitting a primary module interface, we must re-stream any TU-local
    entities that we saw in a partition.  This patch adds the missing
    members from core_vals.
    
    As a drive-by fix, in some cases we might have a typedef referring to a
    TU-local entity; we need to handle that case as well.
    
            PR c++/120412
    
    gcc/cp/ChangeLog:
    
            * module.cc (trees_out::core_vals): Write TU_LOCAL_ENTITY bits.
            (trees_in::core_vals): Read it.
            (trees_in::tree_node): Handle TU_LOCAL_ENTITY typedefs.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/internal-14_a.C: New test.
            * g++.dg/modules/internal-14_b.C: New test.
            * g++.dg/modules/internal-14_c.C: New test.
    
    Signed-off-by: Nathaniel Shead <nathanielosh...@gmail.com>
    Reviewed-by: Jason Merrill <ja...@redhat.com>

Diff:
---
 gcc/cp/module.cc                             | 15 ++++++++++++++-
 gcc/testsuite/g++.dg/modules/internal-14_a.C | 17 +++++++++++++++++
 gcc/testsuite/g++.dg/modules/internal-14_b.C |  6 ++++++
 gcc/testsuite/g++.dg/modules/internal-14_c.C |  9 +++++++++
 4 files changed, 46 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index e3c1a686b732..a551509ec0b4 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -6785,6 +6785,13 @@ trees_out::core_vals (tree t)
       if (streaming_p ())
        WU (((lang_tree_node *)t)->trait_expression.kind);
       break;
+
+    case TU_LOCAL_ENTITY:
+      WT (((lang_tree_node *)t)->tu_local_entity.name);
+      if (state)
+       state->write_location
+         (*this, ((lang_tree_node *)t)->tu_local_entity.loc);
+      break;
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
@@ -7328,6 +7335,11 @@ trees_in::core_vals (tree t)
       RT (((lang_tree_node *)t)->trait_expression.type2);
       RUC (cp_trait_kind, ((lang_tree_node *)t)->trait_expression.kind);
       break;
+
+    case TU_LOCAL_ENTITY:
+      RT (((lang_tree_node *)t)->tu_local_entity.name);
+      ((lang_tree_node *)t)->tu_local_entity.loc
+       = state->read_location (*this);
     }
 
   if (CODE_CONTAINS_STRUCT (code, TS_TYPED))
@@ -10268,7 +10280,8 @@ trees_in::tree_node (bool is_use)
            && dump ("Read %stypedef %C:%N",
                     DECL_IMPLICIT_TYPEDEF_P (res) ? "implicit " : "",
                     TREE_CODE (res), res);
-         res = TREE_TYPE (res);
+         if (TREE_CODE (res) != TU_LOCAL_ENTITY)
+           res = TREE_TYPE (res);
        }
       break;
 
diff --git a/gcc/testsuite/g++.dg/modules/internal-14_a.C 
b/gcc/testsuite/g++.dg/modules/internal-14_a.C
new file mode 100644
index 000000000000..07eb9658951b
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/internal-14_a.C
@@ -0,0 +1,17 @@
+// PR c++/120412
+// { dg-additional-options "-fmodules -std=c++20 -Wtemplate-names-tu-local" }
+// { dg-module-cmi m:part }
+
+export module m:part;
+
+export template <typename F>
+auto fun1(F) {
+  return true;
+}
+
+using Dodgy = decltype([]{});
+
+export template <typename T>
+auto fun2(T&&) {  // { dg-warning "TU-local" }
+  return fun1(Dodgy{});
+}
diff --git a/gcc/testsuite/g++.dg/modules/internal-14_b.C 
b/gcc/testsuite/g++.dg/modules/internal-14_b.C
new file mode 100644
index 000000000000..ad3b09d0722e
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/internal-14_b.C
@@ -0,0 +1,6 @@
+// PR c++/120412
+// { dg-additional-options "-fmodules -std=c++20 -Wtemplate-names-tu-local" }
+// { dg-module-cmi m }
+
+export module m;
+export import :part;
diff --git a/gcc/testsuite/g++.dg/modules/internal-14_c.C 
b/gcc/testsuite/g++.dg/modules/internal-14_c.C
new file mode 100644
index 000000000000..4f8e785ce872
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/internal-14_c.C
@@ -0,0 +1,9 @@
+// PR c++/120412
+// { dg-additional-options "-fmodules -std=c++20" }
+
+import m;
+
+int main() {
+  // { dg-error "instantiation exposes TU-local entity '(fun1|Dodgy)'" "" { 
target *-*-* } 0 }
+  fun2(123);  // { dg-message "required from here" }
+}

Reply via email to