https://gcc.gnu.org/g:0c52564d1e52002527d0663a77d8d06a70e5c551

commit 0c52564d1e52002527d0663a77d8d06a70e5c551
Author: Mikael Morin <mik...@gcc.gnu.org>
Date:   Thu Jul 24 21:23:52 2025 +0200

    fortran: Consistently use the same assignment reallocation condition
    
    This is a follow-up to:
    r16-2248-gac8e536526393580bc9a4339bab2f8603eff8a47
    fortran: Delay evaluation of array bounds after reallocation
    
    That revision delayed the evaluation of array bounds, with changes in
    two places: in the scalarizer where we save expressions without
    evaluating their values to variables, and in the reallocation code where
    we evaluate to variables the expressions previously saved.  The effect
    should not be visible in scalarized code, as the saving to a variable is
    still done, only in a different place.
    
    Unfortunately, it's actually not the case, and there are cases where
    values are saved to variables before the change, but not after it.  This
    is not a correctness issue, but PR fortran/121185, that made the problem
    apparent, exhibited examples where the lack of an intermediary variable
    was making visible a parent class container reference, causing a
    non- polymorphic array reference to be evaluated in a polymorphic way
    with the virtual table from the parent class container, so exposing a
    wrong-code bug.
    
    The reason for the lack of saving of the variable is differing
    conditions guarding the omission of the evaluation to variables in the
    scalarizer on one hand, and the emission of reallocation code with the
    saving to variables on the other hand.  There is an additional check
    that avoids the emission of reallocation code if we can prove at compile
    time that both sides of the assignment are conformable.
    
    This change moves up the reallocation code condition definition, so
    that it can be used as well to flag the left hand side array as
    reallocatable.
    
            PR fortran/121185
    
    gcc/fortran/ChangeLog:
    
            * trans-expr.cc (gfc_trans_assignment_1): Use the same condition
            to set the is_alloc_lhs flag and to decide to generate
            reallocation code.

Diff:
---
 gcc/fortran/trans-expr.cc | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc
index 7c7621571ad0..0b32a609427f 100644
--- a/gcc/fortran/trans-expr.cc
+++ b/gcc/fortran/trans-expr.cc
@@ -12871,9 +12871,14 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * 
expr2, bool init_flag,
   gfc_init_se (&lse, NULL);
   gfc_init_se (&rse, NULL);
 
+  realloc_flag = flag_realloc_lhs
+                && gfc_is_reallocatable_lhs (expr1)
+                && expr2->rank
+                && !is_runtime_conformable (expr1, expr2);
+
   /* Walk the lhs.  */
   lss = gfc_walk_expr (expr1);
-  if (gfc_is_reallocatable_lhs (expr1))
+  if (realloc_flag)
     {
       lss->no_bounds_check = 1;
       lss->is_alloc_lhs = 1;
@@ -12924,11 +12929,6 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * 
expr2, bool init_flag,
 
   assoc_assign = is_assoc_assign (expr1, expr2);
 
-  realloc_flag = flag_realloc_lhs
-                && gfc_is_reallocatable_lhs (expr1)
-                && expr2->rank
-                && !is_runtime_conformable (expr1, expr2);
-
   /* Only analyze the expressions for coarray properties, when in coarray-lib
      mode.  Avoid false-positive uninitialized diagnostics with initializing
      the codimension flag unconditionally.  */

Reply via email to