Bug 47281

Summary: [4.6 Regression] error: non-trivial conversion at assignment
Product: gcc Reporter: Dominique d'Humieres <dominiq>
Component: middle-endAssignee: Richard Biener <rguenth>
Status: RESOLVED FIXED    
Severity: normal CC: burnus, janus, rguenther, zsojka
Priority: P3    
Version: 4.6.0   
Target Milestone: 4.6.0   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2011-01-14 12:10:10
Bug Depends on:    
Bug Blocks: 46076    

Description Dominique d'Humieres 2011-01-13 18:39:30 UTC
Revision 168665 caused/exposed the following ICE at -O2 or above:

[macbook] f90/bug% gfc6 -O2 pr35971_red.f90
pr35971_red.f90: In function 'gp':
pr35971_red.f90:67:0: error: non-trivial conversion at assignment
void (*<T64>) (void)
void (*<T49d>) (struct array1_unknown &, integer(kind=4), struct array1_integer(kind=4) & restrict)
__result_gp_72 = make_mess;

pr35971_red.f90:67:0: internal compiler error: verify_stmts failed

with the following (probably invalid?) code

[macbook] f90/bug% cat pr35971_red.f90
module other_fun
   use ISO_C_BINDING
   implicit none
   private
! Message to be returned by procedure pointed to
! by the C_FUNPTR
   character, allocatable, save :: my_message(:)
! Interface block for the procedure pointed to
! by the C_FUNPTR
   public abstract_fun
   abstract interface
      function abstract_fun(x)
         use ISO_C_BINDING
         import my_message
         implicit none
         integer(C_INT) x(:)
         character(size(my_message),C_CHAR) abstract_fun(size(x))
      end function abstract_fun
   end interface
   contains
! Procedure to store the message and get the C_FUNPTR
      function gp(message) bind(C,name='BlAh')
!         procedure(abstract_fun) make_mess
         character(kind=C_CHAR) message(*)
         type(C_FUNPTR) gp
         integer(C_INT64_T) i

         i = 1
         do while(message(i) /= C_NULL_CHAR)
            i = i+1
         end do
         my_message = message(int(1,kind(i)):i-1)
         gp = get_funloc(make_mess,aux)
!         gp = aux(make_mess)
      end function gp

! Intermediate procedure to pass the function and get
! back the C_FUNPTR
      function get_funloc(x,y)
         procedure(abstract_fun) x
         type(C_FUNPTR) y
         external y
         type(C_FUNPTR) get_funloc

         get_funloc = y(x)
      end function get_funloc

! Procedure to convert the function to C_FUNPTR
      function aux(x)
         interface
            subroutine x() bind(C)
            end subroutine x
         end interface
         type(C_FUNPTR) aux

         aux = C_FUNLOC(x)
      end function aux

! Procedure pointed to by the C_FUNPTR
      function make_mess(x)
         integer(C_INT) x(:)
         character(size(my_message),C_CHAR) make_mess(size(x))

         make_mess = transfer(my_message,make_mess(1))
      end function make_mess
end module other_fun
end
Comment 1 Richard Biener 2011-01-14 12:10:10 UTC
Simplified C testcase:

struct T;
typedef void F(void);

F* aux(void (*x)())
{
  return x;
}

void make_mess (int);

F*
get_funloc (void (*x)(int), F* (*y)())
{
  return y(x);
}

F*
foo ()
{
  return get_funloc (make_mess, aux);
}
Comment 2 Richard Biener 2011-01-14 12:26:47 UTC
It's the transitivity problem in

  (void (*)(void))(void (*)())(void (*)(int))make_mess

The first conversion is useless as is the second.  But

  (void (*)(void))(void (*)(int))make_mess

is not useless.  That's of course a bug in the type system as implemented
as we rely on

     1) useless_type_conversion_p is transitive.
        If a < b and b < c then a < c.

So we either have to revert the fix for PR46076 or need to make
conversions to unprototyped function pointers not generally useless.

I'm going for reverting the fix for 4.6.
Comment 3 Richard Biener 2011-01-14 12:39:14 UTC
Author: rguenth
Date: Fri Jan 14 12:39:09 2011
New Revision: 168781

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=168781
Log:
2011-01-14  Richard Guenther  <rguenther@suse.de>

	PR middle-end/47281
	Revert
	2011-01-11  Richard Guenther  <rguenther@suse.de>

        PR tree-optimization/46076
        * tree-ssa.c (useless_type_conversion_p): Conversions from
        unprototyped to empty argument list function types are useless.

	* gcc.dg/torture/pr47281.c: New testcase.
	* gcc.dg/tree-ssa/pr46076.c: XFAIL.

Added:
    trunk/gcc/testsuite/gcc.dg/torture/pr47281.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gcc.dg/tree-ssa/pr46076.c
    trunk/gcc/tree-ssa.c
Comment 4 Richard Biener 2011-01-14 12:39:34 UTC
Fixed.