Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615]
Checks
Commit Message
Dear all,
when comparing formal and actual arguments of a procedure, there was no
check of rank for derived types from intrinsic module ISO_C_BINDING.
This could lead to a wrong resolution of generic procedures with dummy
argument of related types, see PR. This was likely an oversight.
The attached fix is simple and regtests cleanly on x86_64-pc-linux-gnu.
OK for mainline?
Thanks,
Harald
Comments
On 4/10/23 1:49 PM, Harald Anlauf via Fortran wrote:
> Dear all,
>
> when comparing formal and actual arguments of a procedure, there was no
> check of rank for derived types from intrinsic module ISO_C_BINDING.
> This could lead to a wrong resolution of generic procedures with dummy
> argument of related types, see PR. This was likely an oversight.
>
> The attached fix is simple and regtests cleanly on x86_64-pc-linux-gnu.
>
> OK for mainline?
>
> Thanks,
> Harald
>
Looks good to go.
Jerry
From d41aa0f60b53799a5d28743f168fbf312461f51f Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 10 Apr 2023 22:39:52 +0200
Subject: [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments
[PR61615]
gcc/fortran/ChangeLog:
PR fortran/61615
* interface.cc (compare_parameter): Enable rank check for arguments
of derived type from the intrinsic module ISO_C_BINDING.
gcc/testsuite/ChangeLog:
PR fortran/61615
* gfortran.dg/interface_49.f90: New test.
---
gcc/fortran/interface.cc | 14 ++++++-
gcc/testsuite/gfortran.dg/interface_49.f90 | 43 ++++++++++++++++++++++
2 files changed, 56 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gfortran.dg/interface_49.f90
@@ -2361,7 +2361,19 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
&& formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
&& actual->ts.type == BT_DERIVED
&& actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
- return true;
+ {
+ if (ranks_must_agree
+ && ((actual->rank == 0 && formal->attr.dimension)
+ || (actual->rank != 0 && !formal->attr.dimension)))
+ {
+ if (where)
+ argument_rank_mismatch (formal->name, &actual->where,
+ symbol_rank (formal), actual->rank,
+ NULL);
+ return false;
+ }
+ return true;
+ }
if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
/* Make sure the vtab symbol is present when
new file mode 100644
@@ -0,0 +1,43 @@
+! { dg-do run }
+! PR fortran/61615 - resolve correct generic with TYPE(C_PTR) arguments
+! Contributed by Jacob Abel
+
+MODULE foo
+ USE iso_c_binding, only : c_ptr
+ IMPLICIT NONE
+ integer :: rank = -99
+ INTERFACE bar
+ MODULE PROCEDURE bar_s
+ MODULE PROCEDURE bar_a1d
+ END INTERFACE bar
+CONTAINS
+ SUBROUTINE bar_s(a)
+ TYPE(c_ptr) :: a
+ WRITE (0, *) 'in bar_s'
+ rank = 0
+ END SUBROUTINE bar_s
+
+ SUBROUTINE bar_a1d(a)
+ TYPE(c_ptr) :: a(:)
+ WRITE (0, *) 'in bar_a1d'
+ rank = 1
+ END SUBROUTINE bar_a1d
+END MODULE foo
+
+PROGRAM cptr_array_vs_scalar_arg
+ USE foo
+ USE iso_c_binding, only : c_ptr, c_loc
+ IMPLICIT NONE
+ INTEGER, TARGET :: i
+ TYPE(c_ptr) :: a, b(1)
+ a = C_LOC(i)
+ b(1) = C_LOC(i)
+ CALL bar(a)
+ if (rank /= 0) stop 1
+ CALL bar(b)
+ if (rank /= 1) stop 2
+ CALL bar((a))
+ if (rank /= 0) stop 3
+ CALL bar((b))
+ if (rank /= 1) stop 4
+END PROGRAM cptr_array_vs_scalar_arg
--
2.35.3