From e4cb3bb9ac11b4126ffa718287dd509a4b10a658 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Tue, 31 Aug 2021 21:00:53 +0200 Subject: [PATCH] Fortran - extend set of substring expressions handled in length simplification gcc/fortran/ChangeLog: PR fortran/100950 * simplify.c (substring_has_constant_len): Minimize checks for substring expressions being allowed. gcc/testsuite/ChangeLog: PR fortran/100950 * gfortran.dg/pr100950.f90: Extend coverage. --- gcc/fortran/simplify.c | 31 ++----------------------------- gcc/testsuite/gfortran.dg/pr100950.f90 | 12 ++++++++++++ 2 files changed, 14 insertions(+), 29 deletions(-) diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c index 4cb73e8..b46cbfa 100644 --- a/gcc/fortran/simplify.c +++ b/gcc/fortran/simplify.c @@ -4533,14 +4533,7 @@ substring_has_constant_len (gfc_expr *e) || !ref->u.ss.start || ref->u.ss.start->expr_type != EXPR_CONSTANT || !ref->u.ss.end - || ref->u.ss.end->expr_type != EXPR_CONSTANT - || !ref->u.ss.length) - return false; - - /* For non-deferred strings the given length shall be constant. */ - if (!e->ts.deferred - && (!ref->u.ss.length->length - || ref->u.ss.length->length->expr_type != EXPR_CONSTANT)) + || ref->u.ss.end->expr_type != EXPR_CONSTANT) return false; /* Basic checks on substring starting and ending indices. */ @@ -4551,27 +4544,7 @@ substring_has_constant_len (gfc_expr *e) iend = gfc_mpz_get_hwi (ref->u.ss.end->value.integer); if (istart <= iend) - { - if (istart < 1) - { - gfc_error ("Substring start index (%wd) at %L below 1", - istart, &ref->u.ss.start->where); - return false; - } - - /* For deferred strings use end index as proxy for length. */ - if (e->ts.deferred) - length = iend; - else - length = gfc_mpz_get_hwi (ref->u.ss.length->length->value.integer); - if (iend > length) - { - gfc_error ("Substring end index (%wd) at %L exceeds string length", - iend, &ref->u.ss.end->where); - return false; - } - length = iend - istart + 1; - } + length = iend - istart + 1; else length = 0; diff --git a/gcc/testsuite/gfortran.dg/pr100950.f90 b/gcc/testsuite/gfortran.dg/pr100950.f90 index cb9d126..a19409c 100644 --- a/gcc/testsuite/gfortran.dg/pr100950.f90 +++ b/gcc/testsuite/gfortran.dg/pr100950.f90 @@ -46,6 +46,18 @@ program p integer, parameter :: l9 = len (r(1)%u(:)(3:4)) if (l9 /= 2) stop 13 end block + + call sub (42, "abcde") +contains + subroutine sub (m, c) + integer, intent(in) :: m + character(len=*), intent(in) :: c + character(len=m) :: p, o(3) + integer, parameter :: l10 = len (p(6:7)) + integer, parameter :: l11 = len (o(:)(6:7)) + integer, parameter :: l12 = len (c(2:3)) + if (l10 /= 2 .or. l11 /= 2 .or. l12 /= 2) stop 14 + end subroutine sub end ! { dg-final { scan-tree-dump-times "_gfortran_stop_numeric" 2 "original" } } -- 2.7.4