From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx-relay84-hz1.antispameurope.com (mx-relay84-hz1.antispameurope.com [94.100.133.252]) by sourceware.org (Postfix) with ESMTPS id B813C38708DF for ; Sat, 13 Feb 2021 20:49:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org B813C38708DF Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=net-b.de Authentication-Results: sourceware.org; spf=none smtp.mailfrom=prvs=06717e691f=burnus@net-b.de Received: from s041.wsp.plusnet.de ([195.90.7.81]) by mx-relay84-hz1.antispameurope.com; Sat, 13 Feb 2021 21:49:34 +0100 Received: from [192.168.0.28] (port-92-195-199-151.dynamic.as20676.net [92.195.199.151]) by s041.wsp.plusnet.de (Postfix) with ESMTPSA id DEB702C026F; Sat, 13 Feb 2021 21:49:30 +0100 (CET) To: gcc-patches , fortran From: Tobias Burnus Subject: [Patch] Fortran: Fix ubound simplifcation [PR99027] Message-ID: <767e8576-908d-769d-7c63-80a7f6a048af@net-b.de> Date: Sat, 13 Feb 2021 21:49:29 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.7.1 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------E697324DAFC35A44571033FB" Content-Language: en-US X-cloud-security-sender: burnus@net-b.de X-cloud-security-recipient: fortran@gcc.gnu.org X-cloud-security-Virusscan: CLEAN X-cloud-security-disclaimer: This E-Mail was scanned by E-Mailservice on mx-relay84-hz1.antispameurope.com with 519D61725000 X-cloud-security-connect: s041.wsp.plusnet.de[195.90.7.81], TLS=1, IP=195.90.7.81 X-cloud-security-Digest: 13d8143bb1f83da6dfbb3fe55cf86058 X-cloud-security: scantime:1.662 X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=unavailable autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: fortran@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Fortran mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 13 Feb 2021 20:49:38 -0000 This is a multi-part message in MIME format. --------------E697324DAFC35A44571033FB Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit The problem which accessing dim= for an expression is that when the argument is an array, dim= and expr->rank are relative to the resulting array – but the array-ref also contains the DIMEN_ELEMENT which does not count for dim=/rank. OK for the trunk? (Reported against GCC 11, but I wonder whether we should still apply it to GCC 10.) Tobias --------------E697324DAFC35A44571033FB Content-Type: text/x-patch; charset=UTF-8; name="ubound.diff" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="ubound.diff" Fortran: Fix ubound simplifcation [PR99027] gcc/fortran/ChangeLog: PR fortran/99027 * simplify.c (simplify_bound_dim): Honor DIMEN_ELEMENT when using dim=. gcc/testsuite/ChangeLog: PR fortran/99027 * gfortran.dg/ubound_1.f90: New test. gcc/fortran/simplify.c | 12 +++++++++++- gcc/testsuite/gfortran.dg/ubound_1.f90 | 22 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/gcc/fortran/simplify.c b/gcc/fortran/simplify.c index 23317a2e2d9..35f267db588 100644 --- a/gcc/fortran/simplify.c +++ b/gcc/fortran/simplify.c @@ -4168,7 +4168,17 @@ simplify_bound_dim (gfc_expr *array, gfc_expr *kind, int d, int upper, { if (upper) { - if (!gfc_ref_dimen_size (&ref->u.ar, d - 1, &result->value.integer, NULL)) + int d2 = 0, cnt = 0; + for (int idx = 0; idx < ref->u.ar.dimen; ++idx) + { + if (ref->u.ar.dimen_type[idx] == DIMEN_ELEMENT) + d2++; + else if (cnt < d - 1) + cnt++; + else + break; + } + if (!gfc_ref_dimen_size (&ref->u.ar, d2 + d - 1, &result->value.integer, NULL)) goto returnNull; } else diff --git a/gcc/testsuite/gfortran.dg/ubound_1.f90 b/gcc/testsuite/gfortran.dg/ubound_1.f90 new file mode 100644 index 00000000000..7c588b026e1 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/ubound_1.f90 @@ -0,0 +1,22 @@ +! { dg-do run } +! +! PR fortran/99027 +! + program test + integer, dimension (1:3,1:6) :: array + integer, dimension (2:5,3:7,4:9,-4:2) :: array2 + + if (any ([4] /= ubound (array (1, 1:4)))) stop 1 + if (4 /= ubound (array (1, 1:4), dim=1)) stop 2 + + if (any (ubound (array2 (3,3,4,:)) /= [4+1+2])) stop 3 + if ( ubound (array2 (3,3,4,:), dim=1) /= 4+1+2 ) stop 4 + + if (any (ubound (array2 (3,:,4,:)) /= [7-3+1, 4+1+2])) stop 5 + if ( ubound (array2 (3,:,4,:), dim=1) /= 7-3+1 ) stop 6 + if ( ubound (array2 (3,:,4,:), dim=2) /= 4+1+2) stop 7 + if (any (ubound (array2 (3,:,4:4,:)) /= [7-3+1, 1, 4+1+2])) stop 8 + if ( ubound (array2 (3,:,4:4,:), dim=1) /= 7-3+1 ) stop 9 + if ( ubound (array2 (3,:,4:4,:), dim=2) /= 1 ) stop 10 + if ( ubound (array2 (3,:,4:4,:), dim=3) /= 4+1+2) stop 11 + end program test --------------E697324DAFC35A44571033FB--