Hi all,

PING.

Re-Regtested ok again on x86_64-pc-linux-gnu. Ok for mainline?

Regards,
        Andre

On Wed, 25 Sep 2024 12:29:21 +0200
Andre Vehreschild <ve...@gmx.de> wrote:

> Hi all and esp. Paul,
>
> the attached patch fixes an ICE with coarrays defined in modules and then used
> in submodules. Referencing the variable relied on the curr_module being set in
> the gfc_build_qualified_array routine, which it was not. I therefore took the
> name from the symbol. I don't know if this is correct. Paul any idea? You have
> done the submodule part in the past. I am still not sure, if the coarray is
> exported into the .mod file correctly, because it gets host_assoc set, while I
> would expect it to have use_assoc set. So may be this patch is only healing
> the symptom and not the cause. Could you have a look, please?
>
> Regtests ok on x86_64-pc-linux-gnu / Fedora 39. Ok for mainline?
>
> Regards,
>       Andre
> --
> Andre Vehreschild * Email: vehre ad gmx dot de


--
Andre Vehreschild * Email: vehre ad gmx dot de
From 3f9876347c6229270f18743ff17bc55175b49a57 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild <ve...@gcc.gnu.org>
Date: Tue, 24 Sep 2024 14:30:52 +0200
Subject: [PATCH] [Fortran] Fix ICE with coarrays and submodules [PR80235]

Exposing a variable in a module and referencing it in a submodule made
the compiler ICE, because the external variable was not sorted into the
correct module.  In fact the module name was not set where the variable
got built.

gcc/fortran/ChangeLog:

	PR fortran/80235

	* trans-decl.cc (gfc_build_qualified_array): Make sure the array
	is associated to the correct module and being marked as extern.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray/add_sources/submodule_1_sub.f90: New test.
	* gfortran.dg/coarray/submodule_1.f90: New test.
---
 gcc/fortran/trans-decl.cc                     |  7 +++--
 .../coarray/add_sources/submodule_1_sub.f90   | 22 ++++++++++++++
 .../gfortran.dg/coarray/submodule_1.f90       | 29 +++++++++++++++++++
 3 files changed, 56 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/add_sources/submodule_1_sub.f90
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/submodule_1.f90

diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc
index 56b6202510e..9cced7c02e4 100644
--- a/gcc/fortran/trans-decl.cc
+++ b/gcc/fortran/trans-decl.cc
@@ -1066,7 +1066,8 @@ gfc_build_qualified_array (tree decl, gfc_symbol * sym)
 			IDENTIFIER_POINTER (gfc_sym_mangled_identifier (sym))));
 	  token = build_decl (DECL_SOURCE_LOCATION (decl), VAR_DECL, token_name,
 			      token_type);
-	  if (sym->attr.use_assoc)
+	  if (sym->attr.use_assoc
+	      || (sym->attr.host_assoc && sym->attr.used_in_submodule))
 	    DECL_EXTERNAL (token) = 1;
 	  else
 	    TREE_STATIC (token) = 1;
@@ -1091,9 +1092,11 @@ gfc_build_qualified_array (tree decl, gfc_symbol * sym)

       if (sym->module && !sym->attr.use_assoc)
 	{
+	  module_htab_entry *mod
+	    = cur_module ? cur_module : gfc_find_module (sym->module);
 	  pushdecl (token);
 	  DECL_CONTEXT (token) = sym->ns->proc_name->backend_decl;
-	  gfc_module_add_decl (cur_module, token);
+	  gfc_module_add_decl (mod, token);
 	}
       else if (sym->attr.host_assoc
 	       && TREE_CODE (DECL_CONTEXT (current_function_decl))
diff --git a/gcc/testsuite/gfortran.dg/coarray/add_sources/submodule_1_sub.f90 b/gcc/testsuite/gfortran.dg/coarray/add_sources/submodule_1_sub.f90
new file mode 100644
index 00000000000..fd177fcda29
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/add_sources/submodule_1_sub.f90
@@ -0,0 +1,22 @@
+! This test belongs to submodule_1.f90
+! It is references as additional source in that test.
+! The two code fragments need to be in separate files to show
+! the error of pr80235.
+
+submodule (pr80235) pr80235_sub
+
+contains
+  module subroutine test()
+    implicit none
+    if (var%v /= 42) stop 1
+  end subroutine
+end submodule pr80235_sub
+
+program pr80235_prg
+  use pr80235
+
+  implicit none
+
+  var%v = 42
+  call test()
+end program
diff --git a/gcc/testsuite/gfortran.dg/coarray/submodule_1.f90 b/gcc/testsuite/gfortran.dg/coarray/submodule_1.f90
new file mode 100644
index 00000000000..d0faef93ba7
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/submodule_1.f90
@@ -0,0 +1,29 @@
+!{ dg-do run }
+!{ dg-additional-sources add_sources/submodule_1_sub.f90 }
+
+! Separating the module and the submodule is needed to show the error.
+! Having all code pieces in one file does not show the error.
+
+module pr80235
+  implicit none
+
+  private
+  public :: test, var
+
+  type T
+    integer :: v
+  end type T
+
+interface
+
+  module subroutine test()
+  end subroutine
+
+end interface
+
+  type(T) :: var[*]
+
+end module pr80235
+
+
+
--
2.46.2

Reply via email to