]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Reparent linemaps for partition direct loads [PR124309]
authorNathaniel Shead <nathanieloshead@gmail.com>
Sun, 8 Mar 2026 14:10:02 +0000 (01:10 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Thu, 12 Mar 2026 23:38:31 +0000 (10:38 +1100)
The attached testcase failed because when part-11_d.C sees the import
of X through Z:part, the module's location is maintained as the indirect
(imported) location through Y that came first, but it is still promoted
to a direct import.

When part-11_e.C reads the module then it sees a direct import of X
through Z, but there is no valid source location for X in Z (because we
only kept the imported location) and so we report a bad CMI.

This fixes the issue by updating a module seen as direct for the first
time via a partition to reparent the module's location to where the
partition imported it from, so we properly process it as a direct
location that we need to write out, similarly to what we do when we
reimport an indirect module within the module purview.

PR c++/124309

gcc/cp/ChangeLog:

* module.cc (enum module_directness): Fix inconsistent
capitalisation.
(module_state::read_imports): Reparent module locations newly
seen as direct via partition.

gcc/testsuite/ChangeLog:

* g++.dg/modules/part-11_a.C: New test.
* g++.dg/modules/part-11_b.C: New test.
* g++.dg/modules/part-11_c.C: New test.
* g++.dg/modules/part-11_d.C: New test.
* g++.dg/modules/part-11_e.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/part-11_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-11_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-11_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-11_d.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/part-11_e.C [new file with mode: 0644]

index 628dec9c1b23f15eb2a3c30a9327846d2cec23af..2125cc00fbe73411f76c4654de55a1b032b7d315 100644 (file)
@@ -3735,7 +3735,7 @@ enum module_directness {
   MD_NONE,             /* Not direct.  */
   MD_PARTITION_DIRECT, /* Direct import of a partition.  */
   MD_DIRECT,           /* Direct import.  */
-  MD_PURVIEW_DIRECT,   /* direct import in purview.  */
+  MD_PURVIEW_DIRECT,   /* Direct import in purview.  */
 };
 
 /* State of a particular module. */
@@ -16974,8 +16974,11 @@ module_state::read_imports (bytes_in &sec, cpp_reader *reader, line_maps *lmaps)
 
          if (is_partition ())
            {
-             if (!imp->is_direct ())
-               imp->directness = MD_PARTITION_DIRECT;
+             if (!imp->is_direct () && !imp->is_partition_direct ())
+               {
+                 imp->directness = MD_PARTITION_DIRECT;
+                 linemap_module_reparent (line_table, imp->loc, floc);
+               }
              if (exportedness > 0)
                imp->exported_p = true;
            }
diff --git a/gcc/testsuite/g++.dg/modules/part-11_a.C b/gcc/testsuite/g++.dg/modules/part-11_a.C
new file mode 100644 (file)
index 0000000..d3594e7
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/124309
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi X }
+
+export module X;
+
+export void frob(int*);
diff --git a/gcc/testsuite/g++.dg/modules/part-11_b.C b/gcc/testsuite/g++.dg/modules/part-11_b.C
new file mode 100644 (file)
index 0000000..f4b0aef
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/124309
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi Y }
+
+export module Y;
+export import X;
diff --git a/gcc/testsuite/g++.dg/modules/part-11_c.C b/gcc/testsuite/g++.dg/modules/part-11_c.C
new file mode 100644 (file)
index 0000000..2fb4aad
--- /dev/null
@@ -0,0 +1,6 @@
+// PR c++/124309
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi Z:part }
+
+export module Z:part;
+export import X;
diff --git a/gcc/testsuite/g++.dg/modules/part-11_d.C b/gcc/testsuite/g++.dg/modules/part-11_d.C
new file mode 100644 (file)
index 0000000..a21b954
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/124309
+// { dg-additional-options "-fmodules" }
+// { dg-module-cmi Z }
+
+export module Z;
+export import Y;
+export import :part;
diff --git a/gcc/testsuite/g++.dg/modules/part-11_e.C b/gcc/testsuite/g++.dg/modules/part-11_e.C
new file mode 100644 (file)
index 0000000..9a41ae2
--- /dev/null
@@ -0,0 +1,10 @@
+// PR c++/124309
+// { dg-additional-options "-fmodules" }
+
+import Z;
+
+int main() {
+  frob(2);  // { dg-error "invalid conversion" }
+}
+
+// { dg-regexp "In module X, imported at \[^\n]*part-11_c.C:6,\n\\\s*included from \[^\n]*part-11_d.C:7,\nof module Z, imported at \[^\n]*part-11_e.C:4:\n\[^\n]*part-11_a.C:7:18: note:.*" }