From patchwork Thu Dec 2 21:48:27 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 1563014 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=vCS9Q19U; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4J4qNr0Hmsz9s3q for ; Fri, 3 Dec 2021 08:49:30 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AF442385C413 for ; Thu, 2 Dec 2021 21:49:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AF442385C413 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1638481767; bh=v3jY5ioAWhAkrGB7AqVljZvtMrrMUmB4ZC2Bi10BTsY=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=vCS9Q19UM4lhjrZ9m44l1RrY4zaTprN8zmQYw33UZcDKj/ncNjsvM2SclRD3/vRZN 0Ppi7GSJN8cieF6hepRF1EYfuze5pO+Fnv557ut/MO1zYofW8o+6gwQjaSpUB8t64g C+5gOQHrFVlDVq0/wiRRIjRDAXb0lAvCM8WS1/BY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id 9303E3858010; Thu, 2 Dec 2021 21:48:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 9303E3858010 X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [93.207.80.117] ([93.207.80.117]) by web-mail.gmx.net (3c-app-gmx-bs16.server.lan [172.19.170.68]) (via HTTP); Thu, 2 Dec 2021 22:48:27 +0100 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] PR fortran/103505 - ICE in compare_bound_mpz_t, at fortran/resolve.c:4587 Date: Thu, 2 Dec 2021 22:48:27 +0100 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:DXCGZNnpggQMn2j9a3q4arnMuJAWRkRR9xzCNGYgmp+YA3ve5TARGL7bIaVG99QZOHPCe hAVzu9DI+WiFb2pB630gNzl4iHP/sOakANfQ3N6bBN9orOiiaL/7eYwWv1u5mp0OLFvyRm53OvHg IhKwHzQyUJi2/qr78c5tzEy8R6xCKDEvO6Bh3SYVqPU4ugJx1iLjm53zoT+FS1qXeGXvWDZtBPT8 NISnN3iWhbVbvOCjKB/otVKh7mmaMa6u8qRpGbByG2Xti3lm96+nTI+LKYHF1T2qPYdmSPZ5lM7E Ls= X-UI-Out-Filterresults: notjunk:1;V03:K0:qcff5ZXYskE=:eL7brZ6WyEH4ZTLcqrzr6t 6RHeoUqBwCujosGso0c9DzoOeJagX4iOiBhMJYaIBzByTv3y6QCEQkCX17uP4JruUkuMTn88e ixPoEsCSAVxxCkXFPFp2KwvpieuozO/iH8rh/QniweJn8sj/rIBkuMLF4jeMuj3RDoNRJCfuP kRoPKWuBRYwkDf3AnCH95/qbZRzF8c4N9v4tEHwqT0EMmeGZnEiNsgyO+pKEYvdT3GLVAU7d6 7oUwGe8h422MrJSNl/NyK4KPh9/nmbvTE9MWa/WTyH5MwOT+2vrwZkqB2U7RIe4HRzn9JZCwQ OyPtqv/0DufGIWhOmdeKeHi/oATLal44HH/R6Gqj7i6ZoAJz2wQ31J7mPm0FjVI28yZG2DHhP 3hpCmjc+I7TkP52LjXWIkK4u2gO2+G4hloXyF2N5r9Nht6NCusv8PsXlDX+FvGqSnpu2S5u1/ sR8my/FLSOeZXzFnSFcjjO7p7oKAME4ZQdcMez4Zk7R5hbFfyGFKlm6D1gbQLcow7uX5MOrSb MuzG4udW74KQLOCn7eS1oSfaQZhN9pUE69Do4LU7H4j80f8t2cuUxysjRW2tKBnbMfhOwuPOm /7bX/bEN2p3oY29x4FmILwKlqAmZe0nexIduwdSINJYwTigmeQUHNoWydbhoyaUqRaXVahXAm 71ztnvir/tY2D4jwfcF0cn9chVB4ayccYdpFsw/V6LO3jHAlRPykJQGN84AWNhejpqshlen2n bJjN3jScXFQX5WIpv4dV2E2Hyz0YGhhcUsI8vioaj3x8kc6YMcHQeyzrQDN9i9Nzms1jHMLlo c3+aTbC X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" Dear Fortranners, specifying invalid constant array declarations (e.g. real array bounds) could lead to an ICE because the array specs were checked for consistency. A possible solution is to try an early simplification before doing that check and was suggested by Steve. However, regtesting did reveal that bad declarations involving e.g. arithmetic errors - like division by zero - were now handled differently. (The relevant testcase was gfortran.dg/arith_divide_2.f90). I therefore added a new function gfc_try_simplify_expr that accepts the simplification only if no error occurs. This allows discovery of arithmetic errors also at later stages and will be used here. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 27f981bd1e9611373e4565c1d350b1da3eb653f0 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Thu, 2 Dec 2021 22:33:49 +0100 Subject: [PATCH] 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 --- 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(+) create mode 100644 gcc/testsuite/gfortran.dg/pr103505.f90 diff --git a/gcc/fortran/array.c b/gcc/fortran/array.c index fbc66097c80..5762c8d92d4 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 4084d18f136..87089321a3b 100644 --- a/gcc/fortran/expr.c +++ b/gcc/fortran/expr.c @@ -2331,6 +2331,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 1846ee4fd3c..24ad3ed4d06 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -3585,6 +3585,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 -- 2.26.2