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

Gonzalo Silvalde Blanco <[email protected]>
Newsgroups gmane.comp.gcc.patches,gmane.comp.gcc.fortran
Message-ID <CANS+kgdpocT-_P7oyEq7DSZGWDKaMEHO0qXas+otnRB+X_cYTQ@mail.gmail.com>
Sorry, resending with the patch as attachment. I thought that git
send-email attached the file.

El mar, 31 mar 2026 a las 21:59, Harald Anlauf (<[email protected]>) escribió:
>
> Hi Gonzalo,
>
> the patch looks basically fine, but:
>
> 1) please sign it off, like the previous time;
>
> 2) please attach it to your mail, don't inline it.
>
> Thanks,
> Harald
>
> On 3/28/26 12:35, Gonzalosilvalde wrote:
> > 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.
> > ---
> >   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
>
v2-0001-fortran-Fix-host-association-in-module-procedure-.patch (text/x-patch, 2.9 KB)
From 9bed8a3f23274e3aaa45351c4a95695e62dc9fba Mon Sep 17 00:00:00 2001
From: Gonzalosilvalde <[email protected]>
Date: Sat, 28 Mar 2026 12:22:42 +0100
Subject: [PATCH v2] 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]>
---
 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.