public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r11-9516] Fortran: fix simplification of TRANSFER for zero-sized character array result
Date: Wed, 26 Jan 2022 18:16:58 +0000 (GMT)	[thread overview]
Message-ID: <20220126181658.2353F385F00C@sourceware.org> (raw)

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

commit r11-9516-gc3251374af4b82888b6be3eb9cfa6b0b3944907b
Author: Harald Anlauf <anlauf@gmx.de>
Date:   Thu Jan 20 22:36:50 2022 +0100

    Fortran: fix simplification of TRANSFER for zero-sized character array result
    
    gcc/fortran/ChangeLog:
    
            PR fortran/104127
            * simplify.c (gfc_simplify_transfer): Ensure that the result
            typespec is set up for TRANSFER with MOLD of type CHARACTER
            including character length even if the result is a zero-sized
            array.
    
    gcc/testsuite/ChangeLog:
    
            PR fortran/104127
            * gfortran.dg/transfer_simplify_11.f90: Fix logic.
            * gfortran.dg/transfer_simplify_13.f90: New test.
    
    (cherry picked from commit 6c1a93102b41a558f3ad49a7c66015257535c747)

Diff:
---
 gcc/fortran/simplify.c                             | 13 ++++++++-
 gcc/testsuite/gfortran.dg/transfer_simplify_11.f90 |  2 +-
 gcc/testsuite/gfortran.dg/transfer_simplify_13.f90 | 34 ++++++++++++++++++++++
 3 files changed, 47 insertions(+), 2 deletions(-)

diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c
index 664fac65a0b..4c9dccb90e3 100644
--- a/gcc/fortran/simplify.c
+++ b/gcc/fortran/simplify.c
@@ -8152,7 +8152,18 @@ gfc_simplify_transfer (gfc_expr *source, gfc_expr *mold, gfc_expr *size)
      set even for array expressions, in order to pass this information into
      gfc_target_interpret_expr.  */
   if (result->ts.type == BT_CHARACTER && gfc_is_constant_expr (mold_element))
-    result->value.character.length = mold_element->value.character.length;
+    {
+      result->value.character.length = mold_element->value.character.length;
+
+      /* Let the typespec of the result inherit the string length.
+	 This is crucial if a resulting array has size zero.  */
+      if (mold_element->ts.u.cl->length)
+	result->ts.u.cl->length = gfc_copy_expr (mold_element->ts.u.cl->length);
+      else
+	result->ts.u.cl->length =
+	  gfc_get_int_expr (gfc_charlen_int_kind, NULL,
+			    mold_element->value.character.length);
+    }
 
   /* Set the number of elements in the result, and determine its size.  */
 
diff --git a/gcc/testsuite/gfortran.dg/transfer_simplify_11.f90 b/gcc/testsuite/gfortran.dg/transfer_simplify_11.f90
index 0911f9dba3a..409e4768a10 100644
--- a/gcc/testsuite/gfortran.dg/transfer_simplify_11.f90
+++ b/gcc/testsuite/gfortran.dg/transfer_simplify_11.f90
@@ -4,5 +4,5 @@
    integer, parameter :: N = 2
    character(len=1) :: chr(N)
    chr = transfer(repeat("x",ncopies=N),[character(len=1) ::], N)
-   if (chr(1) /= 'x' .and. chr(2) /= 'x') STOP 1
+   if (chr(1) /= 'x' .or. chr(2) /= 'x') STOP 1
 end
diff --git a/gcc/testsuite/gfortran.dg/transfer_simplify_13.f90 b/gcc/testsuite/gfortran.dg/transfer_simplify_13.f90
new file mode 100644
index 00000000000..bafb046afd4
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/transfer_simplify_13.f90
@@ -0,0 +1,34 @@
+! { dg-do compile }
+! { dg-options "-fdump-tree-original" }
+! PR fortran/104127 - ICE in get_array_charlen
+! Contributed by G.Steinmetz
+
+program p
+  character(4) :: mold = "XYZ"
+  integer      :: i = 0
+  integer, parameter :: l1 = len  (transfer('ab', 'xyz', size=0))
+  integer, parameter :: s1 = size (transfer('ab', 'xyz', size=0))
+  integer, parameter :: l4 = len  (transfer(4_'abcd', 4_'xy', size=0))
+  integer, parameter :: s4 = size (transfer(4_'abcd', 4_'xy', size=0))
+  integer, parameter :: l2 = len  (transfer('ab', mold,  size=0))
+  integer, parameter :: l3 = len  (transfer('ab', mold,  size=1))
+  integer, parameter :: l5 = len  (transfer('ab',['xyz'], size=0))
+  integer, parameter :: s5 = size (transfer('ab',['xyz'], size=0))
+  call sub0 ( transfer('a', 'y', size=0) )
+  call sub1 ([transfer('a', 'y', size=0)])
+  call sub2 ([transfer('a',['y'],size=0)])
+  call sub3 ( transfer('a', 'y', size=1) )
+  call sub4 ([transfer('a', 'y', size=1)])
+  call sub5 ( transfer('a', 'y', size=i) )
+  call sub6 ( transfer(1_'abcd', 1_'xy' , size=0))
+  call sub7 ( transfer(1_'abcd',[1_'xy'], size=0))
+  call sub8 ( transfer(4_'abcd', 4_'xy' , size=0))
+  call sub9 ( transfer(4_'abcd',[4_'xy'], size=0))
+  print *, transfer('abcd', 'xy', size=0)
+  if (l1 /= 3 .or. s1 /= 0) stop 1
+  if (l4 /= 2 .or. s4 /= 0) stop 2
+  if (l2 /= 4 .or. l3 /= 4) stop 3
+  if (l5 /= 3 .or. s5 /= 0) stop 4
+end
+
+! { dg-final { scan-tree-dump-not "_gfortran_stop_numeric" "original" } }


                 reply	other threads:[~2022-01-26 18:16 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220126181658.2353F385F00C@sourceware.org \
    --to=anlauf@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).