Problem with default assignment operator of type with polymorphic allocatable

Problem with default assignment operator of type with polymorphic allocatable


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?

module MyMod
 type :: BaseExtraProps
 character(len=1) :: dummyChar
 end type BaseExtraProps
type :: MyIntType
 integer :: int
 class(BaseExtraProps), allocatable :: extra
 !procedure MyIntTypeAssignOperator
 !generic :: assignment(=) => MyIntTypeAssignOperator
 end type MyIntType
 type :: MyIntWrapperType
 type(MyIntType) :: myInt
 procedure :: Init => MyIntWrapperInit
 procedure :: Get => MyIntWrapperGet
 end type MyIntWrapperType
 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'
 print *, 'ok'
 end if
end program AllocatableBug

3 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

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

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.

Retired 12/31/2016

Leave a Comment

Please sign in to add a comment. Not a member? Join today