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

Fortran polymorphic assignment to temporary now preserves class information

Fortran compiler now correctly uses polymorphic assignment for class values copied to temporaries.

This change ensures that when a Fortran class value is copied to a temporary variable, the compiler uses polymorphic assignment. Previously, the class information could be lost, causing the assignment to fall back to non-polymorphic behavior. The fix involves saving and using the class container reference in expression metadata, allowing the compiler to correctly handle assignments involving polymorphic types and temporaries.

In Details

The Fortran frontend's trans_scalar_class_assign was incorrectly falling back to non-polymorphic assignment when a temporary variable was involved in copying a class value. This happened because the gfc_get_class_from_expr call could not infer the class container from the temporary. This commit modifies gfc_conv_variable to save the class container expression, and trans_scalar_class_assign now prioritizes using this saved metadata, resolving PR fortran/128349.

For Context
polymorphic assignment
In Fortran, an assignment operation that can handle derived types with different dynamic types at runtime, often used with assumed-type variables or procedure arguments.
class value
A value of a Fortran derived type that may exhibit polymorphic behavior.
temporary variable
A variable created implicitly by the compiler during expression evaluation or for intermediate results.
non-polymorphic assignment
A standard assignment operation that operates on concrete types and does not account for runtime type variations.
scalarizer
A compiler pass that transforms array operations into equivalent scalar operations for easier processing or optimization.
expression metadata
Information associated with an expression that describes its properties, such as its type, location, or class container.
Filed Under: fortranassignmentpolymorphism