]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Fix ICE with multiple module declarations and -M [PR123738]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 31 Jan 2026 10:45:39 +0000 (21:45 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sun, 8 Feb 2026 06:27:19 +0000 (17:27 +1100)
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 <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/module.cc
gcc/testsuite/g++.dg/modules/dep-5.C [new file with mode: 0644]

index 2d9b727d2b3df12df5117193e623ae00f1d137be..ccbf124876d80975c21f72467b36aff69a5b830e 100644 (file)
@@ -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 (file)
index 0000000..88e22c1
--- /dev/null
@@ -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" }