When associating a variable of type character, if the length of the
target isn't known, set it to zero rather than leaving it unset.  This
is not a complete fix for making associate of characters work
properly, but papers over an ICE in the middle-end. See PR 83344 for
more details.

Regtested on x86_64-pc-linux-gnu, Ok for trunk?

gcc/fortran/ChangeLog:

2018-01-23  Janne Blomqvist  <j...@gcc.gnu.org>

        PR 83975
        PR 83344
        * resolve.c (resolve_assoc_var): Set character length to zero if
        target length unknown.

gcc/testsuite/ChangeLog:

2018-01-23  Janne Blomqvist  <j...@gcc.gnu.org>

        PR 83975
        PR 83344
        * gfortran.dg/associate_31.f90: New test.
---
 gcc/fortran/resolve.c                      | 13 ++++++++-----
 gcc/testsuite/gfortran.dg/associate_31.f90 |  8 ++++++++
 2 files changed, 16 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/associate_31.f90

diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c
index a2b892a..f3fc3ca 100644
--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -8634,11 +8634,14 @@ 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.u.cl->length && !sym->ts.deferred
-         && target->expr_type == EXPR_CONSTANT)
-       sym->ts.u.cl->length
-         = gfc_get_int_expr (gfc_charlen_int_kind,
-                             NULL, target->value.character.length);
+      if (!sym->ts.u.cl->length && !sym->ts.deferred)
+       /* TODO: Setting the length to zero if the expression isn't
+          constant is probably not correct, but at this point we
+          don't have any better idea what it should be either.  */
+       sym->ts.u.cl->length =
+         gfc_get_int_expr (gfc_charlen_int_kind, NULL,
+                           target->expr_type == EXPR_CONSTANT ?
+                           target->value.character.length : 0);
     }
 
   /* If the target is a good class object, so is the associate variable.  */
diff --git a/gcc/testsuite/gfortran.dg/associate_31.f90 
b/gcc/testsuite/gfortran.dg/associate_31.f90
new file mode 100644
index 0000000..75774c8
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/associate_31.f90
@@ -0,0 +1,8 @@
+! { dg-do compile }
+! PR 83975, see also PR 83344
+! Testcase contributed by Gerhard Steinmetz
+subroutine s(x)
+  character(*) :: x
+  associate (y => x)
+  end associate
+end subroutine s
-- 
2.7.4

Reply via email to