From patchwork Thu Jul 18 19:27:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 1962205 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; secure) header.d=gmx.de header.i=anlauf@gmx.de header.a=rsa-sha256 header.s=s31663417 header.b=fnY0MQrh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WQ2rZ5LVgz1ySl for ; Fri, 19 Jul 2024 05:27:41 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id AAC4A3858C48 for ; Thu, 18 Jul 2024 19:27:38 +0000 (GMT) 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 B52373858CDA; Thu, 18 Jul 2024 19:27:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B52373858CDA Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B52373858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=212.227.15.15 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721330836; cv=none; b=GbpCzNmGRr+adpVWbOhug6PvbHkbV2v1TrysKJJ7CjMNS5d/buOcM5fIjVlK/oRL5Ors1p159Twxm2JlV5a1Gvnl00DnmtYD75Po8LNOC0mf100XiAaIf/zgm9gSkVWNeK0I1GnuYj0VJZ7QVULZX/f/bQtCMOMUNbqceFStZU0= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721330836; c=relaxed/simple; bh=s7/cU1RP6Qceg5Q5kACcphSL3CHR7XNn96x5oq4gUNc=; h=DKIM-Signature:MIME-Version:Message-ID:From:To:Subject:Date; b=ALAk6b754jOjZZWhaw1/UzLfRL1UZoPjdyNYIBHuCe4Oalke5wsH5rNQOm/dZ9JFM0sEpTWPQwqCcCdd0HiDpKf+V6PT3lv678+z4YLC32dZ5Ob4OslxtVYKaEJ0QtURFAYOsxDyZlgVPOF9uKIxoEfLe3MEcO3wJvvizVWUQds= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmx.de; s=s31663417; t=1721330833; x=1721935633; i=anlauf@gmx.de; bh=zlMSkwrjAQZtAOSFBJ80HwpC7vmaTmIfagnDFLc2+CA=; h=X-UI-Sender-Class:MIME-Version:Message-ID:From:To:Subject: Content-Type:Date:cc:content-transfer-encoding:content-type:date: from:message-id:mime-version:reply-to:subject:to; b=fnY0MQrhqv0xuonxTMz4pF6sIl8eSqaeIOoebkyaGAW7LSyByZVnF20tIMzdrTFM ujPW88u4B49pCwxK04upa/fALdXTpsHZ4qDipHG1yYCgrByLqpzGulvW9ilBW73E2 JrhCllxbLhv43daVnE0ggmY0xAu5QIiKrghIRC3vccTaYyXKeNP6ypzTC4lml0A0w IrIG0+5OqgPjhbVt7CKkOSoQpfELDa24V2uj1UxFg5aUHo+aW9IB2LLc0Ot8afZJB +O5dPh5f6fT8KnZHOv2GG5wO73+yoRNrzbs6rhHaWJnCbfQFdvw5dEdqFGjwfCt9U 86FwS5GO6L7azdV+oA== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.232.153.156] ([79.232.153.156]) by web-mail.gmx.net (3c-app-gmx-bs03.server.lan [172.19.170.52]) (via HTTP); Thu, 18 Jul 2024 21:27:13 +0200 MIME-Version: 1.0 Message-ID: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: character array constructor with >= 4 constant elements [PR103115] Date: Thu, 18 Jul 2024 21:27:13 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:QGaDvDq+KyJD7cikSjY2FnHO4UmiPDgrgdGwNrj6zdlmDjMSVzU/I1m11CF3nFpM2VSdl nhMyzSy++u7IjZtE6SK5M4XE9GHHW4T5VkT9GO2TSZjta3U0QmVHpXq08mNozYdsQAMhG3o2XeIe l48DEBoHHnHzl7autzK+oFvv0tl6dPfcSgiNbjxH1AtjW9oVAC8ZP8tDn6Sl9JVwBlSc05L83D2C qk//TofA4+O07QcCDAyqU5rFpGLxZqNlE+2RzGCyKLO0O98v0kxrjHXLUGnQoUqSowXXEEWSE0zb B8= UI-OutboundReport: notjunk:1;M01:P0:8NECa1ORd8c=;NH1UVo1h7d5XYNI/6e1HbzD4upk F+NVjLxFcitY4CuXuP9UpbbIY61G99xw2iMmIM2VG3ySzIM74QsrZse9k7AlNJUB1sWxKCC9A UPjP6Orx9QzgPWKhwpyblbOyCE1rLVhemeHZ8RLV7An8Q38/M4sDHDpdPfNy/NNk6l5Uu/aHo R1eNfTCsmQnOuqOufhNwo4EDLBPlAYCAu9sNTnpqbCLDDBlXUHewK4PL9wFqcKSbC1toPiiUr azMMnq7n+z5ifgccsXCCfbLGq9F31Yxk7J4qyVsgEhdSuB6Qh7PvZrV0hqQBw9Vo0yRlvci85 kQUDmLSAveaoZrBSTi9Ze8FD8MgEqRoyWmpieyPTBhVCyMHg44qOHqYv7fGIBAUuJsVfkORWy Uuf9O1hKZxzIClHPiwp3daFcvbmxW6j/dxeS3dtSzZD2me0hgAtApP7TdPOHlI1m3ThK1g0kG kSz5XTWOidnC+re7PUaCBU+TbpsFE7GBM5r/VezaTZhLRi5phnkQSkdPshNozzlhyJM6H745A bbYljvRRGPD8yj3o49DDPZFjcpiBYfbmZd/yIqo1RrtwSDw3rkx07HMH1NFafLFdf7JK5H+uq uYctch+fgJv1THn1JzLqT/slM78TGbsXnmJoFWsW0cf8N1dypqwGnza3Te3lyj9zrF09ir1Q7 JD94/iPs68oNmoQ4RtsB+mnzchAUPM+MpnUdHP4PJg== X-Spam-Status: No, score=-12.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org Dear all, here's a quite obvious fix for an ICE when processing an array constructor where the first element is of deferred length, and at least four constant elements followed, or an iterator with at least four elements. There is a code path that then tries to combine these constant elements and take the element size of the first (variable length) element in the constructor. (For gcc with checking=release, no ICE occured; wrong code was generated instead.) Obvious fix: if we see that the element size is not constant, falls back to the case handling the constructor element-wise. Regtested on x86_64-pc-linux-gnu. OK for mainline / backports? Thanks, Harald From 5b264e77b54e211c7781d2c2e5dc324846a774a1 Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Thu, 18 Jul 2024 21:15:48 +0200 Subject: [PATCH] Fortran: character array constructor with >= 4 constant elements [PR103115] gcc/fortran/ChangeLog: PR fortran/103115 * trans-array.cc (gfc_trans_array_constructor_value): If the first element of an array constructor is deferred-length character and therefore does not have an element size known at compile time, do not try to collect subsequent constant elements into a constructor for optimization. gcc/testsuite/ChangeLog: PR fortran/103115 * gfortran.dg/string_array_constructor_4.f90: New test. --- gcc/fortran/trans-array.cc | 4 +- .../string_array_constructor_4.f90 | 59 +++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/string_array_constructor_4.f90 diff --git a/gcc/fortran/trans-array.cc b/gcc/fortran/trans-array.cc index dc3de6c3b14..c93a5f1e754 100644 --- a/gcc/fortran/trans-array.cc +++ b/gcc/fortran/trans-array.cc @@ -2147,7 +2147,9 @@ gfc_trans_array_constructor_value (stmtblock_t * pblock, p = gfc_constructor_next (p); n++; } - if (n < 4) + /* Constructor with few constant elements, or element size not + known at compile time (e.g. deferred-length character). */ + if (n < 4 || !INTEGER_CST_P (TYPE_SIZE_UNIT (type))) { /* Scalar values. */ gfc_init_se (&se, NULL); diff --git a/gcc/testsuite/gfortran.dg/string_array_constructor_4.f90 b/gcc/testsuite/gfortran.dg/string_array_constructor_4.f90 new file mode 100644 index 00000000000..b5b81f07395 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/string_array_constructor_4.f90 @@ -0,0 +1,59 @@ +! { dg-do run } +! PR fortran/103115 - character array constructor with >= 4 constant elements +! +! This used to ICE when the first element is deferred-length character +! or could lead to wrong results. + +program pr103115 + implicit none + integer :: i + character(*), parameter :: expect(*) = [ "1","2","3","4","5" ] + character(5) :: abc = "12345" + character(5), parameter :: def = "12345" + character(:), dimension(:), allocatable :: list + character(:), dimension(:), allocatable :: titles + titles = ["1"] + titles = [ titles& + ,"2"& + ,"3"& + ,"4"& + ,"5"& ! used to ICE + ] + if (len (titles) /= 1 .or. size (titles) /= 5) stop 1 + if (any (titles /= expect)) stop 2 + titles = ["1"] + titles = [ titles, (char(48+i),i=2,5) ] + if (len (titles) /= 1 .or. size (titles) /= 5) stop 3 + if (any (titles /= expect)) stop 4 + titles = ["1"] + titles = [ titles, ("2345"(i:i),i=1,4) ] + if (len (titles) /= 1 .or. size (titles) /= 5) stop 5 + if (any (titles /= expect)) stop 6 + titles = ["1"] + titles = [ titles, (def(i:i),i=2,5) ] + if (len (titles) /= 1 .or. size (titles) /= 5) stop 7 + if (any (titles /= expect)) stop 8 + list = [ (char(48+i),i=1,5) ] + titles = [ list(1), (char(48+i),i=2,5) ] + if (len (titles) /= 1 .or. size (titles) /= 5) stop 9 + if (any (titles /= expect)) stop 10 + titles = ["1"] + titles = [ titles, (abc(i:i),i=2,5) ] + if (len (titles) /= 1 .or. size (titles) /= 5) stop 11 + if (any (titles /= expect)) stop 12 + + ! with typespec: + list = [ (char(48+i),i=1,5) ] + titles = [ character(2) :: list(1), (char(48+i),i=2,5) ] + if (len (titles) /= 2 .or. size (titles) /= 5) stop 13 + if (any (titles /= expect)) stop 14 + titles = ["1"] + titles = [ character(2) :: titles, (char(48+i),i=2,5) ] + if (len (titles) /= 2 .or. size (titles) /= 5) stop 15 + if (any (titles /= expect)) stop 16 + titles = ["1"] + titles = [ character(2) :: titles, (def(i:i),i=2,5) ] + if (len (titles) /= 2 .or. size (titles) /= 5) stop 17 + if (any (titles /= expect)) stop 18 + deallocate (titles, list) +end -- 2.35.3