Assignment with polymorphic component, wrong result

Assignment with polymorphic component, wrong result

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

9 posts / 0 nouveau(x)
Dernière contribution
Reportez-vous à notre Notice d'optimisation pour plus d'informations sur les choix et l'optimisation des performances dans les produits logiciels Intel.

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

Steve - Intel Developer Support

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.)

Steve - Intel Developer Support

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

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

Steve - Intel Developer Support

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"...

M.

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

Steve - Intel Developer Support

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:

v(1) = x

v(1)%x is not allocated, although it (in F2003) should, right?
Using ifort 13, ALLOCATED(v(1)%x) returns .TRUE.

M.

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.

Steve - Intel Developer Support

Laisser un commentaire

Veuillez ouvrir une session pour ajouter un commentaire. Pas encore membre ? Rejoignez-nous dès aujourd’hui