libgfortran ASSOCIATED now compares element size for polymorphic pointers.
libgfortran's ASSOCIATED intrinsic now correctly handles polymorphic pointers by comparing element sizes.
A bug in libgfortran’s implementation of the ASSOCIATED intrinsic prevented it from correctly identifying associated polymorphic pointers, particularly character sections. The issue stemmed from comparing spans and strides directly, which could differ even when denoting the same storage. This fix introduces a stride_in_bytes helper and ensures ASSOCIATED now compares the stride in bytes and checks for agreement on element length or span, resolving PR126964.
In Details
The Fortran ASSOCIATED intrinsic checks if two pointers refer to the same storage sequence. The existing implementation in intrinsics/associated.c compared descriptor spans and strides field-by-field. However, when a pointer is polymorphic or refers to a character section, its stride may be encoded differently (e.g., spacing folded into stride, element length as span). The fix modifies associated to compare the stride in bytes and requires agreement on either the element length (from dtype) or the span, rather than solely on the span, thus correctly handling these complex cases, parti…
- ASSOCIATED
- A Fortran intrinsic function that returns
.TRUE.if a given pointer is associated with a target, and.FALSE.otherwise. It can also check if two pointers refer to the same storage sequence. - Descriptor
- A data structure that describes an array or a scalar variable. For arrays, a descriptor typically holds information such as the base address, element size, shape (dimensions), and strides (the number of elements to skip to move to the next element along each dimension).
- Pointer
- In Fortran, a variable that holds the memory address of another data item. Pointers are used for dynamic memory allocation and to refer to data indirectly.
- Storage sequence
- A contiguous sequence of memory locations. Two pointers are associated with the same storage sequence if they point to the same block of memory.
- Stride
- The number of elements to skip in memory to advance to the next element along a particular dimension of an array, as defined in its descriptor. It is often expressed in bytes or in number of elements.
- Polymorphic
- In Fortran, a variable that can hold values of different data types during its lifetime. This is typically used with pointers and derived types.
- Character section
- A contiguous part of a character string. When accessing a substring, a descriptor might represent it as a character section with specific span and stride information.