Dear all,

the attached patch addresses an actually very long-standing issue
with bogus bounds checks for components of nested derived types in
assignments when an intermediate level has the POINTER attribute
instead of the ALLOCATABLE attribute.  It turned out that the
check for a reallocatable lhs failed in such situations, as it
depended on an attribute that is currently not properly set.

I did not see a way to fix the alloc_comp attribute so that
it can deal with the current situation and decided to remove
that check in gfc_is_reallocatable_lhs.

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

As this issue leads to wrong code, is it OK to backport
e.g. to 14-branch?

Thanks,
Harald

From 74ef401638194bfc86fec3e78b451445ed86eabe Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anl...@gmx.de>
Date: Wed, 19 Mar 2025 22:56:03 +0100
Subject: [PATCH] Fortran: fix bogus bounds check for reallocation on
 assignment [PR116706]

	PR fortran/116706

gcc/fortran/ChangeLog:

	* trans-array.cc (gfc_is_reallocatable_lhs): Fix check on
	allocatable components of derived type or class objects.

gcc/testsuite/ChangeLog:

	* gfortran.dg/bounds_check_27.f90: New test.
---
 gcc/fortran/trans-array.cc                    |  4 +-
 gcc/testsuite/gfortran.dg/bounds_check_27.f90 | 45 +++++++++++++++++++
 2 files changed, 46 insertions(+), 3 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/bounds_check_27.f90

diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc
index 8ab290bbe61..e9eacf20128 100644
--- a/gcc/fortran/trans-array.cc
+++ b/gcc/fortran/trans-array.cc
@@ -11236,9 +11236,7 @@ gfc_is_reallocatable_lhs (gfc_expr *expr)
     return true;
 
   /* All that can be left are allocatable components.  */
-  if ((sym->ts.type != BT_DERIVED
-       && sym->ts.type != BT_CLASS)
-	|| !sym->ts.u.derived->attr.alloc_comp)
+  if (sym->ts.type != BT_DERIVED && sym->ts.type != BT_CLASS)
     return false;
 
   /* Find a component ref followed by an array reference.  */
diff --git a/gcc/testsuite/gfortran.dg/bounds_check_27.f90 b/gcc/testsuite/gfortran.dg/bounds_check_27.f90
new file mode 100644
index 00000000000..678aef63af6
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bounds_check_27.f90
@@ -0,0 +1,45 @@
+! { dg-do run }
+! { dg-additional-options "-fcheck=bounds" }
+!
+! PR fortran/116706 - bogus bounds check for reallocation on assignment
+! Contributed by Balint Aradi  <baradi09 at gmail.com>
+
+program testprog
+  implicit none
+
+  type :: data_node
+     integer, allocatable :: data(:)
+  end type data_node
+
+  type :: data_list
+     type(data_node), pointer :: nodes(:) => null()
+  end type data_list
+
+  type :: upoly_node
+     class(*), allocatable :: data(:)
+  end type upoly_node
+
+  type :: star_list
+     type(upoly_node), pointer :: nodes(:) => null()
+  end type star_list
+
+  type(data_list) :: datalist
+  type(star_list) :: starlist
+  class(star_list), allocatable :: astarlist
+  class(star_list), pointer     :: pstarlist
+
+  allocate (datalist%nodes(2))
+  datalist%nodes(1)%data = [1, 2, 3]
+
+  allocate (starlist%nodes(2))
+  starlist%nodes(1)%data = [1., 2., 3.]
+
+  allocate (astarlist)
+  allocate (astarlist%nodes(2))
+  astarlist%nodes(1)%data = [1, 2, 3]
+
+  allocate (pstarlist)
+  allocate (pstarlist%nodes(2))
+  pstarlist%nodes(1)%data = [1., 2., 3.]
+
+end program testprog
-- 
2.43.0

Reply via email to