* [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615] @ 2023-04-10 20:49 Harald Anlauf 2023-04-11 0:43 ` Jerry D 0 siblings, 1 reply; 4+ messages in thread From: Harald Anlauf @ 2023-04-10 20:49 UTC (permalink / raw) To: fortran, gcc-patches [-- Attachment #1: Type: text/plain, Size: 397 bytes --] 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 [-- Warning: decoded text below may be mangled, UTF-8 assumed --] [-- Attachment #2: pr61615.diff --] [-- Type: text/x-patch, Size: 2946 bytes --] 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 diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc index db79b104dc2..8682dc999be 100644 --- a/gcc/fortran/interface.cc +++ b/gcc/fortran/interface.cc @@ -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 diff --git a/gcc/testsuite/gfortran.dg/interface_49.f90 b/gcc/testsuite/gfortran.dg/interface_49.f90 new file mode 100644 index 00000000000..67d3e3f871b --- /dev/null +++ b/gcc/testsuite/gfortran.dg/interface_49.f90 @@ -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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615] 2023-04-10 20:49 [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615] Harald Anlauf @ 2023-04-11 0:43 ` Jerry D 2023-04-11 14:54 ` [PATCH, v2] " Harald Anlauf 0 siblings, 1 reply; 4+ messages in thread From: Jerry D @ 2023-04-11 0:43 UTC (permalink / raw) To: Harald Anlauf, fortran, gcc-patches 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 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH, v2] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615] 2023-04-11 0:43 ` Jerry D @ 2023-04-11 14:54 ` Harald Anlauf 2023-04-11 14:54 ` Harald Anlauf 0 siblings, 1 reply; 4+ messages in thread From: Harald Anlauf @ 2023-04-11 14:54 UTC (permalink / raw) To: Jerry D, fortran, gcc-patches [-- Attachment #1: Type: text/plain, Size: 1004 bytes --] Hi Jerry, all, On 4/11/23 02:43, Jerry D via Gcc-patches wrote: > 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 I actually found a flaw in the previous patch regarding the handling of rank, and also realized that a comparison of the types was missing for those from this intrinsic module (and found the related PR99982). I updated the patch accordingly and extended the testcase, see attached. Regtests cleanly on x86_64-pc-linux-gnu. Will wait for 24h for more comments. Thanks, Harald [-- Attachment #2: pr61615-v2.diff --] [-- Type: text/x-patch, Size: 4354 bytes --] From 3fa9d2ee99afa38f42c267d17aed5266daa22dbc Mon Sep 17 00:00:00 2001 From: Harald Anlauf <anlauf@gmx.de> Date: Tue, 11 Apr 2023 16:44:32 +0200 Subject: [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615,PR99982] gcc/fortran/ChangeLog: PR fortran/61615 PR fortran/99982 * interface.cc (compare_parameter): Enable type and rank checks for arguments of derived type from the intrinsic module ISO_C_BINDING. gcc/testsuite/ChangeLog: PR fortran/61615 PR fortran/99982 * gfortran.dg/interface_49.f90: New test. --- gcc/fortran/interface.cc | 18 +++- gcc/testsuite/gfortran.dg/interface_49.f90 | 95 ++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/interface_49.f90 diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc index db79b104dc2..e9843e9549c 100644 --- a/gcc/fortran/interface.cc +++ b/gcc/fortran/interface.cc @@ -2361,7 +2361,23 @@ 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 (formal->ts.u.derived->intmod_sym_id + != actual->ts.u.derived->intmod_sym_id) + return false; + + if (ranks_must_agree + && symbol_rank (formal) != actual->rank + && symbol_rank (formal) != -1) + { + 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 diff --git a/gcc/testsuite/gfortran.dg/interface_49.f90 b/gcc/testsuite/gfortran.dg/interface_49.f90 new file mode 100644 index 00000000000..aef5e0c6609 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/interface_49.f90 @@ -0,0 +1,95 @@ +! { dg-do run } +! PR fortran/61615 - resolve correct generic with TYPE(C_PTR) arguments +! PR fortran/99982 - dto. with C_PTR and C_FUNPTR +! Contributed by Jacob Abel and Scot Breitenfeld + +MODULE foo + USE iso_c_binding, only : c_ptr, c_funptr + IMPLICIT NONE + integer :: rank = -99 + character(8) :: ctyp = "" + INTERFACE bar + MODULE PROCEDURE bar_s + MODULE PROCEDURE bar_a1d + MODULE PROCEDURE bar_a2d + MODULE PROCEDURE bar_fp + MODULE PROCEDURE bar_fp1 + MODULE PROCEDURE bar_fpx + END INTERFACE bar +CONTAINS + SUBROUTINE bar_s(a) + TYPE(c_ptr) :: a + WRITE (0, *) 'in bar_s' + rank = 0 + ctyp = "c_ptr" + END SUBROUTINE bar_s + + SUBROUTINE bar_a1d(a) + TYPE(c_ptr) :: a(:) + WRITE (0, *) 'in bar_a1d' + rank = 1 + ctyp = "c_ptr" + END SUBROUTINE bar_a1d + + SUBROUTINE bar_a2d(a) + TYPE(c_ptr) :: a(:,:) + WRITE (0, *) 'in bar_a2d' + rank = 2 + ctyp = "c_ptr" + END SUBROUTINE bar_a2d + + SUBROUTINE bar_fp(a) + TYPE(c_funptr) :: a + WRITE (0, *) 'in bar_fp' + rank = 0 + ctyp = "c_funptr" + END SUBROUTINE bar_fp + + SUBROUTINE bar_fp1(a) + TYPE(c_funptr) :: a(:) + WRITE (0, *) 'in bar_fp1' + rank = 1 + ctyp = "c_funptr" + END SUBROUTINE bar_fp1 + + SUBROUTINE bar_fpx(a, b) + TYPE(c_funptr) :: a(..) + TYPE(c_ptr) :: b + WRITE (0, *) 'in bar_fpx' + rank = -1 + ctyp = "c_funptr" + END SUBROUTINE bar_fpx +END MODULE foo + +PROGRAM cptr_array_vs_scalar_arg + USE foo + USE iso_c_binding, only : c_ptr, c_loc, c_funptr + IMPLICIT NONE + INTEGER, TARGET :: i + TYPE(c_ptr) :: a, b(1), c(1,1) + type(c_funptr) :: fp, fp1(1), fp2(1,1) + a = C_LOC(i) + b(1) = C_LOC(i) + CALL bar(a) + if (rank /= 0 .or. ctyp /= "c_ptr") stop 1 + CALL bar(b) + if (rank /= 1 .or. ctyp /= "c_ptr") stop 2 + CALL bar(c) + if (rank /= 2 .or. ctyp /= "c_ptr") stop 3 + rank = -99 + ctyp = "" + CALL bar((a)) + if (rank /= 0 .or. ctyp /= "c_ptr") stop 4 + CALL bar((b)) + if (rank /= 1 .or. ctyp /= "c_ptr") stop 5 + rank = -99 + ctyp = "" + CALL bar(fp) + if (rank /= 0 .or. ctyp /= "c_funptr") stop 6 + CALL bar(fp1) + if (rank /= 1 .or. ctyp /= "c_funptr") stop 7 + rank = -99 + ctyp = "" + CALL bar(fp2, a) + if (rank /= -1 .or. ctyp /= "c_funptr") stop 8 +END PROGRAM cptr_array_vs_scalar_arg -- 2.35.3 ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH, v2] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615] 2023-04-11 14:54 ` [PATCH, v2] " Harald Anlauf @ 2023-04-11 14:54 ` Harald Anlauf 0 siblings, 0 replies; 4+ messages in thread From: Harald Anlauf @ 2023-04-11 14:54 UTC (permalink / raw) To: gcc-patches; +Cc: fortran [-- Attachment #1: Type: text/plain, Size: 970 bytes --] Hi Jerry, all, On 4/11/23 02:43, Jerry D via Gcc-patches wrote: > 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 I actually found a flaw in the previous patch regarding the handling of rank, and also realized that a comparison of the types was missing for those from this intrinsic module (and found the related PR99982). I updated the patch accordingly and extended the testcase, see attached. Regtests cleanly on x86_64-pc-linux-gnu. Will wait for 24h for more comments. Thanks, Harald [-- Attachment #2: pr61615-v2.diff --] [-- Type: text/x-patch, Size: 4354 bytes --] From 3fa9d2ee99afa38f42c267d17aed5266daa22dbc Mon Sep 17 00:00:00 2001 From: Harald Anlauf <anlauf@gmx.de> Date: Tue, 11 Apr 2023 16:44:32 +0200 Subject: [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615,PR99982] gcc/fortran/ChangeLog: PR fortran/61615 PR fortran/99982 * interface.cc (compare_parameter): Enable type and rank checks for arguments of derived type from the intrinsic module ISO_C_BINDING. gcc/testsuite/ChangeLog: PR fortran/61615 PR fortran/99982 * gfortran.dg/interface_49.f90: New test. --- gcc/fortran/interface.cc | 18 +++- gcc/testsuite/gfortran.dg/interface_49.f90 | 95 ++++++++++++++++++++++ 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/interface_49.f90 diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc index db79b104dc2..e9843e9549c 100644 --- a/gcc/fortran/interface.cc +++ b/gcc/fortran/interface.cc @@ -2361,7 +2361,23 @@ 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 (formal->ts.u.derived->intmod_sym_id + != actual->ts.u.derived->intmod_sym_id) + return false; + + if (ranks_must_agree + && symbol_rank (formal) != actual->rank + && symbol_rank (formal) != -1) + { + 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 diff --git a/gcc/testsuite/gfortran.dg/interface_49.f90 b/gcc/testsuite/gfortran.dg/interface_49.f90 new file mode 100644 index 00000000000..aef5e0c6609 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/interface_49.f90 @@ -0,0 +1,95 @@ +! { dg-do run } +! PR fortran/61615 - resolve correct generic with TYPE(C_PTR) arguments +! PR fortran/99982 - dto. with C_PTR and C_FUNPTR +! Contributed by Jacob Abel and Scot Breitenfeld + +MODULE foo + USE iso_c_binding, only : c_ptr, c_funptr + IMPLICIT NONE + integer :: rank = -99 + character(8) :: ctyp = "" + INTERFACE bar + MODULE PROCEDURE bar_s + MODULE PROCEDURE bar_a1d + MODULE PROCEDURE bar_a2d + MODULE PROCEDURE bar_fp + MODULE PROCEDURE bar_fp1 + MODULE PROCEDURE bar_fpx + END INTERFACE bar +CONTAINS + SUBROUTINE bar_s(a) + TYPE(c_ptr) :: a + WRITE (0, *) 'in bar_s' + rank = 0 + ctyp = "c_ptr" + END SUBROUTINE bar_s + + SUBROUTINE bar_a1d(a) + TYPE(c_ptr) :: a(:) + WRITE (0, *) 'in bar_a1d' + rank = 1 + ctyp = "c_ptr" + END SUBROUTINE bar_a1d + + SUBROUTINE bar_a2d(a) + TYPE(c_ptr) :: a(:,:) + WRITE (0, *) 'in bar_a2d' + rank = 2 + ctyp = "c_ptr" + END SUBROUTINE bar_a2d + + SUBROUTINE bar_fp(a) + TYPE(c_funptr) :: a + WRITE (0, *) 'in bar_fp' + rank = 0 + ctyp = "c_funptr" + END SUBROUTINE bar_fp + + SUBROUTINE bar_fp1(a) + TYPE(c_funptr) :: a(:) + WRITE (0, *) 'in bar_fp1' + rank = 1 + ctyp = "c_funptr" + END SUBROUTINE bar_fp1 + + SUBROUTINE bar_fpx(a, b) + TYPE(c_funptr) :: a(..) + TYPE(c_ptr) :: b + WRITE (0, *) 'in bar_fpx' + rank = -1 + ctyp = "c_funptr" + END SUBROUTINE bar_fpx +END MODULE foo + +PROGRAM cptr_array_vs_scalar_arg + USE foo + USE iso_c_binding, only : c_ptr, c_loc, c_funptr + IMPLICIT NONE + INTEGER, TARGET :: i + TYPE(c_ptr) :: a, b(1), c(1,1) + type(c_funptr) :: fp, fp1(1), fp2(1,1) + a = C_LOC(i) + b(1) = C_LOC(i) + CALL bar(a) + if (rank /= 0 .or. ctyp /= "c_ptr") stop 1 + CALL bar(b) + if (rank /= 1 .or. ctyp /= "c_ptr") stop 2 + CALL bar(c) + if (rank /= 2 .or. ctyp /= "c_ptr") stop 3 + rank = -99 + ctyp = "" + CALL bar((a)) + if (rank /= 0 .or. ctyp /= "c_ptr") stop 4 + CALL bar((b)) + if (rank /= 1 .or. ctyp /= "c_ptr") stop 5 + rank = -99 + ctyp = "" + CALL bar(fp) + if (rank /= 0 .or. ctyp /= "c_funptr") stop 6 + CALL bar(fp1) + if (rank /= 1 .or. ctyp /= "c_funptr") stop 7 + rank = -99 + ctyp = "" + CALL bar(fp2, a) + if (rank /= -1 .or. ctyp /= "c_funptr") stop 8 +END PROGRAM cptr_array_vs_scalar_arg -- 2.35.3 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2023-04-11 14:54 UTC | newest] Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2023-04-10 20:49 [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments [PR61615] Harald Anlauf 2023-04-11 0:43 ` Jerry D 2023-04-11 14:54 ` [PATCH, v2] " Harald Anlauf 2023-04-11 14:54 ` Harald Anlauf
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).