public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Mikael Morin <mikael@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r12-7726] fortran: Separate associate character lengths earlier [PR104570]
Date: Sat, 19 Mar 2022 20:16:59 +0000 (GMT)	[thread overview]
Message-ID: <20220319201659.4B007385840C@sourceware.org> (raw)

https://gcc.gnu.org/g:907811ddc35da6c1701ed22355ece63a8c3ed7fb

commit r12-7726-g907811ddc35da6c1701ed22355ece63a8c3ed7fb
Author: Mikael Morin <mikael@gcc.gnu.org>
Date:   Sun Mar 13 22:22:55 2022 +0100

    fortran: Separate associate character lengths earlier [PR104570]
    
    This change workarounds an ICE in the evaluation of the character length
    of an array expression referencing an associate variable; the code is
    not prepared to see a non-scalar expression as it doesn’t initialize the
    scalarizer.
    
    Before this change, associate length symbols get a new gfc_charlen at
    resolution stage to unshare them from the associate expression, so that
    at translation stage it is a decl specific to the associate symbol that
    is initialized, not the decl of some other symbol.  This
    reinitialization of gfc_charlen happens after expressions referencing
    the associate symbol have been parsed, so that those expressions retain
    the original gfc_charlen they have copied from the symbol.
    At translation stage, the gfc_charlen for the associate symbol is setup
    with the decl holding the actual length value, but the expressions have
    retained the original gfc_charlen without any decl.  So they need to
    evaluate the character length, and this is where the ICE happens.
    
    This change moves the reinitialization of gfc_charlen earlier at parsing
    stage, so that at resolution stage the gfc_charlen can be retained as
    it’s already not shared with any other symbol, and the expressions which
    now share their gfc_charlen with the symbol are automatically updated
    when the length decl is setup at translation stage.  There is no need
    any more to evaluate the character length as it has all the required
    information, and the ICE doesn’t happen.
    
    The first resolve.cc hunk is necessary to avoid regressing on the
    associate_35.f90 testcase.
    
            PR fortran/104228
            PR fortran/104570
    
    gcc/fortran/ChangeLog:
    
            * parse.cc (parse_associate): Use a new distinct gfc_charlen if the
            copied type has one whose length is not known to be constant.
            * resolve.cc (resolve_assoc_var): Reset charlen if it’s shared with
            the associate target regardless of the expression type.
            Don’t reinitialize charlen if it’s deferred.
    
    gcc/testsuite/ChangeLog:
    
            * gfortran.dg/associate_58.f90: New test.

Diff:
---
 gcc/fortran/parse.cc                       | 18 ++++++++++++++++++
 gcc/fortran/resolve.cc                     |  9 ++++++---
 gcc/testsuite/gfortran.dg/associate_58.f90 | 21 +++++++++++++++++++++
 3 files changed, 45 insertions(+), 3 deletions(-)

diff --git a/gcc/fortran/parse.cc b/gcc/fortran/parse.cc
index db918291b10..e6e915d2a5e 100644
--- a/gcc/fortran/parse.cc
+++ b/gcc/fortran/parse.cc
@@ -4924,6 +4924,24 @@ parse_associate (void)
 	 in case of association to a derived-type.  */
       sym->ts = a->target->ts;
 
+      /* Don’t share the character length information between associate
+	 variable and target if the length is not a compile-time constant,
+	 as we don’t want to touch some other character length variable when
+	 we try to initialize the associate variable’s character length
+	 variable.
+	 We do it here rather than later so that expressions referencing the
+	 associate variable will automatically have the correctly setup length
+	 information.  If we did it at resolution stage the expressions would
+	 use the original length information, and the variable a new different
+	 one, but only the latter one would be correctly initialized at
+	 translation stage, and the former one would need some additional setup
+	 there.  */
+      if (sym->ts.type == BT_CHARACTER
+	  && sym->ts.u.cl
+	  && !(sym->ts.u.cl->length
+	       && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT))
+	sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
+
       /* Check if the target expression is array valued.  This cannot always
 	 be done by looking at target.rank, because that might not have been
 	 set yet.  Therefore traverse the chain of refs, looking for the last
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 0b55961f083..5522be75199 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -9232,7 +9232,7 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
       if (!sym->ts.u.cl)
 	sym->ts.u.cl = target->ts.u.cl;
 
-      if (sym->ts.deferred && target->expr_type == EXPR_VARIABLE
+      if (sym->ts.deferred
 	  && sym->ts.u.cl == target->ts.u.cl)
 	{
 	  sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
@@ -9251,8 +9251,11 @@ resolve_assoc_var (gfc_symbol* sym, bool resolve_target)
 		|| sym->ts.u.cl->length->expr_type != EXPR_CONSTANT)
 		&& target->expr_type != EXPR_VARIABLE)
 	{
-	  sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
-	  sym->ts.deferred = 1;
+	  if (!sym->ts.deferred)
+	    {
+	      sym->ts.u.cl = gfc_new_charlen (sym->ns, NULL);
+	      sym->ts.deferred = 1;
+	    }
 
 	  /* This is reset in trans-stmt.cc after the assignment
 	     of the target expression to the associate name.  */
diff --git a/gcc/testsuite/gfortran.dg/associate_58.f90 b/gcc/testsuite/gfortran.dg/associate_58.f90
new file mode 100644
index 00000000000..9c24f35c0d8
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/associate_58.f90
@@ -0,0 +1,21 @@
+! { dg-do compile }
+!
+! PR fortran/104570
+! The following used to cause an ICE because the string length
+! evaluation of the (y) expression was not prepared to handle
+! a non-scalar expression.
+
+program p
+   character(:), allocatable :: x(:)
+   x = ['abc']
+   call s
+contains
+   subroutine s
+      associate (y => x)
+         associate (z => (y))
+            print *, z
+         end associate
+      end associate
+   end
+end
+


                 reply	other threads:[~2022-03-19 20: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=20220319201659.4B007385840C@sourceware.org \
    --to=mikael@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).