https://gcc.gnu.org/g:9e427f335b73112b0de19b116de97a6b7d563c45

commit r16-8399-g9e427f335b73112b0de19b116de97a6b7d563c45
Author: Gonzalosilvalde <[email protected]>
Date:   Sat Mar 28 12:22:42 2026 +0100

    fortran: Fix host association in module procedure interface bodies [PR79330]
    
    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]>

Diff:
---
 gcc/fortran/decl.cc                              | 12 ++++++++++--
 gcc/testsuite/gfortran.dg/bind_c_module_proc.f90 | 18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 454b65f2c47a..f585800d9c95 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 000000000000..2df933f3c14f
--- /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

Reply via email to