From: Nathaniel Shead Date: Sat, 31 Jan 2026 10:45:39 +0000 (+1100) Subject: c++/modules: Fix ICE with multiple module declarations and -M [PR123738] X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c6dd51c062667fdf2db3bed0ab50e034e048f3b5;p=thirdparty%2Fgcc.git 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 Reviewed-by: Jason Merrill --- diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc index 2d9b727d2b3..ccbf124876d 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 00000000000..88e22c16d9d --- /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" }