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

Assignment with polymorphic component, wrong result

MR
Beginner
702 Views

Hi,
   the attached code should print "1  2", however, it prints "2  2".
Indeed, it seems that each assignment to V(I) in fact assigns to the
whole array V.

ifort -V
Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 13.1 Build 20130121

(I don't have the very latest version, so I don't know how it works).

program ts
 implicit none
 type, abstract :: at
  integer :: f
 end type at
 type, extends(at) :: t
 end type t
 type :: tv
  class(at), allocatable :: x
 end type tv

 type(tv), allocatable :: v(:)
 type(tv) :: x

  allocate(v(2))
  allocate(t::x%x)
  x%x%f = 1
  v(1) = x
  x%x%f = 2
  v(2) = x
  ! Should print "1  2"
  write(*,*) v(1)%x%f, v(2)%x%f

end program ts

0 Kudos
8 Replies
Steven_L_Intel1
Employee
702 Views

I can reproduce this with the latest version - thanks. We'll look into it.

0 Kudos
Steven_L_Intel1
Employee
702 Views

Escalated as issue DPD200243378. It is somewhat interesting that intrinsic assignment to a polymorphic variable is non-standard in F2003 (it is new in F2008) and we don't yet support that, but a polymorphic allocatable component of a derived type is ok in F2003 (and we are supposed to support that feature.)

0 Kudos
MR
Beginner
702 Views

Steve, thank you for the update.

It is somewhat interesting that intrinsic assignment to a polymorphic variable is non-standard in F2003 (it is new in F2008) and we don't yet support that, but a polymorphic allocatable component of a derived type is ok in F2003 (and we are supposed to support that feature.)

Yes, indeed. It looks like this parallels what happened with allocatable arrays: initially they were reallocated only inside derived types (F95+TR), but not as standalone variables, and then this "inconsistency" was removed allowing reallocation of the left-hand-side (F2003).

Marco


0 Kudos
Steven_L_Intel1
Employee
702 Views

This has been fixed for a future update, probably October.

0 Kudos
m_sulc
Beginner
702 Views

Interestingly, using the (hopefully not so old-fashioned) "Intel(R) Fortran Intel(R) 64 Compiler XE for applications running on Intel(R) 64, Version 12.1.5.339 Build 20120612", the provided example compiles fine, nevertheless the result seems to be "0 0"...

0 Kudos
Steven_L_Intel1
Employee
702 Views

The problem is not a compile error, it's wrong results at run-time. Version 12.1.5 is about a year old

0 Kudos
m_sulc
Beginner
702 Views

Yes, of course, I just wanted to point out that the older version produces a different result (still incorrect, though). There, the problem seems to mainly consist in the fact that after the assignment:
[fortran]
v(1) = x
[/fortran]
v(1)%x is not allocated, although it (in F2003) should, right?
Using ifort 13, ALLOCATED(v(1)%x) returns .TRUE.

0 Kudos
Steven_L_Intel1
Employee
702 Views

The present problem is that the compiler incorrectly handles the assignments to v(1) and v(2), effectively assigning to all elements of v for each assignment. The write of v(2) overwrote the value of v(1), leading to the wrong result.

I would not be astonished that 12.1 got it even more wrong.

0 Kudos
Reply