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

commit c0d8ba655da55dd878b84b42f117243612b0c9dd
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 gfc_build_spanned_array_ref function creates a dereference of the result
    of its pointer arithmetics, except if the pointer target type has the string
    flag set.  Remove the latter 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 from 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
            generation of pointer dereference.
            * 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 3e3e88012efd..86fb62db5077 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -7739,10 +7739,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