Bug 68433 - Wrong code with INTERFACE, INTRINSIC, and optional arguments
Summary: Wrong code with INTERFACE, INTRINSIC, and optional arguments
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks: 29670
  Show dependency treegraph
 
Reported: 2015-11-19 08:07 UTC by Dominique d'Humieres
Modified: 2020-02-18 18:35 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-11-19 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Dominique d'Humieres 2015-11-19 08:07:55 UTC
While investigating pr46846, I found that the following code

program ainttest
   implicit none
   intrinsic aint
   intrinsic index
   intrinsic len
   intrinsic nint
   real r
   character c, string2
   character(7) string1
   integer i

   r = 3.14
   c = 'A'
   string2 = 'n'
   string1 = 'string1'
   i = 13
   call rsub('aint',aint,r)
   call rsubi('nint',nint,r)
   call char2isub('index',index,string1,string2)
   call charstarisub('len',len,string1)
end program ainttest

subroutine rsub(label,fun,x)
   implicit none
   character(*) label
   interface
      function fun(x,k)
         implicit none
         integer, intent(in), optional :: k
         real, intent(in) :: x
         real fun
      end function fun
   end interface
   real x

   write(*,*) label//'(',x,') = ',fun(x,k=8)
   print *, "expected:", aint(x,kind=8)
end subroutine rsub

subroutine rsubi(label,fun,x)
   implicit none
   character(*) label
   interface
      function fun(x,k)
         implicit none
         integer, intent(in), optional :: k
         real, intent(in) :: x
         integer fun
      end function fun
   end interface
   real x

   write(*,*) label//'(',x,') = ',"'",fun(x,k=2),"'"
   print *, "expected:","'",nint(x,kind=2),"'"
end subroutine rsubi

subroutine char2isub(label,fun,x,y)
   implicit none
   character(*) label,x,y
   interface
      function fun(x,y,l,k)
         implicit none
         logical, intent(in), optional :: l
         integer, intent(in), optional :: k
         character(*), intent(in) :: x,y
         integer fun
      end function fun
   end interface

   write(*,*) label//'(',x,',',y,') = ',fun(x,y)
   print *, "expected:", index(x,y)
end subroutine char2isub

subroutine charstarisub(label,fun,x)
   implicit none
   character(*) label,x
   write(*,*) label//'(',x,') = ',fun(x)
   print *, "expected:", len(x)
end subroutine charstarisub

while silencing the spurious warnings, generates a wrong code, the output being:

 aint(   3.14000010     ) =    3.00000000    
 expected:   3.0000000000000000     
 nint(   3.14000010     ) = '           3 '
 expected:'      3 '
 index(string1,n) =            1
 expected:           5
 len(string1) =            0
 expected:           7

Note that I am not 100% sure that the above code is valid.
Comment 1 Dominique d'Humieres 2015-11-19 08:08:37 UTC
Present from 4.8 up to trunk (6.0).