Dear all,

in the checking of imported interfaces we need to use the local names
of procedures that are renamed-on-use, as the original name becomes
inaccessible.  Similarly, we should not compare interfaces of
non-bind(C) procedures against bind(C) interfaces that are not
explicitly made accessible via a use statement, see testcase.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Could this one be backportable, e.g. to 14-branch?

Thanks,
Harald

From fb19a4bd29f49935514a7c2a43dbc9f2a6e9b147 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Sun, 26 Jan 2025 22:56:57 +0100
Subject: [PATCH] Fortran: fix bogus diagnostics on renamed interface import
 [PR110993]

	PR fortran/110993

gcc/fortran/ChangeLog:

	* frontend-passes.cc (check_externals_procedure): Do not compare
	interfaces of a non-bind(C) procedure against a bind(C) global one.
	(check_against_globals): Use local name from rename-on-use in the
	search for interfaces.

gcc/testsuite/ChangeLog:

	* gfortran.dg/use_rename_14.f90: New test.
---
 gcc/fortran/frontend-passes.cc              |  7 ++++
 gcc/testsuite/gfortran.dg/use_rename_14.f90 | 46 +++++++++++++++++++++
 2 files changed, 53 insertions(+)
 create mode 100644 gcc/testsuite/gfortran.dg/use_rename_14.f90

diff --git a/gcc/fortran/frontend-passes.cc b/gcc/fortran/frontend-passes.cc
index 987238794da..6b470b83e21 100644
--- a/gcc/fortran/frontend-passes.cc
+++ b/gcc/fortran/frontend-passes.cc
@@ -5704,6 +5704,9 @@ check_externals_procedure (gfc_symbol *sym, locus *loc,
   if (gsym->ns)
     gfc_find_symbol (sym->name, gsym->ns, 0, &def_sym);
 
+  if (gsym->bind_c && def_sym && def_sym->binding_label == NULL)
+    return 0;
+
   if (def_sym)
     {
       gfc_compare_actual_formal (&actual, def_sym->formal, 0, 0, 0, loc);
@@ -5800,6 +5803,10 @@ check_against_globals (gfc_symbol *sym)
 
   if (sym->binding_label)
     sym_name = sym->binding_label;
+  else if (sym->attr.use_rename
+	   && sym->ns->use_stmts->rename
+	   && sym->ns->use_stmts->rename->local_name[0] != '\0')
+    sym_name = sym->ns->use_stmts->rename->local_name;
   else
     sym_name = sym->name;
 
diff --git a/gcc/testsuite/gfortran.dg/use_rename_14.f90 b/gcc/testsuite/gfortran.dg/use_rename_14.f90
new file mode 100644
index 00000000000..03815a5f229
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/use_rename_14.f90
@@ -0,0 +1,46 @@
+! { dg-do compile }
+!
+! PR fortran/110993 - bogus diagnostics on renamed interface import
+!
+! Contributed by Rimvydas Jasinskas <rimvydas.jas at gmail.com>
+
+module m
+  interface
+    subroutine bar(x)
+      use iso_c_binding, only : c_float
+      implicit none
+      real(c_float) :: x(45)
+    end subroutine
+  end interface
+end
+
+module m1
+  interface
+    subroutine bar1(x) bind(c)
+      use iso_c_binding, only : c_float
+      implicit none
+      real(c_float) :: x(45)
+    end subroutine
+  end interface
+end
+
+module m2
+  interface
+    subroutine bar2(x) bind(c, name="bar2_")
+      use iso_c_binding, only : c_float
+      implicit none
+      real(c_float) :: x(45)
+    end subroutine
+  end interface
+end
+
+subroutine foo(y)
+  use m,  notthisone => bar
+  use m1, northisone => bar1
+  use m2,  orthisone => bar2
+  implicit none
+  real :: y(3)
+  call bar (y)
+  call bar1(y)
+  call bar2(y)
+end subroutine
-- 
2.43.0

Reply via email to