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

commit r16-7393-gc6dd51c062667fdf2db3bed0ab50e034e048f3b5
Author: Nathaniel Shead <[email protected]>
Date:   Sat Jan 31 21:45:39 2026 +1100

    c++/modules: Fix ICE with multiple module declarations and -M [PR123738]
    
    When only doing preprocessing, we do not call declare_module, and so we
    do not check that a module has been declared multiple times.  This means
    we end up with multiple "primary modules" being passed to the dependency
    producing logic, which asserts.  Fixed by checking if we've already seen
    a module declaration and complaining.
    
            PR c++/123738
    
    gcc/cp/ChangeLog:
    
            * module.cc (preprocess_module): Complain for any non-imported
            module declaration in a module purview.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/modules/dep-5.C: New test.
    
    Signed-off-by: Nathaniel Shead <[email protected]>
    Reviewed-by: Jason Merrill <[email protected]>

Diff:
---
 gcc/cp/module.cc                     | 15 ++++++++++++---
 gcc/testsuite/g++.dg/modules/dep-5.C |  5 +++++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 2d9b727d2b3d..ccbf124876d8 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -23235,9 +23235,18 @@ preprocess_module (module_state *module, location_t 
from_loc,
 {
   if (!is_import)
     {
-      if (module->loc)
-       /* It's already been mentioned, so ignore its module-ness.  */
-       is_import = true;
+      if (in_purview || module->loc)
+       {
+         /* We've already seen a module declaration.  If only preprocessing
+            then we won't complain in declare_module, so complain here.  */
+         if (flag_preprocess_only)
+           error_at (from_loc,
+                     in_purview
+                     ? G_("module already declared")
+                     : G_("module already imported"));
+         /* Always pretend this was an import to aid error recovery.  */
+         is_import = true;
+       }
       else
        {
          /* Record it is the module.  */
diff --git a/gcc/testsuite/g++.dg/modules/dep-5.C 
b/gcc/testsuite/g++.dg/modules/dep-5.C
new file mode 100644
index 000000000000..88e22c16d9d9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/dep-5.C
@@ -0,0 +1,5 @@
+// { dg-additional-options "-fmodules -M" }
+
+export module A.B:X.Y;
+export module A.B:X.Y;  // { dg-error "module already declared" }
+export module F;       // { dg-error "module already declared" }

Reply via email to