From patchwork Thu Oct 14 21:27:02 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 46251 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A2E2385843D for ; Thu, 14 Oct 2021 21:27:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A2E2385843D DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1634246871; bh=s6ROT0z5Tfn+tymi2sVXGNrHdjFMX1bvesyKYZhZCiw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=b2OBDIT1gwZ4dEdgZTXW/QThwjW1SmZtqmV+qqa4rPUM1MKyBZLwjcU79gGxazsd0 Ll+0Rm4NLwTrZFL89dyydTPwJrOqEygDWGidZPf/UloclwGJQdtbMrPfOBt3XVBsdu 2dEwDr1G0KGplWd0yDJUgkPN9P19yTS4yYi9RcY4= 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.15.15]) by sourceware.org (Postfix) with ESMTPS id 090AF3858419; Thu, 14 Oct 2021 21:27:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 090AF3858419 X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [93.207.82.4] ([93.207.82.4]) by web-mail.gmx.net (3c-app-gmx-bs21.server.lan [172.19.170.73]) (via HTTP); Thu, 14 Oct 2021 23:27:02 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] PR fortran/102685 - ICE in output_constructor_regular_field, at varasm.c:5514 Date: Thu, 14 Oct 2021 23:27:02 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:3MyP3vZ1O18ByiA2X1dsDUbiJpoCuHI2UO56GNjwkmSb1sU55kStnHBD6m1J1WFZ7Khmn 4lkNgcHIapqGg1RXWB+PAfyyp4YaqvxauZluixX+FiE4tuM3yQT2Q3ssat/mcDelPSXiqWk8zQ2I 6/OR27BWRSPgU2dV8d7pqwZBOYldOecYX51GwJWgb33v3kVQ/MPMiz2rGQztwi+KldQV7hcKeU3j I2SIcspe3bdeEhaLAnDVIjnr2HgCUIH23hFYkxzytVgiSV6g/WkHraQlttdepKu4yBUa2WLch6J4 ko= X-UI-Out-Filterresults: notjunk:1;V03:K0:UQZNjX0kVRQ=:1H0UpoC1USZSgjdUQ2CGsE yM++5eee/OyFEg44zvqssAc5U17b5qOTTbqJ/Ji7EIZxUj5FnwbKoPDmgaSikQHzsTdV9CU2n M6xs6S5nPE9huwRqRqsPjwKUt6/4hNSwXUdcJbBbpaJUXC3U4nwpniWK2m12BZOHkOulT7qi0 E1TapEW7dui6f10QQS9O3q9+npC86lPl+zyBthggbdpSaRKSqom7VT3skiF2KM0OB1nXyy3a6 QCj/zhKwAW929DTk9SyVfpcELiJWnhJ+bdv8W4R4GFff23ITgtmEYtiOzITNgw6uXx/HVAJOQ A9877uLplJk3ZXf1+5T257AUW7KqDH+CpRG89Ng7iUp8NQU980yZ6tBKx9W7qYKDjXkDmk3Sz OBKTzeJ0KPAZtU5n8cLcvRTcatjtTXjcZDx8TIwt19mXKqVlz3B1YRfo5xTZoal3RMDZ5KEP4 PYKQwk7fSaORU7idHqBUB0+SOUiKJhNEBG9L0K7j/ndqciFjCVxMhwTNMrRN3QuaDnPu1dhay wAtV7JLK6iirwmy1d/UwSBQZEsbYtRa2PLFWy8HiSDEtyPYzfdeROf5s8ytUh5Uxz3BmOhM53 1TVyL96gQ7uDSXfU+KAcVIalXxcTiEoM18l8N7V0UeiDPHmHKy5zIxQXIigIRLxW8Ew1VQzrf vR/Jtz9YbRSIVFA+TSFbDclwZpXOKHEklKsMs9elSIzzjMp1j/hcbRxNErbAwEFi2ZzP/iu5U cjovdZfCg7Wy0Nfzmss5KUQWAUD8CXq+LHUiArU5u6z3OS6ywgsI7l3NZ3wT0YxzUhtvM5Zmi eN5af8+ X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, KAM_NUMSUBJECT, KAM_SHORT, RCVD_IN_DNSWL_LOW, 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+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Dear Fortranners, the attached patch adds a check for the shape of arrays in derived type constructors. This brings it in line with other major brands. Example: type t integer :: a(1) end type type(t), parameter :: y(2) = t([1,2]) end This was silently accepted before, but now gives: pr102685-z1.f90:4:33: 4 | type(t), parameter :: y(2) = t([1,2]) | 1 Error: The shape of component 'a' in the structure constructor at (1) differs from the shape of the declared component for dimension 1 (2/1) During regtesting several previously invalid testcases surfaced which would be rejected by the present change. They have been adjusted along the discussion with Tobias and Paul, see the thread https://gcc.gnu.org/pipermail/fortran/2021-October/056707.html In developing the patch I encountered a difficulty with testcase dec_structure_6.f90, which uses a DEC extension, namelist "old-style CLIST initializers in STRUCTURE". I could not figure out how to determine the shape of the initializer; it seemed to be always zero. I've added code to accept this, but only under -fdec-structure, and added a TODO in a comment. If somebody reading this could give me a hint to solve end, I would adjust the patch accordingly. Regtested on x86_64-pc-linux-gnu. OK? Or further comments? Thanks, Harald Fortran: validate shape of arrays in constructors against declarations gcc/fortran/ChangeLog: PR fortran/102685 * resolve.c (resolve_structure_cons): In a structure constructor, compare shapes of array components against declared shape. gcc/testsuite/ChangeLog: PR fortran/102685 * gfortran.dg/derived_constructor_char_1.f90: Fix invalid code. * gfortran.dg/pr70931.f90: Likewise. * gfortran.dg/transfer_simplify_2.f90: Likewise. * gfortran.dg/pr102685.f90: New test. diff --git a/gcc/fortran/resolve.c b/gcc/fortran/resolve.c index 0d0af39d23f..d035b30ad7d 100644 --- a/gcc/fortran/resolve.c +++ b/gcc/fortran/resolve.c @@ -29,6 +29,7 @@ along with GCC; see the file COPYING3. If not see #include "data.h" #include "target-memory.h" /* for gfc_simplify_transfer */ #include "constructor.h" +#include "parse.h" /* Types used in equivalence statements. */ @@ -1454,6 +1455,42 @@ resolve_structure_cons (gfc_expr *expr, int init) } } + /* Validate shape. We silently accept some cases where the apparent + shape of the constructor is zero, and we cannot check dynamic or PDT + arrays. */ + if (cons->expr->expr_type == EXPR_ARRAY && rank == cons->expr->rank + && comp->as && !comp->attr.allocatable && !comp->attr.pointer + && !comp->attr.pdt_array) + { + mpz_t len; + mpz_init (len); + for (int n = 0; n < rank; n++) + { + gcc_assert (comp->as->upper[n]->expr_type == EXPR_CONSTANT + && comp->as->lower[n]->expr_type == EXPR_CONSTANT); + mpz_set_ui (len, 1); + mpz_add (len, len, comp->as->upper[n]->value.integer); + mpz_sub (len, len, comp->as->lower[n]->value.integer); + /* Shape agrees for this dimension. */ + if (mpz_cmp (cons->expr->shape[n], len) == 0) + continue; + /* Accept DEC old-style initializers in STRUCTURE, where shape + is currently not correctly set (it is zero. Why?). + TODO: Fix this or find a better solution. */ + if (flag_dec_structure + && mpz_cmp_si (cons->expr->shape[n], 0) == 0) + continue; + gfc_error ("The shape of component %qs in the structure " + "constructor at %L differs from the shape of the " + "declared component for dimension %d (%ld/%ld)", + comp->name, &cons->expr->where, n+1, + mpz_get_si (cons->expr->shape[n]), + mpz_get_si (len)); + t = false; + } + mpz_clear (len); + } + if (!comp->attr.pointer || comp->attr.proc_pointer || cons->expr->expr_type == EXPR_NULL) continue; diff --git a/gcc/testsuite/gfortran.dg/derived_constructor_char_1.f90 b/gcc/testsuite/gfortran.dg/derived_constructor_char_1.f90 index 892a9c9f4c1..91fc4c902d8 100644 --- a/gcc/testsuite/gfortran.dg/derived_constructor_char_1.f90 +++ b/gcc/testsuite/gfortran.dg/derived_constructor_char_1.f90 @@ -5,7 +5,7 @@ ! ! Type :: t5 - character (len=5) :: txt(4) + character (len=5) :: txt(2) End Type t5 character (len=3), parameter :: str3(2) = [ "ABC", "ZYX" ] diff --git a/gcc/testsuite/gfortran.dg/pr102685.f90 b/gcc/testsuite/gfortran.dg/pr102685.f90 new file mode 100644 index 00000000000..d325c27b32a --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr102685.f90 @@ -0,0 +1,30 @@ +! { dg-do compile } +! PR fortran/102685 + +program p + type t + integer :: a(2) + end type + type(t), parameter :: x0 = t([2]) ! { dg-error "shape of component" } + type(t), parameter :: x1(2) = t([2]) ! { dg-error "shape of component" } + type(t), parameter :: x(2) = t([integer::]) ! { dg-error "shape of component" } + + type u + integer :: a + integer :: b(0) + end type + type(u), parameter :: z0(2) = u(1, [integer::]) ! valid + type(u), parameter :: z1 = u(1, 2 ) ! valid + type(u), parameter :: z2(2) = u(1, 2 ) ! valid + type(u), parameter :: z3 = u(1, [2]) ! { dg-error "shape of component" } + type(u), parameter :: z4(2) = u(1, [2]) ! { dg-error "shape of component" } + + type v + integer :: a(2,1) + end type + type(v), parameter :: y0 = v(reshape([1,2],[2,1])) ! valid + type(v), parameter :: y1 = v(reshape([1,2],[1,2])) ! { dg-error "shape of component" } + type(v), parameter :: y(1) = v(reshape([1,2],[1,2])) ! { dg-error "shape of component" } + + print *, x0,x,x1,y0,y1,y,z0,z1,z2,z3,z4 +end diff --git a/gcc/testsuite/gfortran.dg/pr70931.f90 b/gcc/testsuite/gfortran.dg/pr70931.f90 index 08ecd687752..4444b5eec3b 100644 --- a/gcc/testsuite/gfortran.dg/pr70931.f90 +++ b/gcc/testsuite/gfortran.dg/pr70931.f90 @@ -5,6 +5,7 @@ program p integer :: a integer :: b(0) end type - type(t), parameter :: z = t(1, [2]) +! type(t), parameter :: z = t(1, [2]) ! original invalid code + type(t), parameter :: z = t(1, [integer::]) print *, z end diff --git a/gcc/testsuite/gfortran.dg/transfer_simplify_2.f90 b/gcc/testsuite/gfortran.dg/transfer_simplify_2.f90 index e0f3f94c4ca..b428fa64b56 100644 --- a/gcc/testsuite/gfortran.dg/transfer_simplify_2.f90 +++ b/gcc/testsuite/gfortran.dg/transfer_simplify_2.f90 @@ -145,7 +145,7 @@ contains real(4) :: x(2) end type mytype - type (mytype), parameter :: dt1(2) = transfer (c1, mytype ((/1.0,2.0,3.0,4.0/)), 2) + type (mytype), parameter :: dt1(2) = transfer (c1, mytype ((/1.0,2.0/)), 2) type (mytype) :: dt2(2) dt2 = transfer (c2, dt2);