Hi all,

This patch fixes r16-4474 regression.
Reproduce: https://godbolt.org/z/8sqGonnza
The problem: _gfortran_transfer_integer_write (&dt_parm.0, >- &1 -<, 4);

This approach clashed with the coarray test case where a variable is
created and then pointed to. Following Andre's advice that taking the
address of a constant is problematic, I've switched to using the
variable approach. Existing test cases pass with this change.
I've noted some instances where we currently use build_addr on the fly
when an object is not a pointer. Now that conv_constant respects the
want_pointer flag, we can gradually clean up these occurrences and
avoid introducing new ones.
Sorry for the regression. I would appreciate it if you could help
double-test the attached patch to confirm it works as expected.

Yuao
From 76f5f53005a8f5a004e7762a63197e1e77d78401 Mon Sep 17 00:00:00 2001
From: Yuao Ma <[email protected]>
Date: Tue, 21 Oct 2025 23:34:21 +0800
Subject: [PATCH] fortran: revise non-char pointer handling in
 gfc_conv_constant

gcc/fortran/ChangeLog:

        * trans-const.cc (gfc_conv_constant):
        Create a variable for the non-char pointer.
---
 gcc/fortran/trans-const.cc | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/gcc/fortran/trans-const.cc b/gcc/fortran/trans-const.cc
index f70f36284a3..09b7eb27e23 100644
--- a/gcc/fortran/trans-const.cc
+++ b/gcc/fortran/trans-const.cc
@@ -392,6 +392,7 @@ void
 gfc_conv_constant (gfc_se * se, gfc_expr * expr)
 {
   gfc_ss *ss;
+  tree value;
 
   /* We may be receiving an expression for C_NULL_PTR or C_NULL_FUNPTR.  If
      so, the expr_type will not yet be an EXPR_CONSTANT.  We need to make
@@ -444,6 +445,11 @@ gfc_conv_constant (gfc_se * se, gfc_expr * expr)
       if (expr->ts.type == BT_CHARACTER)
        gfc_conv_string_parameter (se);
       else
-       se->expr = gfc_build_addr_expr (NULL_TREE, se->expr);
+       {
+         value = gfc_create_var (TREE_TYPE (se->expr), "value");
+         gfc_add_modify (&se->pre, value,
+                         fold_convert (TREE_TYPE (value), se->expr));
+         se->expr = gfc_build_addr_expr (NULL_TREE, value);
+       }
     }
 }
-- 
2.43.0

Reply via email to