GCC Newspaper
JULY 29, 2026
Date
/
Architectures
Components
Topics
News & Policy
fortran

Fortran: Fix derived type rank for array allocation with mold

Corrects an issue where the rank of an array within a derived type was not set when using the `mold` keyword during allocation.

This change fixes a bug in Fortran’s array allocation where the rank of an array inside a derived type was not correctly determined when the mold keyword was used. The gfc_array_init_size function in trans-array.cc has been updated to ensure the rank is properly set, preventing potential errors in derived type initialization. New test cases have been added to verify the fix.

In Details

In gcc/fortran/trans-array.cc, gfc_array_init_size now correctly sets the rank field of the dtype for arrays allocated using ALLOCATE(derived_type_var%array_member, mold = source_array). Previously, the rank was omitted, leading to incorrect type information for such arrays.

For Context
derived type
A user-defined data type in Fortran that can aggregate variables of different types and kinds, including arrays and other derived types.
rank
The number of dimensions of an array. A scalar has rank 0, a vector has rank 1, a matrix has rank 2, and so on.
mold
An optional keyword in Fortran's ALLOCATE statement that specifies the type, kind, rank, and shape of the array to be allocated, based on an existing array or variable.
Filed Under: fortrancompilerallocation