Dear all,

here's a fix for another one of Gerhard's "torture tests" that triggers
an ICE with -Warray-temporaries.  The cause was that the locus was not
set for the initializer of the symbol for the testcase.  The obvious
fix is to then use the locus from the array constructor generated for
the data values.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald

From b8c58ba3e8113cb05a45e8263dfd60726e173e68 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Tue, 3 Jun 2025 20:30:30 +0200
Subject: [PATCH] Fortran: ICE due to missing locus with data statement for
 coarray [PR99838]

	PR fortran/99838

gcc/fortran/ChangeLog:

	* data.cc (gfc_assign_data_value): For a new initializer use the
	location from the constructor as fallback

gcc/testsuite/ChangeLog:

	* gfortran.dg/coarray_data_2.f90: New test.
---
 gcc/fortran/data.cc                          |  8 +++++++-
 gcc/testsuite/gfortran.dg/coarray_data_2.f90 | 14 ++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/coarray_data_2.f90

diff --git a/gcc/fortran/data.cc b/gcc/fortran/data.cc
index 5c83f6948b8..a438c268cda 100644
--- a/gcc/fortran/data.cc
+++ b/gcc/fortran/data.cc
@@ -593,7 +593,13 @@ gfc_assign_data_value (gfc_expr *lvalue, gfc_expr *rvalue, mpz_t index,
 	{
 	  /* Point the container at the new expression.  */
 	  if (last_con == NULL)
-	    symbol->value = expr;
+	    {
+	      symbol->value = expr;
+	      /* For a new initializer use the location from the
+		 constructor as fallback.  */
+	      if (!GFC_LOCUS_IS_SET(expr->where) && con != NULL)
+		symbol->value->where = con->where;
+	    }
 	  else
 	    last_con->expr = expr;
 	}
diff --git a/gcc/testsuite/gfortran.dg/coarray_data_2.f90 b/gcc/testsuite/gfortran.dg/coarray_data_2.f90
new file mode 100644
index 00000000000..bda57f3df89
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/coarray_data_2.f90
@@ -0,0 +1,14 @@
+! { dg-do compile }
+! { dg-additional-options "-fcoarray=lib -Warray-temporaries" }
+!
+! PR fortran/99838 - ICE due to missing locus with data statement for coarray
+!
+! Contributed by Gerhard Steinmetz
+
+program p
+  type t
+     integer :: a
+  end type
+  type(t) :: x(3)[*]
+  data x%a /1, 2, 3/ ! { dg-warning "Creating array temporary" }
+end
-- 
2.43.0

Reply via email to