Hi all,

attached patch fixes an ICE when in an expression with a coindex a function was
used more than once. E.g. coarray(mod( something ), mod( something else ))[i]
ICE'd because a component for aliasing the second mod() could not be created.

Regtests ok on x86_64-pc-linux-gnu / F41. Ok for mainline and gcc-15 later on?

Regards,
        Andre
-- 
Andre Vehreschild * Email: vehre ad gmx dot de 
From e4d4dd9768f7797e30542ec99b16093a663c65f3 Mon Sep 17 00:00:00 2001
From: Andre Vehreschild <ve...@gcc.gnu.org>
Date: Fri, 27 Jun 2025 15:31:21 +0200
Subject: [PATCH] Fortran: Ensure arguments in coarray call get unique
 components in add_data [PR120847]

	PR fortran/120847

gcc/fortran/ChangeLog:

	* coarray.cc (check_add_new_comp_handle_array): Make the count
	of components static to be able to create more than one.  Create
	an array component only for array expressions.

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray/coindexed_7.f90: New test.
---
 gcc/fortran/coarray.cc                        |  4 ++--
 .../gfortran.dg/coarray/coindexed_7.f90       | 24 +++++++++++++++++++
 2 files changed, 26 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray/coindexed_7.f90

diff --git a/gcc/fortran/coarray.cc b/gcc/fortran/coarray.cc
index 2f067f855e5..6914697c78b 100644
--- a/gcc/fortran/coarray.cc
+++ b/gcc/fortran/coarray.cc
@@ -503,7 +503,7 @@ check_add_new_comp_handle_array (gfc_expr *e, gfc_symbol *type,
 				 gfc_symbol *add_data)
 {
   gfc_component *comp;
-  int cnt = -1;
+  static int cnt = -1;
   gfc_symtree *caller_image;
   gfc_code *pre_code = caf_accessor_prepend;
   bool static_array_or_scalar = true;
@@ -566,7 +566,7 @@ check_add_new_comp_handle_array (gfc_expr *e, gfc_symbol *type,
   else
     {
       comp->initializer = gfc_copy_expr (e);
-      if (e_attr.dimension)
+      if (e_attr.dimension && e->rank)
 	{
 	  comp->attr.dimension = 1;
 	  comp->as = get_arrayspec_from_expr (e);
diff --git a/gcc/testsuite/gfortran.dg/coarray/coindexed_7.f90 b/gcc/testsuite/gfortran.dg/coarray/coindexed_7.f90
new file mode 100644
index 00000000000..066397024f4
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray/coindexed_7.f90
@@ -0,0 +1,24 @@
+!{ dg-do compile }
+
+! Check PR120847 is fixed.
+
+program p
+  implicit none
+
+  type T
+    integer, allocatable :: i(:, :) [:]
+  end type T
+
+  type(T) :: o
+  integer, allocatable :: c[:]
+  integer :: i
+
+  c = 7
+
+  allocate(o%i(4, 5)[*], source=6)
+
+  do i = 1, 4
+    c = o%i(mod(i, 2), mod(i, 3))[1]
+  end do
+
+end program p
-- 
2.50.0

Reply via email to