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

Fortran: Copy parenthesized scalar CLASS actual arguments

Fixes segfaults when passing parenthesized CLASS variables with allocatable components to TYPE dummies.

A bug in the Fortran frontend caused parenthesized scalar CLASS variables passed as actual arguments to be aliased instead of copied. This led to a segfault when such variables were passed to TYPE dummies with allocatable components, as the components were incorrectly copied and freed. The fix ensures these arguments are properly copied, with the copy’s lifetime managed correctly to avoid issues with its components.

In Details

The gfc_conv_expr_op in GCC's Fortran frontend mishandled parenthesized scalar CLASS variables passed as actual arguments. Instead of creating a copy, it allowed the dummy argument to alias the original variable. This led to incorrect copying and freeing of derived type components when the CLASS variable contained allocatable components and was passed to a TYPE dummy argument. A new function, conv_class_value_copy, is introduced to ensure proper value copying through the vtable and manage the copy's finalization.

For Context
CLASS
In Fortran, a derived type that can hold values of different intrinsic or other derived types, determined at runtime. It's conceptually similar to a C++ std::variant or union with type tagging.
TYPE dummy argument
A procedure argument in Fortran that is declared with a specific derived type. When passed a value, the compiler ensures it conforms to that type.
allocatable components
Members of a derived type that can be dynamically allocated and deallocated, similar to pointers in C++ but with automatic memory management handled by the language.
vtable
Virtual Method Table. A runtime lookup table used by C++ and some other languages (like Fortran's CLASS types) to support dynamic dispatch, allowing method calls to be resolved at runtime based on the actual type of an object.
Filed Under: fortrantype systempass-by-valuebugfix