https://gcc.gnu.org/g:b80e7138abe3d8948e690f1d3e70893190544734

commit b80e7138abe3d8948e690f1d3e70893190544734
Author: Mikael Morin <[email protected]>
Date:   Thu Sep 17 21:24:42 2026 +0200

    fortran: Dereference elements of span-addressed character arrays
    
    I have seen this causing regressions more than once, when working on a
    patch.  I finally dare fix it.
    Fortran-tested on aarch64-unknown-linux-gnu.  OK for mainline?
    
    -- >8 --
    
    The final step of gfc_build_spanned_array_ref after determining the pointer
    to the array element is generating a dereference of the pointer to access
    the actual data.  There was an exception that omitted that dereference if
    the pointer target type had the string flag set.  Remove the exception and
    always dereference the pointer.
    
    Because of the exception, the resulting type was different between a regular
    array reference and a span-based pointer arithmetics access, in the
    character case.  This could cause regressions when changing between the two
    array access ways, as code downstream could get an expression that didn't
    match its expectations.
    
    The non-dereferenced pointer returned by gfc_build_spanned_array_ref was
    causing gfc_get_data_ptr_offset to have its own dereference, to fix it
    after the fact.  That's now redundant; remove it as well.
    
    gcc/fortran/ChangeLog:
    
            * trans.cc (gfc_build_spanned_array_ref): Remove condition guarding
            pointer dereference generation.
            * trans-array.cc (gfc_get_dataptr_offset): Remove now redundant
            pointer dereference generation.

Diff:
---
 gcc/fortran/trans-array.cc | 4 ----
 gcc/fortran/trans.cc       | 4 +---
 2 files changed, 1 insertion(+), 7 deletions(-)

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index bbd50aa2ab47..9f60a72a3941 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -7738,10 +7738,6 @@ gfc_get_dataptr_offset (stmtblock_t *block, tree parm, 
tree desc, tree offset,
      to reference an element.  */
   tmp = build_array_ref (desc, offset, span_addressed_array (desc), NULL);
 
-  /* A spanned character element is referenced by a pointer.  */
-  if (POINTER_TYPE_P (TREE_TYPE (tmp)) && span_addressed_array (desc))
-    tmp = build_fold_indirect_ref_loc (input_location, tmp);
-
   /* Offset the data pointer for pointer assignments from arrays with
      subreferences; e.g. my_integer => my_type(:)%integer_component.  */
   if (subref)
diff --git a/gcc/fortran/trans.cc b/gcc/fortran/trans.cc
index b98fe0c680fb..2730ebf62c3d 100644
--- a/gcc/fortran/trans.cc
+++ b/gcc/fortran/trans.cc
@@ -498,9 +498,7 @@ gfc_build_spanned_array_ref (tree base, tree offset, tree 
span)
   tmp = gfc_build_addr_expr (pvoid_type_node, base);
   tmp = fold_build_pointer_plus_loc (input_location, tmp, offset);
   tmp = fold_convert (build_pointer_type (type), tmp);
-  if ((TREE_CODE (type) != INTEGER_TYPE && TREE_CODE (type) != ARRAY_TYPE)
-      || !TYPE_STRING_FLAG (type))
-    tmp = build_fold_indirect_ref_loc (input_location, tmp);
+  tmp = build_fold_indirect_ref_loc (input_location, tmp);
   return tmp;
 }

Reply via email to