]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix gimplification error on assignment to pointer [PR103391]
authorAndre Vehreschild <vehre@gcc.gnu.org>
Tue, 4 Mar 2025 11:56:20 +0000 (12:56 +0100)
committerAndre Vehreschild <vehre@gcc.gnu.org>
Tue, 4 Mar 2025 15:14:15 +0000 (16:14 +0100)
PR fortran/103391

gcc/fortran/ChangeLog:

* trans-expr.cc (gfc_trans_assignment_1): Do not use poly assign
for pointer arrays on lhs (as it is done for allocatables
already).

gcc/testsuite/ChangeLog:

* gfortran.dg/assign_12.f90: New test.

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

index 0d790b63f956139799d4df490898a66d484c0a9b..fbe7333fd711f071413b04b37da40f8b87098862 100644 (file)
@@ -12876,14 +12876,14 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag,
      needed.  */
   lhs_attr = gfc_expr_attr (expr1);
 
-  is_poly_assign = (use_vptr_copy || lhs_attr.pointer
-                   || (lhs_attr.allocatable && !lhs_attr.dimension))
-                  && (expr1->ts.type == BT_CLASS
-                      || gfc_is_class_array_ref (expr1, NULL)
-                      || gfc_is_class_scalar_expr (expr1)
-                      || gfc_is_class_array_ref (expr2, NULL)
-                      || gfc_is_class_scalar_expr (expr2))
-                  && lhs_attr.flavor != FL_PROCEDURE;
+  is_poly_assign
+    = (use_vptr_copy
+       || ((lhs_attr.pointer || lhs_attr.allocatable) && !lhs_attr.dimension))
+      && (expr1->ts.type == BT_CLASS || gfc_is_class_array_ref (expr1, NULL)
+         || gfc_is_class_scalar_expr (expr1)
+         || gfc_is_class_array_ref (expr2, NULL)
+         || gfc_is_class_scalar_expr (expr2))
+      && lhs_attr.flavor != FL_PROCEDURE;
 
   assoc_assign = is_assoc_assign (expr1, expr2);
 
diff --git a/gcc/testsuite/gfortran.dg/assign_12.f90 b/gcc/testsuite/gfortran.dg/assign_12.f90
new file mode 100644 (file)
index 0000000..be31021
--- /dev/null
@@ -0,0 +1,28 @@
+!{ dg-do run }
+!
+! Check assignment works for derived types to memory referenced by pointer
+! Contributed by G. Steinmetz  <gscfq@t-online.de>
+
+program pr103391
+   type t
+     character(1) :: c
+   end type
+   type t2
+      type(t), pointer :: a(:)
+   end type
+
+   type(t), target :: arr(2)
+   type(t2) :: r
+
+   arr = [t('a'), t('b')]
+
+   r = f([arr])
+   if (any(r%a(:)%c /= ['a', 'b'])) stop 1
+contains
+   function f(x)
+      class(t), intent(in), target :: x(:)
+      type(t2) :: f
+      allocate(f%a(size(x,1)))
+      f%a = x
+   end
+end