Bug 56814 - [4.8/4.9 Regression] Bogus Interface mismatch in dummy procedure
Summary: [4.8/4.9 Regression] Bogus Interface mismatch in dummy procedure
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 4.8.1
Assignee: janus
URL:
Keywords: rejects-valid
: 56823 (view as bug list)
Depends on:
Blocks:
 
Reported: 2013-04-02 15:52 UTC by Tobias Burnus
Modified: 2013-04-26 19:25 UTC (History)
3 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-04-03 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2013-04-02 15:52:52 UTC
The following code compiles with ifort and gfortran 4.5 to 4.7. With 4.8 and 4.9 it fails with:

Error: Interface mismatch in dummy procedure 'fun' at (1): PROCEDURE POINTER mismatch in function result

However, to me the interfaces look the same.


Reported at comp.lang.fortran by Macro Restelli, https://groups.google.com/forum/?fromgroups=#!topic/comp.lang.fortran/rfoD-brnIqI


module m1
 abstract interface
  pure function i_f(x) result(d)
   real, intent(in) :: x(:,:)
   real :: d(size(x,1),size(x,2))
  end function i_f
 end interface

 procedure(i_f), pointer :: f => null()
end module m1

module m2
contains
 pure subroutine ns_dirdata(fun)
  interface
   pure function fun(x) result(d)
    real, intent(in) :: x(:,:)
    real :: d(size(x,1),size(x,2))
   end function fun
  end interface
 end subroutine ns_dirdata
end module m2

program p
 use m1
 use m2
  call ns_dirdata(f)
end program p
Comment 1 Tobias Burnus 2013-04-02 17:25:18 UTC
The check is in interface.c's check_result_characteristics:

  /* Check PROCEDURE POINTER attribute.  */
  if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
    {
      snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
                "function result");


There are two things bogus here:
a) Passing a proc-pointer to a non-pointer procedure dummy is valid
b) There is no procedure(-pointer) function result, just a proc(-ptr) actual/dummy argument.
Comment 2 Tobias Burnus 2013-04-03 08:22:05 UTC
*** Bug 56823 has been marked as a duplicate of this bug. ***
Comment 3 janus 2013-04-03 08:25:35 UTC
(In reply to comment #1)
> The check is in interface.c's check_result_characteristics:
> 
>   /* Check PROCEDURE POINTER attribute.  */
>   if (r1 != s1 && r1->attr.proc_pointer != r2->attr.proc_pointer)
>     {
>       snprintf (errmsg, err_len, "PROCEDURE POINTER mismatch in "
>                 "function result");
> 
> There are two things bogus here:
> a) Passing a proc-pointer to a non-pointer procedure dummy is valid
> b) There is no procedure(-pointer) function result, just a proc(-ptr)
> actual/dummy argument.

Right, but actually I think the above check should handle both of these points correctly.

The problem rather seems to be that the symbol for 'f' does not have its result field set up properly: It points to 'f' itself instead of 'd' (from the interface).


The trivial fix is:

Index: gcc/fortran/resolve.c
===================================================================
--- gcc/fortran/resolve.c	(revision 197388)
+++ gcc/fortran/resolve.c	(working copy)
@@ -216,7 +216,7 @@ resolve_procedure_interface (gfc_symbol *sym)
       if (ifc->result)
 	{
 	  sym->ts = ifc->result->ts;
-	  sym->result = sym;
+	  sym->result = ifc->result;
 	}
       else
 	sym->ts = ifc->ts;


No idea how that got messed up. Will commit as obvious after regtesting.
Comment 4 janus 2013-04-03 09:41:12 UTC
Unfortunately this patch produces one testsuite failure:


> gfortran-4.9 recursive_interface_1.f90 
recursive_interface_1.f90:16:0: internal compiler error: in gfc_typenode_for_spec, at fortran/trans-types.c:1063
   function baz() result(r3)
 ^
0x6b6aca gfc_typenode_for_spec(gfc_typespec*)
        /home/janus/gcc49/trunk/gcc/fortran/trans-types.c:1063
0x6b9ca5 gfc_sym_type(gfc_symbol*)
        /home/janus/gcc49/trunk/gcc/fortran/trans-types.c:2129
0x6bbe95 gfc_get_function_type(gfc_symbol*)
        /home/janus/gcc49/trunk/gcc/fortran/trans-types.c:2878
0x6bbe4d gfc_get_function_type(gfc_symbol*)
        /home/janus/gcc49/trunk/gcc/fortran/trans-types.c:2871
0x6bbe4d gfc_get_function_type(gfc_symbol*)
        /home/janus/gcc49/trunk/gcc/fortran/trans-types.c:2871
0x658504 build_function_decl
        /home/janus/gcc49/trunk/gcc/fortran/trans-decl.c:1858
0x65ac7b gfc_create_function_decl(gfc_namespace*, bool)
        /home/janus/gcc49/trunk/gcc/fortran/trans-decl.c:2513


Will investigate ...
Comment 5 janus 2013-04-17 08:58:25 UTC
Alternative patch:

Index: gcc/fortran/interface.c
===================================================================
--- gcc/fortran/interface.c	(revision 198007)
+++ gcc/fortran/interface.c	(working copy)
@@ -1184,9 +1184,20 @@ check_result_characteristics (gfc_symbol *s1, gfc_
 {
   gfc_symbol *r1, *r2;
 
-  r1 = s1->result ? s1->result : s1;
-  r2 = s2->result ? s2->result : s2;
+  if (s1->ts.interface && s1->ts.interface->result)
+    r1 = s1->ts.interface->result;
+  else if (s1->result)
+    r1 = s1->result;
+  else
+    r1 = s1;
 
+  if (s2->ts.interface && s2->ts.interface->result)
+    r2 = s2->ts.interface->result;
+  else if (s2->result)
+    r2 = s2->result;
+  else
+    r2 = s2;
+
   if (r1->ts.type == BT_UNKNOWN)
     return true;
 

Regtesting now ...
Comment 6 janus 2013-04-17 11:41:21 UTC
(In reply to comment #5)
> Alternative patch:

In contrast to the patch in comment #3, this one regtests cleanly ...
Comment 7 janus 2013-04-17 16:15:06 UTC
Fixed on trunk with:


Author: janus
Date: Wed Apr 17 16:13:07 2013
New Revision: 198032

URL: http://gcc.gnu.org/viewcvs?rev=198032&root=gcc&view=rev
Log:
2013-04-17  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/56814
	* interface.c (check_result_characteristics): Get result from interface
	if present.


2013-04-17  Janus Weil  <janus@gcc.gnu.org>

	PR fortran/56814
	* gfortran.dg/proc_ptr_42.f90: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_42.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/interface.c
    trunk/gcc/testsuite/ChangeLog



Will backport to 4.8 soon.
Comment 8 janus 2013-04-26 19:25:06 UTC
Fixed on 4.8 branch with:


Author: janus
Date: Fri Apr 26 19:20:55 2013
New Revision: 198345

URL: http://gcc.gnu.org/viewcvs?rev=198345&root=gcc&view=rev
Log:
2013-04-26  Janus Weil  <janus@gcc.gnu.org>

	Backports from trunk:

	PR fortran/56814
	* interface.c (check_result_characteristics): Get result from interface
	if present.

	PR fortran/56968
	* expr.c (gfc_check_pointer_assign): Handle generic functions returning
	procedure pointers.

	PR fortran/53685
	PR fortran/57022
	* check.c (gfc_calculate_transfer_sizes): Fix for array-valued SOURCE
	expressions.
	* target-memory.h (gfc_element_size): New prototype.
	* target-memory.c (size_array): Remove.
	(gfc_element_size): New function.
	(gfc_target_expr_size): Modified to always return the full size of the
	expression.


2013-04-26  Janus Weil  <janus@gcc.gnu.org>

	Backports from trunk:

	PR fortran/56968
	* gfortran.dg/proc_ptr_41.f90: New.

	PR fortran/56814
	* gfortran.dg/proc_ptr_42.f90: New.

	PR fortran/53685
	PR fortran/57022
	* gfortran.dg/transfer_check_4.f90: New.

Added:
    branches/gcc-4_8-branch/gcc/testsuite/gfortran.dg/proc_ptr_41.f90
    branches/gcc-4_8-branch/gcc/testsuite/gfortran.dg/proc_ptr_42.f90
    branches/gcc-4_8-branch/gcc/testsuite/gfortran.dg/transfer_check_4.f90
Modified:
    branches/gcc-4_8-branch/gcc/fortran/ChangeLog
    branches/gcc-4_8-branch/gcc/fortran/check.c
    branches/gcc-4_8-branch/gcc/fortran/expr.c
    branches/gcc-4_8-branch/gcc/fortran/interface.c
    branches/gcc-4_8-branch/gcc/fortran/target-memory.c
    branches/gcc-4_8-branch/gcc/fortran/target-memory.h
    branches/gcc-4_8-branch/gcc/testsuite/ChangeLog


Closing ...