https://gcc.gnu.org/g:661acde60ef4e9ac5a9e48be18770fb3a9aeb9a5

commit r15-3014-g661acde60ef4e9ac5a9e48be18770fb3a9aeb9a5
Author: Andre Vehreschild <ve...@gcc.gnu.org>
Date:   Thu Jul 11 15:44:56 2024 +0200

    Fix ICE in recompute_tree_invariant_for_addr_expr, at tree.c:4535 [PR84244]
    
    Declaring an unused function with a derived type having a pointer
    component and using that derived type as a coarray, lead the compiler to
    ICE because the caf_token for the pointer was not linked into the
    component correctly.
    
            PR fortran/84244
    
    gcc/fortran/ChangeLog:
    
            * trans-types.cc (gfc_get_derived_type): When a caf_sub_token is
            generated for a component, link it to the component it is
            generated for (the previous one).
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/coarray/ptr_comp_5.f08: New test.

Diff:
---
 gcc/fortran/trans-types.cc                       |  6 +++++-
 gcc/testsuite/gfortran.dg/coarray/ptr_comp_5.f08 | 19 +++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc
index e6da8e1a58b..bc582085f57 100644
--- a/gcc/fortran/trans-types.cc
+++ b/gcc/fortran/trans-types.cc
@@ -2661,7 +2661,7 @@ gfc_get_derived_type (gfc_symbol * derived, int codimen)
   tree *chain = NULL;
   bool got_canonical = false;
   bool unlimited_entity = false;
-  gfc_component *c;
+  gfc_component *c, *last_c = nullptr;
   gfc_namespace *ns;
   tree tmp;
   bool coarray_flag, class_coarray_flag;
@@ -2961,10 +2961,14 @@ gfc_get_derived_type (gfc_symbol * derived, int codimen)
         types.  */
       if (class_coarray_flag || !c->backend_decl)
        c->backend_decl = field;
+      if (c->attr.caf_token && last_c)
+       last_c->caf_token = field;
 
       if (c->attr.pointer && (c->attr.dimension || c->attr.codimension)
          && !(c->ts.type == BT_DERIVED && strcmp (c->name, "_data") == 0))
        GFC_DECL_PTR_ARRAY_P (c->backend_decl) = 1;
+
+      last_c = c;
     }
 
   /* Now lay out the derived type, including the fields.  */
diff --git a/gcc/testsuite/gfortran.dg/coarray/ptr_comp_5.f08 
b/gcc/testsuite/gfortran.dg/coarray/ptr_comp_5.f08
new file mode 100644
index 00000000000..ed3a8db13fa
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/ptr_comp_5.f08
@@ -0,0 +1,19 @@
+! { dg-do compile }
+
+! Check PR84244 does not ICE anymore.
+
+program ptr_comp_5
+  integer, target :: dest = 42
+  type t
+    integer, pointer :: p
+  end type
+  type(t) :: o[*]
+
+  o%p => dest
+contains
+  ! This unused routine is crucial for the ICE.
+  function f(x)
+    type(t), intent(in) ::x
+  end function
+end program
+

Reply via email to