]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix ICE with automatic reallocation [PR100245]
authorJosé Rui Faustino de Sousa <jrfsousa@gmail.com>
Fri, 2 Sep 2022 19:35:22 +0000 (21:35 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Fri, 16 Sep 2022 19:02:13 +0000 (21:02 +0200)
gcc/fortran/ChangeLog:

PR fortran/100245
* trans-expr.cc (trans_class_assignment): Add if clause to handle
derived type in the LHS.

gcc/testsuite/ChangeLog:

PR fortran/100245
* gfortran.dg/PR100245.f90: New test.

(cherry picked from commit 504424f33771be0405454e7845219d5df1bb88bb)

gcc/fortran/trans-expr.cc
gcc/testsuite/gfortran.dg/PR100245.f90 [new file with mode: 0644]

index e35ea2fc790b0c587adc9d7a9e81f89f54370ad2..7895d0346101ea22603e880b1f2128a816efbbed 100644 (file)
@@ -11435,6 +11435,9 @@ trans_class_assignment (stmtblock_t *block, gfc_expr *lhs, gfc_expr *rhs,
       class_han = GFC_CLASS_TYPE_P (TREE_TYPE (lse->expr))
          ? gfc_class_data_get (lse->expr) : lse->expr;
 
+      if (!POINTER_TYPE_P (TREE_TYPE (class_han)))
+       class_han = gfc_build_addr_expr (NULL_TREE, class_han);
+
       /* Allocate block.  */
       gfc_init_block (&alloc);
       gfc_allocate_using_malloc (&alloc, class_han, size, NULL_TREE);
diff --git a/gcc/testsuite/gfortran.dg/PR100245.f90 b/gcc/testsuite/gfortran.dg/PR100245.f90
new file mode 100644 (file)
index 0000000..07c1f7b
--- /dev/null
@@ -0,0 +1,28 @@
+! { dg-do run }
+!
+! Test the fix for PR100245
+!
+
+program main_p
+
+  implicit none
+
+  type :: foo_t
+    integer :: a
+  end type foo_t
+
+  integer, parameter :: a = 42
+
+  class(foo_t), allocatable :: val
+  class(foo_t), allocatable :: rs1
+  type(foo_t),  allocatable :: rs2
+
+  allocate(val, source=foo_t(42))
+  if (val%a/=a) stop 1
+  rs1 = val
+  if (rs1%a/=a) stop 2
+  rs2 = val
+  if (rs2%a/=a) stop 3
+  deallocate(val, rs1, rs2)
+
+end program main_p