GCC Newspaper
OCTOBER 6, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

Fortran: Add deep copy mode to subreference array argument conversion.

New deep copy mode for Fortran array arguments in subreferences improves handling of allocatable components.

A new DEEP_COPY mode has been added to the gfc_conv_subref_array_arg function in GCC’s Fortran front-end. This mode, defaulting to false, allows temporary array arguments to have their own distinct allocatable components, rather than sharing them with the original argument. This change, which does not affect existing callers, ensures proper memory management for allocatable components by deallocating them after the call, and also fixes two latent issues in the pass_optional path related to descriptor assignment and string length handling.

In Details

The gfc_conv_subref_array_arg function, used in Fortran argument passing, gains a DEEP_COPY flag. When true, it propagates to gfc_trans_scalar_assign to create temporaries with independent allocatable components. These components are then freed post-call. This resolves issues in pass_optional handling, specifically incorrect pointer assignment and uncopied string lengths, which were previously non-reachable due to check_contiguous preconditions.

For Context
allocatable components
In Fortran, these are parts of a data structure (like an array or derived type) that can be dynamically allocated and deallocated at runtime. They are managed using ALLOCATE and DEALLOCATE statements.
INTENT_IN
A Fortran dummy argument attribute indicating that the argument is intended only for input to the procedure and will not be modified.
pass_optional
A mechanism in GCC's Fortran front-end to handle optional dummy arguments that are not passed by the caller. It involves specific checks and data structure handling to ensure correctness.
Filed Under: fortranoptimization