- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I agree this is a bug. You don't even get a warning when standards checking is enabled.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It is a bug and I escalated it to be fixed (CMPLRLLVM-74900). Thank you for reporting it.
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page