https://gcc.gnu.org/g:2e0087de645e3fd3629dc8976f4aed70e664d287

commit r17-6550-g2e0087de645e3fd3629dc8976f4aed70e664d287
Author: Paul Thomas <[email protected]>
Date:   Fri Nov 20 09:59:53 2026 +0000

    Fortran: Invalid memory reference with class PDT intent out [PR128886]
    
    2026-11-29  Paul Thomas  <[email protected]>
    
    gcc/fortran
            PR fortran/128886
            * trans-expr.cc (gfc_trans_class_array_init_assign): The check
            for a NULL rhs is needed for class PDTs as well as unlimited
            polymorphis expressions.
    
    gcc/testsuite/
            PR fortran/128886
            * gfortran.dg/pdt_bug31.f90: New test.

Diff:
---
 gcc/fortran/trans-expr.cc               |  2 +-
 gcc/testsuite/gfortran.dg/pdt_bug31.f90 | 66 +++++++++++++++++++++++++++++++++
 2 files changed, 67 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 390089b45edb..695afaec2255 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -1791,7 +1791,7 @@ gfc_trans_class_array_init_assign (gfc_expr *rhs, 
gfc_expr *lhs, gfc_expr *obj)
   res = gfc_trans_call (ppc_code, false, NULL, NULL, false);
   gfc_free_statements (ppc_code);
 
-  if (UNLIMITED_POLY(obj))
+  if (UNLIMITED_POLY(obj) || IS_CLASS_PDT (obj))
     {
       /* Check if rhs is non-NULL. */
       gfc_se src;
diff --git a/gcc/testsuite/gfortran.dg/pdt_bug31.f90 
b/gcc/testsuite/gfortran.dg/pdt_bug31.f90
new file mode 100644
index 000000000000..71fa758d30dd
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/pdt_bug31.f90
@@ -0,0 +1,66 @@
+! { dg-do run }
+!
+! CLASS PDT dummy arrays: Primary problem was a repeated typo in trans-decl.cc.
+!
+! Subroutine 's_out' needed special attention because the vptr default 
initializer
+! was NULL.
+!
+! Contributed by Jerry Delisle  <[email protected]>
+!
+module m
+  implicit none
+  type t(n)
+    integer, len :: n = 2
+    integer :: x(n)
+    integer :: tag = 42
+  end type
+contains
+  subroutine s_shape (a)
+    class(t(n=3)), intent(in) :: a(:)
+    if (sum (a%x(2)) /= 7) stop 1
+  end subroutine
+  subroutine s_expl (a)
+    class(t(n=3)), intent(in) :: a(2)
+    if (sum (a(1)%x) /= 6) stop 2
+  end subroutine
+  subroutine s_size (a)
+    class(t(n=3)), intent(in) :: a(*)
+    if (sum (a(2)%x) /= 15) stop 3
+  end subroutine
+  subroutine s_rank (a, i)
+    class(t(n=3)), intent(in) :: a(..)
+    integer, intent(in) :: i
+    if (rank (a) /= i) stop 4
+  end subroutine
+  subroutine s_alloc (a)
+    class(t(n=:)), allocatable, intent(inout) :: a(:)
+    if (.not. allocated (a)) allocate (t(n=3) :: a(2))
+    a(1)%x = [11, 12, 13]
+    a(2)%x = [14, 15, 16]
+  end subroutine
+  subroutine s_out (a)
+    class(t(n=3)), intent(out) :: a(:)
+    if ((size (a, 1) /= 2) .or. &            ! Dimension of a retained
+        (sum (a(1)%x) /= 36) .or. &          ! Size and contents of pdt_array 
retained
+        a(2)%tag /= 42) stop 5               ! Not pdt component set to 
initalizer.
+  end subroutine
+end module
+
+  use m
+  class(t(n=3)), allocatable :: x(:), y(:,:)
+  allocate (x(2))
+  x(1)%x = [1, 2, 3]
+  x(2)%x = [4, 5, 6]
+  x%tag = 1
+  call s_shape (x)
+  call s_expl (x)
+  call s_size (x)
+  call s_rank (x, 1)
+  call s_rank (y, 2)
+  deallocate (x)
+  call s_alloc (x)
+  if (sum (x(1)%x) + sum (x(2)%x) /= 81) stop 6
+  x%tag = 1
+  call s_out (x)
+  if (allocated (x)) deallocate (x)
+end

Reply via email to