GCC Newspaper
NOVEMBER 30, 2026
Date
/
Architectures
Components
Topics
News & Policy
Other
fortran

fortran: Improve target-independent ASSOCIATE type inference

Fortran compiler now infers ASSOCIATE types in a target-independent manner, fixing cross-compilation issues.

The Fortran compiler’s type inference for ASSOCIATE constructs has been made target-independent to resolve cross-compilation failures. Previously, when an ASSOCIATE selector was a function not yet parsed, the compiler guessed the associate name’s type based on the first component reference. This guess depended on the order symbols were inserted, which varied by target. The fix ensures the type guess is predictable by picking the first candidate type where the entire component chain exists and by collecting candidates using an in-order symbol tree walk. This change also improves handling of substring and array section parsing when the guessed component is a character array.

In Details

This commit refactors the gfc_match_varspec function in the Fortran frontend to make the type inference for ASSOCIATE selectors target-independent. Previously, when an ASSOCIATE selector was a function call or similar construct whose type was not yet fully resolved, gfc_match_varspec would infer the type based on the first component reference. This inference relied on a pre-order traversal of the symbol tree, leading to target-dependent behavior as the symtree's structure could differ. The changes introduce inferred_type_chain_ok and skip_ref_groups to select the first candidate t…

For Context
ASSOCIATE construct
A Fortran construct that allows a block of code to refer to a variable or expression using a different name within that block.
selector
In an ASSOCIATE construct, the variable or expression whose name is being given an alias.
derived type
A user-defined composite data type in Fortran, similar to a struct or class in other languages.
component
A member of a derived type.
symtree
Symbol Tree. An internal data structure used by the compiler to organize and manage declared symbols (variables, types, functions, etc.) in a program.
in-order traversal
A tree traversal algorithm that visits nodes in a specific order: left subtree, root, right subtree. In this context, it refers to iterating through symbols in a sorted or defined sequence.
pre-order traversal
A tree traversal algorithm that visits nodes in the order: root, left subtree, right subtree. This was the previous method used, leading to target-dependent results.
substring
A contiguous sequence of characters within a character string.
array section
A portion of an array specified using indexing, which can include ranges and strides.
Filed Under: fortrancompiler frontendcross-compilation