https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59997
--- Comment #8 from Dominique d'Humieres <dominiq at lps dot ens.fr> --- I am confused by the comments 5 to 7, probably because my comment 4 was confusing. What I have tested is 4.8.5 with the patch for fortran/trans-expr.c plus the additional tests diff -upN ../4.8_clean/gcc/testsuite/gfortran.dg/c_loc_test_20.f90 gcc/testsuite/gfortran.dg/c_loc_test_20.f90 --- ../4.8_clean/gcc/testsuite/gfortran.dg/c_loc_test_20.f90 1970-01-01 01:00:00.000000000 +0100 +++ gcc/testsuite/gfortran.dg/c_loc_test_20.f90 2013-03-25 18:07:07.000000000 +0100 @@ -0,0 +1,34 @@ +! { dg-do run } +! +! PR fortran/38829 +! PR fortran/40963 +! PR fortran/38813 +! +! +program testcloc + use, intrinsic :: iso_c_binding + implicit none + + type obj + real :: array(10,10) + real, allocatable :: array2(:,:) + end type + + type(obj), target :: obj1 + type(c_ptr) :: cptr + integer :: i + real, pointer :: array(:) + + allocate (obj1%array2(10,10)) + obj1%array = reshape ([(i, i=1,100)], shape (obj1%array)) + obj1%array2 = reshape ([(i, i=1,100)], shape (obj1%array)) + + cptr = c_loc (obj1%array) + call c_f_pointer (cptr, array, shape=[100]) + if (any (array /= [(i, i=1,100)])) call abort () + + cptr = c_loc (obj1%array2) + call c_f_pointer (cptr, array, shape=[100]) + if (any (array /= [(i, i=1,100)])) call abort () +end program testcloc + diff -upN ../4.8_clean/gcc/testsuite/gfortran.dg/pr59997.f90 gcc/testsuite/gfortran.dg/pr59997.f90 --- ../4.8_clean/gcc/testsuite/gfortran.dg/pr59997.f90 1970-01-01 01:00:00.000000000 +0100 +++ gcc/testsuite/gfortran.dg/pr59997.f90 2015-04-11 11:00:43.000000000 +0200 @@ -0,0 +1,29 @@ +! { dg-do compile } + +program test + use iso_c_binding + implicit none + +interface +function cfun(a) ! should be subroutine if saythis returns void +import :: c_ptr +type(c_ptr), value :: a +end function cfun +end interface + + + type scratch + character(c_char) :: c_line(11) + end type + type(scratch), target :: X + type(c_ptr) :: c_string_ptr + character(10) :: line + integer cerr ; + + line = 'my text' + X%c_line = line + X%c_line(11) = c_null_char + c_string_ptr = c_loc(X%c_line) + cerr = cfun(c_string_ptr); + write (*,*) "cerr= " , cerr ; +end program gfortran.dg/pr59997.f90 is a small modified version of the test proposed in comment 3. This test can only be compiled because cfun is not present. If you want to extend the test to run it, you should have a look at the several tests checking the C/Fortran interoperability, e.g., gfortran.dg/c_ptr_tests*. gfortran.dg/c_loc_test_20.f90 is a copy of the test found in the 4.9 branch or in trunk. I have checked that compiling this test with a clean 4.8.5 gives an ICE while the test compiles and runs with the patch.