public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kargl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/114024] ICE allocate statement with source=cmp%re and z an array
Date: Wed, 21 Feb 2024 01:31:13 +0000	[thread overview]
Message-ID: <bug-114024-4-kNfM10ZNA1@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-114024-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=114024

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P4

--- Comment #2 from kargl at gcc dot gnu.org ---
This is ugly.  Essentially, the translation of an allocate statement is
mot prepared to have a complex-part-ref as the source expression.  For this
code

program foo
   implicit none
   complex :: cmp(3)
   real, allocatable :: xx(:), yy(:), zz(:)
   cmp = (3.45,6.78)
   allocate (xx, source = cmp%re)       ! ICE
   allocate (yy, source = cmp(1:3)%re)  ! ICE
   allocate (zz, source = (cmp%re))
   print *, xx
   print *, yy
   print *, zz
end

The lines marked with ICE will cause gfortran to, well, ICE.  The
following patch cures the issues by impose a set of parentheses 
about the source expression.  There is likely a better way.

diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index 5247d3d39d7..6ff3f12d7ed 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -6355,8 +6355,24 @@ gfc_trans_allocate (gfc_code * code, gfc_omp_namelist
*omp_allocate)
        vtab_needed = (al->expr->ts.type == BT_CLASS);

       gfc_init_se (&se, NULL);
-      /* When expr3 is a variable, i.e., a very simple expression,
-            then convert it once here.  */
+
+      /* When expr3 is a variable, i.e., a very simple expression, then
+        convert it once here.  Note, if one has source = z%re or z%im, 
+        then things can go sideways with the complex-part-ref, so wrap
+        the entity in parentheses to force evaluation of an expression.
+        That is, the else-branch of the ensuing if-else-stmt is entered.  */
+
+      if (code->expr3->ref
+         && ((code->expr3->ref->u.i == INQUIRY_RE
+              || code->expr3->ref->u.i == INQUIRY_IM)
+             || (code->expr3->ref->type == REF_ARRAY
+                 && code->expr3->ref->u.ar.type == AR_SECTION)))
+       {
+         gfc_expr *etmp = gfc_get_parentheses (code->expr3);
+         code->expr3 = gfc_copy_expr (etmp);
+         gfc_free_expr (etmp);
+       }
+
       if (code->expr3->expr_type == EXPR_VARIABLE
          || code->expr3->expr_type == EXPR_ARRAY
          || code->expr3->expr_type == EXPR_CONSTANT)

  parent reply	other threads:[~2024-02-21  1:31 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-20 21:42 [Bug fortran/114024] New: " kargl at gcc dot gnu.org
2024-02-20 21:57 ` [Bug fortran/114024] " sgk at troutmask dot apl.washington.edu
2024-02-21  1:31 ` kargl at gcc dot gnu.org [this message]
2024-02-21 18:27 ` kargl at gcc dot gnu.org
2024-02-23 21:39 ` cvs-commit at gcc dot gnu.org
2024-03-06 21:03 ` cvs-commit at gcc dot gnu.org
2024-03-06 21:08 ` anlauf at gcc dot gnu.org

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=bug-114024-4-kNfM10ZNA1@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@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).