[PATCH] Fortran: Fix PR125761 Wrong code: array section of an unlimited-polymorphic CHARACTER array

Jerry D jvdelisle2@gmail.com
Tue Jun 23 21:24:28 GMT 2026


See attached patch.

I incorporated Mikael's comments from the PR.

I regression tested this and also verified the original test case is fixed.

I also added a new test case.

OK for mainline?

Regards,

Jerry
---

     fortran: [PR125761] Unlimited-poly character array section base

     When a CLASS(*) array section carrying a CHARACTER payload is passed
     as the base of an array reference, build_array_ref and
     gfc_build_array_ref computed the element span from the dynamic type's
     vptr size alone, ignoring the unlimited-polymorphic object's _len
     field.  For a deferred-length character payload this gave the wrong
     element size, so the section's data pointer was offset incorrectly by
     the lower bound, producing wrong code (no ICE needed to reproduce: a
     section of a CLASS(*) character array passed to a procedure reads from
     the wrong address).

             PR fortran/125761

     gcc/fortran/ChangeLog:

             * trans-array.cc (build_array_ref): When the descriptor's
             canonical type is a class type, pass the class container as
             'decl' to gfc_build_array_ref for genuine array element
             references (rank > 0), so the element size can be corrected
             for an unlimited-polymorphic character payload.
             * trans-types.cc (gfc_typenode_for_spec): Move setting the
             GFC_CLASS_TYPE_P bit to..
             (gfc_get_derived_type): .. and use attr.is_class.
             (gfc_get_derived_type): Move setting the GFC_CLASS_TYPE_P bit
             as the last step before returning the derived->backend_decl
             if the derived->attr.is_class is true.
             * trans.cc (gfc_build_array_ref): Recognize a class container
             via its canonical type as well as GFC_CLASS_TYPE_P, and apply
             the _len-based span correction in that case too.

     gcc/testsuite/ChangeLog:

             * gfortran.dg/select_type_53.f90: New test.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: pr125761.diff
Type: text/x-patch
Size: 6960 bytes
Desc: not available
URL: <https://gcc.gnu.org/pipermail/fortran/attachments/20260623/4ad5fe79/attachment.bin>


More information about the Fortran mailing list