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

Non-standard assignment with different derived types in constructor of derived type

Oliver_Zick
Novice
495 Views

We came across a compiler bug that misses to check the compatibility of derived types on initialization of members of a derived type using its constructor:

 

program SampleProg

  implicit none

  ! Type definitions
  type :: TA
    integer(4) :: value_
  end type

  type :: TB
    character(8) :: name_
  end type

  type :: THost
    type(TA) :: a_
  end type

  ! Variables
  type(THost) :: host

  ! This is the non-standard part:
  ! Why can an instance of TB get assigned to a member of type TA without compiler error?
  host = THost(a_ = TB('ABCDEFGH'))

  print *, "Host%a_%value_ interpreted as integer:", host%a_%value_
  
end program

 

For line 23, the compiler shoud raise an error indicating that an instance of TB cannot be assigned to the member a_ which is of type TA.

 

We observed this behavior in Intel Fortran compiler even with standard semantics and all checks/warning turned on. Did we miss something?

 

Gfortran reports an error indicating that TA and TB are incompatible derived types which is the behavior we also have expected from Intel Fortran compiler.

0 Kudos
5 Replies
witwald
New Contributor II
440 Views

Although it may seem unusual at first glance, I don't think that this storage of characters in an integer is all that unusual for Fortran compilers. It may fall under the heading of implicit type conversion. The characters can also, of course, be stored in a real number. It can be noted that the 8-character string would have been truncated as it is stored in a 4-byte integer. To store all 8 characters would require the use of an integer(8) or real(8) data type. This sort of behaviour is probably supported in order to maintain compatibility with older Fortran codes that made specific use of this feature.

0 Kudos
Oliver_Zick
Novice
438 Views

I can understand this and had the same idea. But on the other hand, it's confusing because you somehow expect a compiler error when you try to assign two incompatible derived types.

 

In our case, it caused some issues because we had corrupted data and no one took notice of it because of - at least - a compiler hint that two incompatible derived types are assigned.

Oliver_Zick
Novice
436 Views

So when you do the same with local variables like in the example below, the compiler reports error #6197: An assignment of different structure types is invalid.

 

program SampleProg

  implicit none

  ! Type definitions
  type :: TA
    integer(4) :: value_
  end type

  type :: TB
    character(8) :: name_
  end type
  
  type(TA):: a
  
  ! Properly handled by compiler indicating error #6197: An assignment of different structure types is invalid.
  a = TB('ABCDEFGH')

end program

 

This is the behavior we expect also when doing the same assignment within a constructor call of a derived type.

Steve_Lionel
Honored Contributor III
416 Views

I agree this is a bug. You don't even get a warning when standards checking is enabled.

Igor_V_Intel
Moderator
375 Views

It is a bug and I escalated it to be fixed (CMPLRLLVM-74900). Thank you for reporting it.


Reply