https://gcc.gnu.org/g:114850c8aef6e266b8f08be10ff417d41ad68592

commit 114850c8aef6e266b8f08be10ff417d41ad68592
Author: Mikael Morin <mik...@gcc.gnu.org>
Date:   Tue Jul 15 21:49:27 2025 +0200

    Extraction gfc_init_absent_descriptor
    
    Correction gfc_clear_descriptor assumed rank
    
    Correction partielle class_optional_2
    
    Correction class_optional_2

Diff:
---
 gcc/fortran/trans-descriptor.cc | 31 +++++++++++++++++++++++++++----
 gcc/fortran/trans-descriptor.h  |  2 +-
 gcc/fortran/trans-expr.cc       |  5 ++---
 3 files changed, 30 insertions(+), 8 deletions(-)

diff --git a/gcc/fortran/trans-descriptor.cc b/gcc/fortran/trans-descriptor.cc
index 26fd6ba4fcf8..e3762d70bb36 100644
--- a/gcc/fortran/trans-descriptor.cc
+++ b/gcc/fortran/trans-descriptor.cc
@@ -670,15 +670,15 @@ gfc_get_descriptor_offsets_for_info (const_tree 
desc_type, tree *data_off,
 #undef LBOUND_SUBFIELD
 #undef UBOUND_SUBFIELD
 
-
 void
-gfc_init_descriptor_variable (stmtblock_t *block, gfc_symbol *sym, tree descr)
+gfc_init_descriptor_variable (stmtblock_t *block, gfc_symbol *sym, gfc_expr 
*expr, tree descr)
 {
   symbol_attribute attr = gfc_symbol_attr (sym);
 
   /* NULLIFY the data pointer for non-saved allocatables, or for non-saved
      pointers when -fcheck=pointer is specified.  */
   if (attr.allocatable
+      || attr.optional
       || (attr.pointer && (gfc_option.rtcheck & GFC_RTCHECK_POINTER)))
     {
       gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
@@ -694,10 +694,26 @@ gfc_init_descriptor_variable (stmtblock_t *block, 
gfc_symbol *sym, tree descr)
   else
     as = sym->as;
 
-  gcc_assert (as && as->rank >= 0);
+  int rank;
+  if (as == nullptr)
+    rank = 0;
+  else if (as->type != AS_ASSUMED_RANK)
+    rank = as->rank;
+  else if (expr)
+    rank = expr->rank;
+  else
+    rank = -1;
+
   etype = gfc_get_element_type (TREE_TYPE (descr));
   gfc_conv_descriptor_dtype_set (block, descr,
-                                gfc_get_dtype_rank_type (as->rank, etype));
+                                gfc_get_dtype_rank_type (rank, etype));
+}
+
+
+void
+gfc_init_descriptor_variable (stmtblock_t *block, gfc_symbol *sym, tree descr)
+{
+  return gfc_init_descriptor_variable (block, sym, nullptr, descr);
 }
 
 
@@ -748,3 +764,10 @@ gfc_build_default_class_descriptor (const gfc_typespec 
&ts, tree class_type)
   return gfc_class_set_static_fields (class_type, vptr, tmp);
 }
 
+
+void
+gfc_init_absent_descriptor (stmtblock_t *block, tree descr)
+{
+  gfc_conv_descriptor_data_set (block, descr, null_pointer_node);
+}
+
diff --git a/gcc/fortran/trans-descriptor.h b/gcc/fortran/trans-descriptor.h
index f28565d783ee..de57a8e606e8 100644
--- a/gcc/fortran/trans-descriptor.h
+++ b/gcc/fortran/trans-descriptor.h
@@ -20,7 +20,6 @@ along with GCC; see the file COPYING3.  If not see
 #define GFC_TRANS_DESCRIPTOR_H
 
 /* Build a null array descriptor constructor.  */
-tree gfc_build_null_descriptor (tree);
 tree gfc_build_default_class_descriptor (const gfc_typespec &, tree);
 void gfc_nullify_descriptor (stmtblock_t *block, gfc_expr *, tree);
 void gfc_set_scalar_null_descriptor (stmtblock_t *block, tree, gfc_symbol *, 
gfc_expr *, tree);
@@ -97,5 +96,6 @@ gfc_get_descriptor_offsets_for_info (const_tree desc_type, 
tree *data_off,
 void gfc_init_descriptor_variable (stmtblock_t *block, gfc_symbol *sym, tree 
descr);
 void gfc_init_descriptor_result (stmtblock_t *block, tree descr);
 void gfc_init_static_descriptor (tree descr);
+void gfc_init_absent_descriptor (stmtblock_t *block, tree descr);
 
 #endif /* GFC_TRANS_DESCRIPTOR_H */
diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 39e953fa2af3..1e345c1ee9f6 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -978,10 +978,9 @@ gfc_conv_derived_to_class (gfc_se *parmse, gfc_expr *e, 
gfc_symbol *fsym,
              tmp = gfc_finish_block (&block);
 
              gfc_init_block (&block);
-             gfc_conv_descriptor_data_set (&block, ctree, null_pointer_node);
+             gfc_init_absent_descriptor (&block, ctree);
              if (derived_array && *derived_array != NULL_TREE)
-               gfc_conv_descriptor_data_set (&block, *derived_array,
-                                             null_pointer_node);
+               gfc_init_absent_descriptor (&block, *derived_array);
 
              tmp = build3_v (COND_EXPR, cond_optional, tmp,
                              gfc_finish_block (&block));

Reply via email to