Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

OOP function overriding bug

Patrice_l_
Beginner
627 Views

Hi all,

When using a generic operator in an extended type, the operator binding is not added to the list from the ' mother' type. The compiler reject the code saying the operation is invalid. If you remove the operator in the extend type, everything is fine.

Also, I'm testing overriding feature, and I do not really understand why function overriding is working but not when using the operator.

 

Thanks.

 

module test
type  :: my_integer
                integer, private :: value
        contains
                procedure,pass :: add_r => add_my_integer_r
                generic,public :: operator(+) => add_r
end type my_integer
type, extends(my_integer) :: my_integerr
        contains
                procedure,pass :: add_r => add_my_integer_rr
                procedure,pass :: add_real
                generic,public :: operator(+) => add_real
end type my_integerr

contains
function add_real(a,b) result(r)
        class(my_integerr),intent(in) :: a
        real,intent(in) :: b
        real :: r
        r=a%value+4d0*b
end function
function add_my_integer_rr(a,b) result(r)
        class(my_integerr),intent(in) :: a
        integer,intent(in) :: b
        integer :: r
        r=a%value+3*b
        print *,'overriding function'
end function
function add_my_integer_r(a,b) result(r)
        class(my_integer),intent(in) :: a
        integer,intent(in) :: b
        integer :: r
        r=a%value+b
end function
end module test
program toto
        use test
class(my_integer),allocatable :: t
class(my_integerr),allocatable :: tt
type(my_integerr),allocatable :: ttt
integer :: a

allocate(t,tt,ttt)
a=2
print *,'with operator'
print *,t+a
print *,tt+a
print *,ttt+a
print *,ttt+2.0

print *,'with function'
print *,t%add_r(a)
print *,tt%add_r(a)
print *,ttt%add_r(a)

end program toto

 

Result in  :

test2.f90(47): error #6355: This binary operation is invalid for this data type.   [TT]
print *,tt+a
--------^
test2.f90(47): error #6549: An arithmetic or LOGICAL type is required in this context.
print *,tt+a
----------^
test2.f90(48): error #6355: This binary operation is invalid for this data type.   [TTT]
print *,ttt+a
--------^
test2.f90(48): error #6549: An arithmetic or LOGICAL type is required in this context.
print *,ttt+a
-----------^
compilation aborted for test2.f90 (code 1)

 

 

0 Kudos
4 Replies
pbkenned1
Employee
627 Views

Look like it might be an ifort bug regarding  'generic,public :: operator(+) => add_real'.  Let me investigate a little bit and I'll get back to you.

Patrick

0 Kudos
pbkenned1
Employee
627 Views

I'm convinced this is an ifort bug regarding recognition of operator(+) for the extended type.  The code is standard-conforming Fortran and I don't see anything wrong with it.  I've reported this to the developers (internal ID DPD200362489).  I'll keep this thread updated with developments.

Patrick

0 Kudos
Patrice_l_
Beginner
627 Views

Hi,

I ran into another example slightly different but i think it is the same issue. The interface does not add up for a generic operator if a generic binding has alredy been defined. This gives an error for the "t1=t2", the error goes away if you comment the assignment copy  in type cm.

 

module types1
        implicit none
type cm
integer :: i
contains
        procedure,pass(t2) :: copy!_real=>copy_to_int
        generic,public :: assignment(=) => copy
end type cm
type cm2
double precision :: j
contains
        procedure,pass(t2) :: to_int => copy_to_int
        generic,public :: assignment(=) => to_int
end type cm2


contains
subroutine copy(t1,t2)
class(cm),intent(out) ::t1
class(cm),intent(in) ::t2
t1%i=t2%i
end subroutine

subroutine copy_to_int(t1,t2)
        class(cm),intent(out) :: t1
        class(cm2),intent(in) :: t2

        t1%i=int(t2%j)
end subroutine
subroutine test()
class(cm),allocatable :: t1
class(cm2),allocatable :: t2
allocate(t1,t2)
t1=t2
end subroutine
end module

 

0 Kudos
pbkenned1
Employee
627 Views

The original test case is fixed in the 16.0 compiler, so I am closing this ticket now.  I tested this on Windows, but it shouldn't be any different on Linux or Mac.  If the outputs are incorrect, please reply.

C:\ISN_Forums\U534286>ifort U534286.f90
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 16.0.0.110 Build 20150815
Copyright (C) 1985-2015 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 12.00.21005.1
Copyright (C) Microsoft Corporation.  All rights reserved.

-out:U534286.exe
-subsystem:console
U534286.obj

C:\ISN_Forums\U534286>U534286.exe
 with operator
     6226006
 overriding function
           6
 overriding function
           6
   8.000000
 with function
     6226006
 overriding function
           6
 overriding function
           6

C:\ISN_Forums\U534286>

0 Kudos
Reply