[gcc r17-6977] fortran: array descriptor: Extract class container with its data pointer [PR122521]

Mikael Morin mikael@gcc.gnu.org
Tue Dec 1 11:14:45 GMT 2026


https://gcc.gnu.org/g:e9e8f7221c80ab13e3f66202b48ea313d2c56ac7

commit r17-6977-ge9e8f7221c80ab13e3f66202b48ea313d2c56ac7
Author: Mikael Morin <mikael@gcc.gnu.org>
Date:   Tue Dec 1 11:54:43 2026 +0100

    fortran: array descriptor: Extract class container with its data pointer [PR122521]
    
    In gfc_set_descriptor_from_scalar_class, use a single walk over the input
    reference to extract both the class container and the pointer to the
    underlying data.
    
    This is a follow-up to the previous change.  That change introduced the walk
    of the input reference to extract the class container, but it was ignoring
    the change before, that independently checked whether the input reference
    was the class container, to produce a reference to its _data.  This
    integrates the latter into the walk of the input reference.
    
    The test is a case that isn't covered by the testsuite and failed silently
    with a previous iteration of the patch.
    
            PR fortran/122521
    
    gcc/fortran/ChangeLog:
    
            * trans-descriptor.cc (is_polymorphic_ref): Add argument valptr_ref.
            Set it to the value pointer reference found during the walk, if such
            a reference was found.  Otherwise set it to a new reference to the
            _data field of the class container reference.
            (gfc_set_descriptor_from_scalar_class): Add a valptr_ref variable
            and pass it to is_polymorphic_ref.  Don't check the input reference
            type and don't create a new reference to the _data from the input.
            Directly use the reference returned by is_polymorphic_ref instead.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/assumed_rank_27.f90: New test.

Diff:
---
 gcc/fortran/trans-descriptor.cc               | 66 +++++++++++++++++++++------
 gcc/testsuite/gfortran.dg/assumed_rank_27.f90 | 39 ++++++++++++++++
 2 files changed, 91 insertions(+), 14 deletions(-)

diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index fa599f3a5c4b..3f22789548e6 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -907,11 +907,18 @@ gfc_set_descriptor_from_scalar (stmtblock_t *block, tree descr, tree scalar)
 
 /* Return true if EXPR is a polymorphic reference; otherwise return false.  If
    CLASS_REF isn't nullptr, set its target to a reference to the class container
-   in the true case, and leave it unmodified in the false case.  */
+   in the true case, and leave it unmodified in the false case.  If VAL_PTR
+   isn't nullptr, set it to a pointer to the actual data wrapped by the class
+   container similarly.  */
 
 static bool
-is_polymorphic_ref (tree expr, tree *class_ref = nullptr)
+is_polymorphic_ref (tree expr, tree *class_ref = nullptr,
+		    tree *val_ptr = nullptr)
 {
+  tree valptr_candidate = NULL_TREE;
+  tree class_pointer = NULL_TREE;
+  tree class_container = NULL_TREE;
+
   tree e = expr;
   while (true)
     {
@@ -921,15 +928,13 @@ is_polymorphic_ref (tree expr, tree *class_ref = nullptr)
       else if (POINTER_TYPE_P (TREE_TYPE (e))
 	       && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (e))))
 	{
-	  if (class_ref != nullptr)
-	    *class_ref = build_fold_indirect_ref_loc (input_location, e);
-	  return true;
+	  class_pointer = e;
+	  break;
 	}
       else if (GFC_CLASS_TYPE_P (TREE_TYPE (e)))
 	{
-	  if (class_ref != nullptr)
-	    *class_ref = e;
-	  return true;
+	  class_container = e;
+	  break;
 	}
       else if (TREE_CODE (e) == COMPONENT_REF)
 	{
@@ -947,8 +952,44 @@ is_polymorphic_ref (tree expr, tree *class_ref = nullptr)
 		 || TREE_CODE (e) == POINTER_PLUS_EXPR))
 	return false;
 
+      if (valptr_candidate == nullptr
+	  && TREE_CODE (e) != INDIRECT_REF
+	  && !(GFC_CLASS_TYPE_P (TREE_TYPE (e))
+	       || (POINTER_TYPE_P (TREE_TYPE (e))
+		   && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (e))))))
+	valptr_candidate = e;
+
       e = TREE_OPERAND (e, 0);
     }
+
+  gcc_assert (class_container != nullptr || class_pointer != nullptr);
+
+  bool valptr_from_class_container = false;
+  if (val_ptr != nullptr)
+    {
+      if (valptr_candidate != nullptr)
+	{
+	  if (POINTER_TYPE_P (TREE_TYPE (valptr_candidate)))
+	    *val_ptr = valptr_candidate;
+	  else
+	    *val_ptr = gfc_build_addr_expr (NULL_TREE, valptr_candidate);
+	}
+      else
+	valptr_from_class_container = true;
+    }
+
+  if (class_container == nullptr
+      && (class_ref != nullptr
+	  || valptr_from_class_container))
+    class_container = build_fold_indirect_ref_loc (input_location,
+						   class_pointer);
+  if (class_ref != nullptr)
+    *class_ref = class_container;
+
+  if (valptr_from_class_container)
+    *val_ptr = gfc_class_data_get (class_container);
+
+  return true;
 }
 
 
@@ -962,16 +1003,13 @@ gfc_set_descriptor_from_scalar_class (stmtblock_t *block, tree descr,
 				      tree scalar)
 {
   tree class_ref = nullptr;
-  bool is_class = is_polymorphic_ref (scalar, &class_ref);
+  tree val_ptr = nullptr;
+  bool is_class = is_polymorphic_ref (scalar, &class_ref, &val_ptr);
   gcc_assert (is_class);
   tree dtype_val = gfc_get_dtype_rank_type (0, TREE_TYPE (class_ref));
   gfc_conv_descriptor_dtype_set (block, descr, dtype_val);
 
-  tree tmp = scalar;
-  if (GFC_CLASS_TYPE_P (TREE_TYPE (tmp))
-      || (POINTER_TYPE_P (TREE_TYPE (tmp))
-	  && GFC_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (tmp)))))
-    tmp = gfc_class_data_get (tmp);
+  tree tmp = val_ptr;
   if (!POINTER_TYPE_P (TREE_TYPE (tmp)))
     tmp = gfc_build_addr_expr (NULL_TREE, tmp);
 
diff --git a/gcc/testsuite/gfortran.dg/assumed_rank_27.f90 b/gcc/testsuite/gfortran.dg/assumed_rank_27.f90
new file mode 100644
index 000000000000..a3574c70baae
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/assumed_rank_27.f90
@@ -0,0 +1,39 @@
+! { dg-do run }
+!
+! Check that scalar values are correctly passed to polymorphic assumed-rank
+! dummy arguments.
+
+program prog
+   implicit none
+   integer, parameter :: n = 5
+   type :: t
+      integer :: c
+   end type
+   class(t), allocatable :: a(:)
+   integer :: i
+   allocate(a(n))
+   do i=1,n
+      a(i)%c = i*i
+   end do
+   call sub(a(2),  4, 1)
+   call sub(a(3),  9, 2)
+   call sub(a(5), 25, 3)
+contains
+   subroutine sub(x, y, e)
+      class(t), intent(in) :: x(..)
+      integer, intent(in) :: y, e
+      select rank(x)
+      rank (0)
+         call check(x%c, y, e*10+1)
+      rank default
+         call check(rank(x), 0, e*10+2)
+      end select
+   end subroutine
+   subroutine check(x, y, e)
+      integer, intent(in) :: x, y, e
+      if (x /= y) then
+         print *, e, "FAIL", x, y
+         error stop e
+      end if
+   end subroutine
+end program prog


More information about the Gcc-cvs mailing list