Fortran: Fixes finalizer list truncation bug
Corrects a bug in the Fortran compiler that truncated finalizer lists when three or more finalizers were present.
A linked-list bug in GCC’s Fortran frontend has been resolved, which previously caused the finalizer list of derived types to be truncated when three or more finalizers matched. The fix implements a standard tail-pointer idiom to ensure all matching finalizers are correctly appended and preserved. A test case has been updated to verify this correction.
In Details
The gfc_resolve_finalizers function in gcc/fortran/resolve.cc suffered from a linked-list implementation flaw. When copying finalizers from a template type to a derived type's finalizer list, if three or more finalizers matched, subsequent entries were orphaned. This commit rectifies this by using a proper tail-pointer approach, ensuring the linked list is correctly traversed and new entries are appended to the end.
- Finalizer
- In Fortran, a finalizer is a procedure that is automatically executed when an object of a derived type goes out of scope or is deallocated. It's used for cleanup operations.
- Derived Type
- A user-defined composite data type in Fortran, similar to 'struct' in C or 'class' in C++. It can contain components of various intrinsic or other derived types.
- Linked List
- A linear data structure where elements (nodes) are not stored contiguously in memory. Each node contains data and a pointer to the next node in the sequence.
- Tail-pointer idiom
- A common programming technique for linked lists where a pointer to the last node (the tail) is maintained. This allows for efficient addition of new elements at the end of the list.