Fortran: Fixes inferred CLASS associate name type resolution
Corrects type resolution for CLASS associate names with inferred types in Fortran, preventing compile-time folding of polymorphic references.
This commit fixes a bug in the Fortran compiler where the type of a CLASS associate name with an inferred type was being incorrectly resolved. When the associate name’s selector was a function not yet parsed, resolve_variable would call gfc_fix_class_refs, which erroneously added a _data reference to polymorphic array component references. This made the expression non-polymorphic and caused SAME_TYPE_AS to be folded to .false. at compile time. The fix prevents gfc_fix_class_refs from being called on inferred-type references that gfc_fixup_inferred_type_refs has already processed.
In Details
The Fortran compiler's resolve_variable function has a bug related to CLASS associate names whose types are inferred. When such an associate name's selector is a function not yet parsed, resolve_variable calls gfc_fixup_inferred_type_refs and subsequently gfc_fix_class_refs. The latter incorrectly modifies polymorphic array component references (e.g., q%c1(4)%c2) by prepending _data, effectively de-polymorphizing them. This leads to SAME_TYPE_AS folding to .false.. This commit skips the problematic gfc_fix_class_refs call for inferred-type references, as they are already c…
- associate name
- A Fortran construct that associates a name with a variable or expression, allowing it to be referred to by that name within a scope.
- CLASS
- A Fortran keyword used for declaring variables that can hold objects of different types derived from a common ancestor type, enabling polymorphism.
- inferred type
- A type that the compiler determines automatically based on context, rather than being explicitly declared by the programmer.
- polymorphic
- In object-oriented programming, an entity (like a variable or function) that can take on different forms or behave differently depending on the type of data it is operating on or refers to.
- SAME_TYPE_AS
- A Fortran intrinsic function that checks if two entities have the same dynamic type.
- gfc_fix_class_refs
- An internal GCC function within the Fortran frontend responsible for fixing up references to class (polymorphic) components.
- gfc_fixup_inferred_type_refs
- An internal GCC function within the Fortran frontend responsible for resolving references to variables with inferred types.