Bug 57048 - [7/8 Regression] Handling of C_PTR and C_FUNPTR leads to reject valid
Summary: [7/8 Regression] Handling of C_PTR and C_FUNPTR leads to reject valid
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.9.0
: P4 normal
Target Milestone: 7.5
Assignee: Thomas Koenig
URL:
Keywords: rejects-valid
Depends on:
Blocks: ISO_C_Binding
  Show dependency treegraph
 
Reported: 2013-04-23 13:02 UTC by Tobias Burnus
Modified: 2019-02-02 16:58 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-11-23 00:00:00


Attachments
Bigger patch (doesn't work better :-/ ) (1.34 KB, patch)
2014-01-26 17:23 UTC, Mikael Morin
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2013-04-23 13:02:26 UTC
Reported by Angelo Graziosi at http://gcc.gnu.org/ml/fortran/2013-04/msg00210.html

Using in file1:
  module m
  ...
    type t
      type(c_funptr) :: funptr
    end type

and in file2:
   use iso_c_binding, ONLY: c_funloc
   use m
   type(t) :: x
   ...
   x%funptr = c_funloc(proc)

fails with:
  Error: Can't convert TYPE(c_funptr) to INTEGER(4) at (1)


The problem is that the .mod file only contains:
  win32_types.mod:5 'C_funptr' '__iso_c_binding' '' 1 ((DERIVED UNKNOWN-INTENT
while the symtree is searched for "c_funptr".

Workaround: Editing the .mod file and changing C_funptr to c_funptr.
Comment 1 Dominique d'Humieres 2013-11-23 13:33:06 UTC
On x86_64-apple-darwin13, I get with gfortran 4.8.2

[Book15] f90/bug% gfortran pr57048.f90
[Book15] f90/bug% grep funptr m.mod
(('c_funptr' '__iso_c_binding' 2) ('t' 'm' 3))
(2 'C_funptr' '__iso_c_binding' '' 1 ((DERIVED UNKNOWN-INTENT
DERIVED 2 0 1 1 UNKNOWN ()) 0 0 () () 0 ((4 '__c_funptr_c_address' (
(UNKNOWN 0 0 0 0 UNKNOWN ()) 0 0 () () 0 ((5 'funptr' (DERIVED 2 0 0 0
7 'c_funptr' '__iso_c_binding' '' 1 ((PROCEDURE UNKNOWN-INTENT
('C_funptr' 0 2 'T' 0 3 '__iso_c_binding' 0 6 'c_funptr' 0 7 'm' 0 8 't'

What is setting the capital C and why?
Comment 2 Mikael Morin 2014-01-26 17:21:11 UTC
This fixes this case;
However, it is not free of regressions.

diff --git a/gcc/fortran/trans-types.c b/gcc/fortran/trans-types.c
index adc34dd..24ceaea 100644
--- a/gcc/fortran/trans-types.c
+++ b/gcc/fortran/trans-types.c
@@ -2366,8 +2366,6 @@ gfc_get_derived_type (gfc_symbol * derived)
       else
        derived->backend_decl = pfunc_type_node;
 
-      derived->ts.kind = gfc_index_integer_kind;
-      derived->ts.type = BT_INTEGER;
       /* Set the f90_type to BT_VOID as a way to recognize something of type
          BT_INTEGER that needs to fit a void * for the purpose of the
          iso_c_binding derived types.  */
Comment 3 Mikael Morin 2014-01-26 17:23:45 UTC
Created attachment 31956 [details]
Bigger patch (doesn't work better :-/ )

And this is how far I got before giving up.
Comment 4 Jakub Jelinek 2014-04-22 11:36:55 UTC
GCC 4.9.0 has been released
Comment 5 Jakub Jelinek 2014-07-16 13:30:18 UTC
GCC 4.9.1 has been released.
Comment 6 Jakub Jelinek 2014-10-30 10:40:23 UTC
GCC 4.9.2 has been released.
Comment 7 Jakub Jelinek 2015-06-26 19:57:48 UTC
GCC 4.9.3 has been released.
Comment 8 Richard Biener 2016-08-03 11:00:09 UTC
GCC 4.9 branch is being closed
Comment 9 Jakub Jelinek 2018-10-26 10:20:19 UTC
GCC 6 branch is being closed
Comment 10 Thomas Koenig 2019-01-27 20:47:23 UTC
Here's something that appears to work.

Looks like a hack, swims like a hack, and quacks like a hack...

Index: interface.c
===================================================================
--- interface.c (Revision 268104)
+++ interface.c (Arbeitskopie)
@@ -692,6 +692,15 @@
   if (ts1->type == BT_VOID || ts2->type == BT_VOID)
     return true;
 
+  /* Special case for our C interop types.  There should be a better
+     way of doing this...  */
+
+  if (((ts1->type == BT_INTEGER && ts2->type == BT_DERIVED)
+       || (ts1->type == BT_DERIVED && ts2->type == BT_INTEGER))
+      && ts1->u.derived && ts2->u.derived
+      && ts1->u.derived == ts2->u.derived)
+    return true;
+
   /* The _data component is not always present, therefore check for its
      presence before assuming, that its derived->attr is available.
      When the _data component is not present, then nevertheless the
Comment 11 Thomas Koenig 2019-01-27 21:15:48 UTC
... and even passes regression-testing.

Well, let's see.
Comment 12 Thomas Koenig 2019-01-29 22:40:57 UTC
Author: tkoenig
Date: Tue Jan 29 22:40:26 2019
New Revision: 268372

URL: https://gcc.gnu.org/viewcvs?rev=268372&root=gcc&view=rev
Log:
2019-01-29  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/57048
	* interface.c (gfc_compare_types): If a derived type and an
	integer both have a derived type, and they are identical,
	this is a C binding type and compares equal.

2019-01-29  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/57048
	* gfortran.dg/c_funptr_1.f90: New file.
	* gfortran.dg/c_funptr_1_mod.f90: New file.


Added:
    trunk/gcc/testsuite/gfortran.dg/c_funptr_1.f90
    trunk/gcc/testsuite/gfortran.dg/c_funptr_1_mod.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/interface.c
    trunk/gcc/testsuite/ChangeLog
Comment 13 Thomas Koenig 2019-02-02 16:36:18 UTC
Author: tkoenig
Date: Sat Feb  2 16:35:47 2019
New Revision: 268476

URL: https://gcc.gnu.org/viewcvs?rev=268476&root=gcc&view=rev
Log:
2019-02-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/57048
	Backport from trunk
	* interface.c (gfc_compare_types): If a derived type and an
	integer both have a derived type, and they are identical,
	this is a C binding type and compares equal.

2019-02-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/57048
	Backport from trunk
	* gfortran.dg/c_funptr_1.f90: New file.
	* gfortran.dg/c_funptr_1_mod.f90: New file.


Added:
    branches/gcc-8-branch/gcc/testsuite/gfortran.dg/c_funptr_1.f90
    branches/gcc-8-branch/gcc/testsuite/gfortran.dg/c_funptr_1_mod.f90
Modified:
    branches/gcc-8-branch/gcc/fortran/ChangeLog
    branches/gcc-8-branch/gcc/fortran/interface.c
    branches/gcc-8-branch/gcc/testsuite/ChangeLog
Comment 14 Thomas Koenig 2019-02-02 16:58:11 UTC
Author: tkoenig
Date: Sat Feb  2 16:57:39 2019
New Revision: 268478

URL: https://gcc.gnu.org/viewcvs?rev=268478&root=gcc&view=rev
Log:
2019-02-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/57048
	Backport from trunk
	* interface.c (gfc_compare_types): If a derived type and an
	integer both have a derived type, and they are identical,
	this is a C binding type and compares equal.

2019-02-02  Thomas Koenig  <tkoenig@gcc.gnu.org>

	PR fortran/57048
	Backport from trunk
	* gfortran.dg/c_funptr_1.f90: New file.
	* gfortran.dg/c_funptr_1_mod.f90: New file.


Added:
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/c_funptr_1.f90
    branches/gcc-7-branch/gcc/testsuite/gfortran.dg/c_funptr_1_mod.f90
Modified:
    branches/gcc-7-branch/gcc/fortran/ChangeLog
    branches/gcc-7-branch/gcc/fortran/interface.c
    branches/gcc-7-branch/gcc/testsuite/ChangeLog
Comment 15 Thomas Koenig 2019-02-02 16:58:39 UTC
Fixed on all open branches, closing.