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

Problem with default assignment operator of type with polymorphic allocatable

Marat_S_
Beginner
687 Views

Hello, 

please take a look at this small program. MyIntType is not assigned properly in function MyIntWrapperGet (program prints error). Even stack is corrupted after calling MyIntWrapperGet. If you uncomment MyIntTypeAssignOperator, then everything works as expected, MyIntType is assigned properly, stack is not corrupted.

As for me, this code looks fine. So could you please explain if it is a bug in compiler or the code actually is wrong?

[fortran]

module MyMod
type :: BaseExtraProps
character(len=1) :: dummyChar
end type BaseExtraProps

type :: MyIntType
integer :: int
class(BaseExtraProps), allocatable :: extra
contains
!procedure MyIntTypeAssignOperator
!generic :: assignment(=) => MyIntTypeAssignOperator
end type MyIntType

type :: MyIntWrapperType
type(MyIntType) :: myInt
contains
procedure :: Init => MyIntWrapperInit
procedure :: Get => MyIntWrapperGet
end type MyIntWrapperType

contains
subroutine MyIntWrapperInit(this, int)
class(MyIntWrapperType), intent(out) :: this
integer, intent(in) :: int

this%myInt%int = int
end subroutine MyIntWrapperInit


function MyIntWrapperGet(this) result(val)
class(MyIntWrapperType), intent(in) :: this
type(MyIntType) :: val

val = this%myInt
end function MyIntWrapperGet


!subroutine MyIntTypeAssignOperator(lhs, rhs)
! class(MyIntType), intent(inout) :: lhs
! type(MyIntType), intent(in) :: rhs
!
! lhs%int = rhs%int
! allocate(lhs%extra, source=rhs%extra)
!end subroutine MyIntTypeAssignOperator
end module MyMod


program AllocatableBug
use MyMod
implicit none

type(MyIntWrapperType) :: myIntWrapper
type(MyIntType) :: myInt
integer :: i = 5

call myIntWrapper%Init(i)
myInt = myIntWrapper%Get()
if (myIntWrapper%myInt%int /= myInt%int) then
print *, 'error'
else
print *, 'ok'
end if

end program AllocatableBug

[/fortran]

0 Kudos
2 Replies
Marat_S_
Beginner
687 Views

By the way, I use Intel Visual Fortran Composer XE 2013.
 Also, if I change
class(BaseExtraProps), allocatable :: extra
to
type(BaseExtraProps), allocatable :: extra
I will get expected behavior and no erros, but in this case I want extra to be polymorphic.  

0 Kudos
Steven_L_Intel1
Employee
687 Views

I can reproduce this in Update 2 but not in Update 3, which should be out this week. It looks similar to an issue I escalated a while ago.

0 Kudos
Reply