Bug 99982 - INTERFACE selects wrong module procedure involving C_PTR and C_FUNPTR
Summary: INTERFACE selects wrong module procedure involving C_PTR and C_FUNPTR
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 10.2.0
: P3 normal
Target Milestone: 13.0
Assignee: anlauf
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2021-04-09 03:02 UTC by Scot Breitenfeld
Modified: 2023-05-16 13:25 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2022-04-20 00:00:00


Attachments
program uses interface and module procedure to select between subroutines with type C_PTR and C_FUNPTR (392 bytes, text/plain)
2021-04-09 03:02 UTC, Scot Breitenfeld
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Scot Breitenfeld 2021-04-09 03:02:02 UTC
Created attachment 50532 [details]
program uses interface and module procedure to select between subroutines with type C_PTR and C_FUNPTR

The attached program always selects the TYPE(C_FUNPTR) when selecting between a (overloaded) subroutine with TYPE(C_PTR) and TYPE(C_FUNPTR), even when the variable type is TYPE(C_PTR).

It does this for both passing a variable as the argument or using the C_LOC or c_funloc directly in the call.

I tried it with 7.5.0, 10.2.0, same behavior.
Intel and NAG both select the correct subroutine.
Comment 1 Scot Breitenfeld 2021-04-21 04:53:09 UTC
I checked with gcc master (4/20/2021), and it still has the same issue.
Comment 2 Thierry Thomas 2022-04-20 16:24:40 UTC
I noticed the problem when building CGNS with gfortran / Gcc 10.2.0, and now with Gcc 10.3.2 the issue disappeared.
Comment 3 anlauf 2022-04-20 19:19:19 UTC
(In reply to Thierry Thomas from comment #2)
> I noticed the problem when building CGNS with gfortran / Gcc 10.2.0, and now
> with Gcc 10.3.2 the issue disappeared.

No.  The reported problem still exists.  Output for the attached testcase:

 cg_configure_funptr
 cg_configure_funptr
 cg_configure_funptr
 cg_configure_funptr

Expected:

 cg_configure_ptr
 cg_configure_funptr
 cg_configure_ptr
 cg_configure_funptr
Comment 4 Scot Breitenfeld 2023-03-22 16:41:42 UTC
Is there an update on this issue, the CGNS fortran wrappers will not work with gfortran as there are no work arounds for this issue.

Thanks.
Comment 5 Scot Breitenfeld 2023-03-22 17:08:18 UTC
I removed the c_funptr interface in CGNS since it was not being used.
Comment 7 GCC Commits 2023-04-12 09:14:16 UTC
The master branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:c482995cc5bac4a2168ea0049041e712544e474b

commit r13-7145-gc482995cc5bac4a2168ea0049041e712544e474b
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Apr 11 16:44:32 2023 +0200

    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.
Comment 8 anlauf 2023-04-12 09:21:59 UTC
Fixed on mainline for gcc-13.
Comment 9 Scot Breitenfeld 2023-04-12 13:56:09 UTC
This is Great! Thank-you.
Comment 10 GCC Commits 2023-04-23 18:53:29 UTC
The releases/gcc-12 branch has been updated by Harald Anlauf <anlauf@gcc.gnu.org>:

https://gcc.gnu.org/g:62a4f2fb356cab8cfebfeeac2895b657c32b8dd4

commit r12-9467-g62a4f2fb356cab8cfebfeeac2895b657c32b8dd4
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Tue Apr 11 16:44:32 2023 +0200

    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.
    
    (cherry picked from commit c482995cc5bac4a2168ea0049041e712544e474b)
Comment 11 Scot Breitenfeld 2023-05-16 13:25:53 UTC
Thanks for the standard reference. That is indeed what I was looking for. I understand now that, in this case, the INTENT refers to whether C_PTR can be changed and does not relate to the INTENT state of the target. I should change all my usage cases to INTENT(IN) since none of the Fortran wrappers allow changes to the pointer itself.