From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2071) id B77973858C60; Sat, 4 Dec 2021 21:31:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B77973858C60 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Harald Anlauf To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-9355] Fortran: improve checking of array specifications X-Act-Checkin: gcc X-Git-Author: Harald Anlauf X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 2fdef526a3a8cb4a6f89852979c7ca6437b994f3 X-Git-Newrev: 9e9c674dfc6fae8f967341759699b4d75943d256 Message-Id: <20211204213137.B77973858C60@sourceware.org> Date: Sat, 4 Dec 2021 21:31:37 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Dec 2021 21:31:37 -0000 https://gcc.gnu.org/g:9e9c674dfc6fae8f967341759699b4d75943d256 commit r11-9355-g9e9c674dfc6fae8f967341759699b4d75943d256 Author: Harald Anlauf Date: Thu Dec 2 22:33:49 2021 +0100 Fortran: improve checking of array specifications gcc/fortran/ChangeLog: PR fortran/103505 * array.c (match_array_element_spec): Try to simplify array element specifications to improve early checking. * expr.c (gfc_try_simplify_expr): New. Try simplification of an expression via gfc_simplify_expr. When an error occurs, roll back. * gfortran.h (gfc_try_simplify_expr): Declare it. gcc/testsuite/ChangeLog: PR fortran/103505 * gfortran.dg/pr103505.f90: New test. Co-authored-by: Steven G. Kargl (cherry picked from commit f46d32dd29b7623915e31b0508e2e925526fa7d8) Diff: --- gcc/fortran/array.c | 4 ++++ gcc/fortran/expr.c | 25 +++++++++++++++++++++++++ gcc/fortran/gfortran.h | 1 + gcc/testsuite/gfortran.dg/pr103505.f90 | 9 +++++++++ 4 files changed, 39 insertions(+) diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c index a4d1cb4c72d..c8dbb882542 100644 --- a/gcc/fortran/array.c +++ b/gcc/fortran/array.c @@ -492,6 +492,8 @@ match_array_element_spec (gfc_array_spec *as) if (!gfc_expr_check_typed (*upper, gfc_current_ns, false)) return AS_UNKNOWN; + gfc_try_simplify_expr (*upper, 0); + if (((*upper)->expr_type == EXPR_CONSTANT && (*upper)->ts.type != BT_INTEGER) || ((*upper)->expr_type == EXPR_FUNCTION @@ -524,6 +526,8 @@ match_array_element_spec (gfc_array_spec *as) if (!gfc_expr_check_typed (*upper, gfc_current_ns, false)) return AS_UNKNOWN; + gfc_try_simplify_expr (*upper, 0); + if (((*upper)->expr_type == EXPR_CONSTANT && (*upper)->ts.type != BT_INTEGER) || ((*upper)->expr_type == EXPR_FUNCTION diff --git a/gcc/fortran/expr.c b/gcc/fortran/expr.c index 186e860e382..82ef0fdfeb2 100644 --- a/gcc/fortran/expr.c +++ b/gcc/fortran/expr.c @@ -2328,6 +2328,31 @@ gfc_simplify_expr (gfc_expr *p, int type) } +/* Try simplification of an expression via gfc_simplify_expr. + When an error occurs (arithmetic or otherwise), roll back. */ + +bool +gfc_try_simplify_expr (gfc_expr *e, int type) +{ + gfc_expr *n; + bool t, saved_div0; + + if (e == NULL || e->expr_type == EXPR_CONSTANT) + return true; + + saved_div0 = gfc_seen_div0; + gfc_seen_div0 = false; + n = gfc_copy_expr (e); + t = gfc_simplify_expr (n, type) && !gfc_seen_div0; + if (t) + gfc_replace_expr (e, n); + else + gfc_free_expr (n); + gfc_seen_div0 = saved_div0; + return t; +} + + /* Returns the type of an expression with the exception that iterator variables are automatically integers no matter what else they may be declared as. */ diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index 7935aca23db..0436c4f308f 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -3370,6 +3370,7 @@ void gfc_free_ref_list (gfc_ref *); void gfc_type_convert_binary (gfc_expr *, int); bool gfc_is_constant_expr (gfc_expr *); bool gfc_simplify_expr (gfc_expr *, int); +bool gfc_try_simplify_expr (gfc_expr *, int); int gfc_has_vector_index (gfc_expr *); gfc_expr *gfc_get_expr (void); diff --git a/gcc/testsuite/gfortran.dg/pr103505.f90 b/gcc/testsuite/gfortran.dg/pr103505.f90 new file mode 100644 index 00000000000..522e53efcb2 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr103505.f90 @@ -0,0 +1,9 @@ +! { dg-do compile } +! PR fortran/103505 - this used to ICE in compare_bound_mpz_t +! Testcase by G.Steinmetz + +program p + integer, parameter :: a((2.)) = [4,8] ! { dg-error "scalar INTEGER" } + integer, parameter :: z(1:(2.)) = [4,8] ! { dg-error "scalar INTEGER" } + print *, a(1:1) ! { dg-error "Syntax error" } +end