[PATCH v2] fortran: Fix host association in module procedure interface bodies [PR79330]

Gonzalo Silvalde <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <[email protected]>
From: Gonzalosilvalde <[email protected]>

Named constants from the host scope were not accessible in module
procedure interface bodies, causing bind(C, name=...) expressions
referencing such constants to fail.  The compiler treated the constant
as an implicitly typed REAL(4) variable instead of resolving it from
the enclosing module scope.

The fix sets has_import_set on the current namespace when a module
procedure is detected inside an interface block, before bind(C) is
parsed, so that symbol lookup can reach the host scope.

gcc/fortran/ChangeLog:

	PR fortran/79330
	* decl.cc (gfc_match_subroutine): Set has_import_set when
	matching a module procedure inside an interface block.
	(gfc_match_function_decl): Likewise.

gcc/testsuite/ChangeLog:

	PR fortran/79330
	* gfortran.dg/bind_c_module_proc.f90: New test.

Signed-off-by: Gonzalo Silvalde <[email protected]>
---
 gcc/fortran/decl.cc                            | 12 ++++++++++--
 .../gfortran.dg/bind_c_module_proc.f90         | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/bind_c_module_proc.f90

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 454b65f2c47..faf404d994a 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -8191,7 +8191,11 @@ gfc_match_function_decl (void)
     sym = sym->result;
 
   if (current_attr.module_procedure)
-    sym->attr.module_procedure = 1;
+    {
+      sym->attr.module_procedure = 1;
+      if (gfc_current_state () == COMP_INTERFACE)
+        gfc_current_ns->has_import_set = 1;
+    }
 
   gfc_new_block = sym;
 
@@ -8687,7 +8691,11 @@ gfc_match_subroutine (void)
 					     &gfc_current_locus);
 
   if (current_attr.module_procedure)
-    sym->attr.module_procedure = 1;
+    {
+      sym->attr.module_procedure = 1;
+      if (gfc_current_state () == COMP_INTERFACE)
+        gfc_current_ns->has_import_set = 1;
+    }
 
   if (add_hidden_procptr_result (sym))
     sym = sym->result;
diff --git a/gcc/testsuite/gfortran.dg/bind_c_module_proc.f90 b/gcc/testsuite/gfortran.dg/bind_c_module_proc.f90
new file mode 100644
index 00000000000..2df933f3c14
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bind_c_module_proc.f90
@@ -0,0 +1,18 @@
+! { dg-do compile }
+! PR fortran/79330
+! Verify that named constants from the host scope are accessible
+! in module procedure interface bodies for bind(C, name=...).
+
+module m
+  implicit none
+  character(len=*), parameter :: PREFIX = "_gfortran_"
+  interface
+    module subroutine sub() bind(C, name=PREFIX//"caf_sub")
+      implicit none
+    end subroutine
+    module function func() result(r) bind(C, name=PREFIX//"caf_func")
+      implicit none
+      integer :: r
+    end function
+  end interface
+end module
-- 
2.47.3
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.