https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127637
--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The trunk branch has been updated by Mikael Morin <[email protected]>: https://gcc.gnu.org/g:ad428b981ac3bc113f3cbb4041126c3c1fef4709 commit r17-4832-gad428b981ac3bc113f3cbb4041126c3c1fef4709 Author: Mikael Morin <[email protected]> Date: Fri Oct 2 14:34:18 2026 +0200 fortran: Ignore attributes of class containers's _data component [PR127637] The majority of the attributes returned by gfc_variable_attr are taken from the last component reference if there is one; otherwise from the root symbol. This misses some attributes if the last component reference is the implicit _data reference of a class container, as those only store the allocatable, dimension, codimension and pointer attributes. The rest of attributes remain in the original entity and are not returned if there is a component reference that follows. This change adds a tracking of the current type through the reference chain walk, and a check on each component ref to avoid overwriting the current set of attributes if the current type is a class container, and the component its _data field. The attributes stored in the _data component are unaffected by this as they are already tracked in dedicated variables outside of the main attribute set. The optional variable, tracking the optional attribute value, was cleared on the first component ref. Move that under the new check for the same reason. With the _data components ignored, the attributes returned for a reference to a polymorphic function result would become those of the procedure itself. Add a check of the current namespace and adjust the attributes if the namespace is the procedure body, meaning the reference is to the function result. The removal of some attributes for function results has some impact when the function is not polymorphic, requiring additional adjustments to prevent regressions. In gfc_check_pointer_assign, the change causes an earlier check to trigger, emitting a less specific error; move it after the function result check that emitted the error before the change. In gfc_check_c_funloc, the function attribute was guarding a check for function result, causing it to be skipped and a later check to emit a worse error message after the change; remove that attribute from the guarding condition. The testcase catches a missed optional attribute causing an uninitialized loop bound to be used. PR fortran/127637 gcc/fortran/ChangeLog: * primary.cc (gfc_variable_attr): Track the current type through the data reference walk. Don't overwrite the attribute set and the optional variable if the current type is a class container and the component its _data field. If the expression symbol is a function without separate result name, and the current namespace is the symbol's procedure body, adjust the attributes to those of the function result. * expr.cc (gfc_check_pointer_assign): Move the generic error check after the specific function result error check. * check.cc (gfc_check_c_funloc): Drop the function attribute from the condition guarding the function result check. gcc/testsuite/ChangeLog: * gfortran.dg/optional_class_2.f90: New test.
