On 1/31/26 7:00 PM, Nathaniel Shead wrote:
Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk/15?
OK.
-- >8 --
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]>
---
gcc/cp/module.cc | 15 ++++++++++++---
gcc/testsuite/g++.dg/modules/dep-5.C | 5 +++++
2 files changed, 17 insertions(+), 3 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/modules/dep-5.C
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" }