diff mbox series

c++/modules: Finalise non-local imported vars [PR113708]

Message ID 65c8cafd.050a0220.3ef59.516b@mx.google.com
State New
Headers show
Series c++/modules: Finalise non-local imported vars [PR113708] | expand

Commit Message

Nathaniel Shead Feb. 11, 2024, 1:26 p.m. UTC
Bootstrapped and regtested (just modules.exp so far) on
x86_64-pc-linux-gnu, OK for trunk if full regtest succeeds?

-- >8 --

Currently inline vars imported from modules aren't correctly finalised,
which means that import_export_decl gets called at the end of TU
processing despite not being meant to for these kinds of declarations.

This patch takes the logic from 'make_rtl_for_nonlocal_decl' to
determine when to perform rest of decl compilation on these decls.
However other parts of this function (asmspec handling) are not yet
covered.

	PR c++/113708

gcc/cp/ChangeLog:

	* module.cc (trees_in::read_var_def): Perform rest of decl
	compilation on non-local statics.

gcc/testsuite/ChangeLog:

	* g++.dg/modules/init-7_a.H: New test.
	* g++.dg/modules/init-7_b.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
---
 gcc/cp/module.cc                        | 12 +++++++++++-
 gcc/testsuite/g++.dg/modules/init-7_a.H |  6 ++++++
 gcc/testsuite/g++.dg/modules/init-7_b.C |  6 ++++++
 3 files changed, 23 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/modules/init-7_a.H
 create mode 100644 gcc/testsuite/g++.dg/modules/init-7_b.C

Comments

Jason Merrill Feb. 13, 2024, 11:08 p.m. UTC | #1
On 2/11/24 08:26, Nathaniel Shead wrote:
> 
> Currently inline vars imported from modules aren't correctly finalised,
> which means that import_export_decl gets called at the end of TU
> processing despite not being meant to for these kinds of declarations.

I disagree that it's not meant to; inline variables are vague linkage 
just like template instantiations, so the bug seems to be that 
import_export_decl doesn't accept them.  And on the other side, that 
make_rtl_for_nonlocal_decl doesn't defer them like instantations.

Jason
diff mbox series

Patch

diff --git a/gcc/cp/module.cc b/gcc/cp/module.cc
index 560d8f3b614..49a3421e6cc 100644
--- a/gcc/cp/module.cc
+++ b/gcc/cp/module.cc
@@ -11809,6 +11809,7 @@  trees_in::read_var_def (tree decl, tree maybe_template)
     {
       if (DECL_EXTERNAL (decl))
 	DECL_NOT_REALLY_EXTERN (decl) = true;
+      DECL_INITIAL (decl) = init;
       if (VAR_P (decl))
 	{
 	  DECL_INITIALIZED_P (decl) = true;
@@ -11819,8 +11820,17 @@  trees_in::read_var_def (tree decl, tree maybe_template)
 		  && !DECL_VTABLE_OR_VTT_P (decl)
 		  && !DECL_TEMPLATE_INFO (decl)))
 	    note_vague_linkage_variable (decl);
+
+	  /* Emit RTL as needed, as with make_rtl_for_nonlocal_decl.  */
+	  // FIXME: Handle asmspec?
+	  if ((!DECL_FUNCTION_SCOPE_P (decl)
+	       || (TREE_STATIC (decl) && var_in_maybe_constexpr_fn (decl)))
+	      && !DECL_VIRTUAL_P (decl)
+	      && (!DECL_TEMPLATE_INFO (decl)
+		  || DECL_EXPLICIT_INSTANTIATION (decl)))
+	    rest_of_decl_compilation (decl, !DECL_FUNCTION_SCOPE_P (decl),
+				      /*at_end=*/0);
 	}
-      DECL_INITIAL (decl) = init;
       if (!dyn_init)
 	;
       else if (CP_DECL_THREAD_LOCAL_P (decl))
diff --git a/gcc/testsuite/g++.dg/modules/init-7_a.H b/gcc/testsuite/g++.dg/modules/init-7_a.H
new file mode 100644
index 00000000000..7a0bb721c30
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/init-7_a.H
@@ -0,0 +1,6 @@ 
+// PR c++/113708
+// { dg-additional-options "-fmodule-header" }
+// { dg-module-cmi {} }
+
+inline int f() { return 42; }
+inline int a = f();
diff --git a/gcc/testsuite/g++.dg/modules/init-7_b.C b/gcc/testsuite/g++.dg/modules/init-7_b.C
new file mode 100644
index 00000000000..58bb0620ca5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/init-7_b.C
@@ -0,0 +1,6 @@ 
+// PR c++/113708
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import "init-7_a.H";
+int main() { a; }