https://gcc.gnu.org/bugzilla/show_bug.cgi?id=123393

Nathaniel Shead <nshead at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |ASSIGNED
                 CC|                            |nshead at gcc dot gnu.org
   Last reconfirmed|                            |2026-01-08
     Ever confirmed|0                           |1
           Keywords|                            |ice-on-valid-code
           Assignee|unassigned at gcc dot gnu.org      |nshead at gcc dot 
gnu.org

--- Comment #1 from Nathaniel Shead <nshead at gcc dot gnu.org> ---
Confirmed.  Reduced:


// a.cpp
export module fmt;
inline namespace ns {
  export template <typename = int> void format();
  namespace {}
}


// b.cpp
export module m;
import fmt;
inline void test() { format(); }


$ g++ -fmodules -S [ab].cpp
b.cpp:1:8: internal compiler error: in operator(), at cp/module.cc:17853
    1 | export module m;
      |        ^~~~~~
0x3e9f7f0 internal_error(char const*, ...)
        ../../gcc/gcc/diagnostic-global-context.cc:787
0x3eaf502 fancy_abort(char const*, int, char const*)
        ../../gcc/gcc/diagnostics/context.cc:1805
0x122f934 operator()
        ../../gcc/gcc/cp/module.cc:17853
0x122fb0f module_state::write_using_directives(elf_out*, depset::hash&,
vec<depset*, va_heap, vl_ptr>, unsigned int*)
        ../../gcc/gcc/cp/module.cc:17868
0x123984b module_state::write_begin(elf_out*, cpp_reader*,
module_state_config&, unsigned int&)
        ../../gcc/gcc/cp/module.cc:20991
0x12420e1 finish_module_processing(cpp_reader*)
        ../../gcc/gcc/cp/module.cc:23587
0x114a68d c_parse_final_cleanups()
        ../../gcc/gcc/cp/decl2.cc:5830
0x15805f9 c_common_parse_file()
        ../../gcc/gcc/c-family/c-opts.cc:1440
Please submit a full bug report, with preprocessed source (by using
-freport-bug).
Please include the complete backtrace with any bug report.
See <https://gcc.gnu.org/bugs/> for instructions.


The issue is that the implicit using-directive for the anonymous namespace is
not considered imported, because make_namespace_finish calls
add_using_namespace without propagating 'from_import'.  The following patch
should fix the ICE; I'll test and submit soon:

diff --git a/gcc/cp/name-lookup.cc b/gcc/cp/name-lookup.cc
index 8a7a81a9e32..9cf8a38980f 100644
--- a/gcc/cp/name-lookup.cc
+++ b/gcc/cp/name-lookup.cc
@@ -9288,7 +9288,8 @@ make_namespace_finish (tree ns, tree *slot, bool
from_import = false)
   /* An unnamed namespace implicitly has a using-directive inserted so
      that its contents are usable in the surrounding context.  */
   if (!DECL_NAMESPACE_INLINE_P (ns) && !DECL_NAME (ns))
-    add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns);
+    add_using_namespace (NAMESPACE_LEVEL (ctx)->using_directives, ns,
+                         from_import);
 }

 /* NS is a possibly-imported namespace that is now needed for

Reply via email to