From patchwork Tue Oct 4 21:19:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 1703 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp315058wrs; Tue, 4 Oct 2022 14:20:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6+bPvpTs9Jrom6WaZzDu4eb81RMok9mlCEz5zNRJsWEzWMVoqS3qcJFT9xDN8Px1ORTkxs X-Received: by 2002:aa7:d39a:0:b0:458:a612:f805 with SMTP id x26-20020aa7d39a000000b00458a612f805mr16955915edq.49.1664918446928; Tue, 04 Oct 2022 14:20:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664918446; cv=none; d=google.com; s=arc-20160816; b=QougAVScGKXr+ydX4jzFuyHSX8KqT1Mw6bmrxRYmg0OapxIPODiqvnqDQPBugtJ3vd hJdEH9RcmjirlmNhMpnYHlfOrZPS/9EacHrLWAUWWF9mNpdpj7sehvOeHDGE8/zzRz63 BrbdLd6pskHS0aYHzZRMVtNbe4rkl0GrgyFzTmt5CL1lkwT5RfiIp4e8xUEiM9248cMg 6i0eGrQl0++OMSy59I0m5imhFiShUcZ0/ZX5YO+tni4ldrg+OsL+OIQJi8B4ylr4yLUQ EimD/jk+OpUazB54X1u08znLiXQ8bFcXeWE3UkTK0BQhgUl76UFzrFV84118GTshfo8x EKHg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:sensitivity :importance:date:subject:to:message-id:mime-version:dmarc-filter :delivered-to:dkim-signature:dkim-filter; bh=3Rej7CMHLDEuVpQJr9KDX71iznNQmyDm8JaKpI6C7Aw=; b=yZYjYJ9IKKLiEdrivWEwOogF42QlUwDdwzVnHOlceA05jw8oNbUJmlG6+Zi0kN0U08 mB9UhtPvTRo0Q4Sdnt5tcOt1GlhygwDkAyI65H8H1s3cKctAl8CaMQsUovfeFvtwfJVk SkcsKF19QAWTadiuKOQ8Ig6zXeKph0hhE63KCx9g9uxjzuonRjnMc8SB1SCJOC1t7fTp vRX6McJxlMk7J4ahziTHLW5IZeWuuhqdsxYxQVUB7h5yzcUrgCB62WZNdQNriWDmVS4F m3HsyKEV1j4X72VxniA/n2Iz53nzDaK607jcUBJMU25o98Ssgmk1ZvqrgyxzVFa9b08a RlPQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="eA/eLuWv"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id ji20-20020a170907981400b0073ce34d1a13si12372452ejc.499.2022.10.04.14.20.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Oct 2022 14:20:46 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="eA/eLuWv"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 0C369385801E for ; Tue, 4 Oct 2022 21:20:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0C369385801E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1664918444; bh=3Rej7CMHLDEuVpQJr9KDX71iznNQmyDm8JaKpI6C7Aw=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=eA/eLuWvpjRlcrWCVKzXOg47JMbyvR8fw+NCHRJE9SQ/C8IkrnbmrxGqFk99tPqPj yQDR+Gh+6V9GjwoWDL0lednftKpYdKWNDemDVUNqwF/xE8+2A4uf/24GnbOGmhJfm/ IHHBfO6JSlCeZdHBtY7fx7zGml3+qGR5SzWxV/4A= 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 A32473858C50; Tue, 4 Oct 2022 21:19:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A32473858C50 X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [93.207.82.142] ([93.207.82.142]) by web-mail.gmx.net (3c-app-gmx-bs23.server.lan [172.19.170.75]) (via HTTP); Tue, 4 Oct 2022 23:19:56 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: error recovery for invalid types in array constructors [PR107000] Date: Tue, 4 Oct 2022 23:19:56 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:qUyO5B4xSEFz683G695FDTO6xu8G8eUGABRmwOXQMT5kmI13/uCjdJvV08WxdHQ61mdkd 61B8ohRuHBbhULplHwLKkjp9NyyzgGBP/VtOolWIrsp0+vcXhpP7iHM5YC9AZtOSn3RNJuNX7kAE c5gYN3ZxKCRlQkOmSRQql7i/H0YIuNo4eBjx5K1LJLtfHd6KdwAYKjIsQLJTNoifqG6jV19SHICL /THtMij2qLV37DQbXwPXnvYtxvpm7jID1c5ySq91odEiSRC3tq4wZ0fdCVAjpED0sMcffMenXLHr H4= X-UI-Out-Filterresults: notjunk:1;V03:K0:sVkNsJxnZ0E=:QYp/LdlmCZq73Vcg41Wqfa DMm8n7BKgi5JIeBdROCZd5j/UHhdtb50Hkjxi5ezNu5SUdR4y8Us6y79cpJjrRsZQfgDXn411 8uPPAaMNkjy4KSG6SsDjCnibjCJEUG3vkmSB5O3wP1++WjP+1vaVBAaxAHBsX2OWvWtwAJdyK NNPY5jtVnyMzzorCdde7M18JhCkTr6/aHRFYhSe85bAOPQZEiB6zDelVx8mNdl7BaMEgWJzjN RouHJwGIrUbK0J8bVI95OGZ8z1KQYJaMfFNnZ+xAVcYtAhnXYNwz5PrWH9lii3eQKNVzcAdUR vZ7Bep3pwiavZ2475y4ui+M3pHGODZayCVidbUPuCsxUmMQEuGOswhvjNszsHeCl/QH2Pc5hN cf99alhXsStKecTD3PXEKJJcTQXpDoiCJE47g3OEmwCs/xMoxKtp66Rg1eZRPkuUAtb1SkDyj /9PEBddyu7x1XV0acO3WRn/vcnC0c3KBPrPgU1L4oqJiFP/1S9Pmez1PmIrXFgYZypHe7PgPG YH4Q3xkGyxwu9Q6D8m7OBIN0xyx22f2KzqHZcknxRepehbSqj3o3syuqomJG8IF3GTas31PgM UnLYgDu8UxWCo7A3J0NW8C/YgJUvmc744SgUJ9hRnsgbhY5+s21CBvobtRqm94/km6/fO4N3p xyLJQz1ofzbZUPp18IMGOzAmgFYkxT+0k8q960T5YpfspgRxuvA813X1Ck1U5ntq18Jf+qEmh eD4PFhySmK8QSQpPtQB9vjFnfLmsXeWHCdWKgEVmYdv9rH4PayoL+xuOYtpVF7uJM1mVfeKDj AgGBA+T X-Spam-Status: No, score=-12.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, 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.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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1745793525226629377?= X-GMAIL-MSGID: =?utf-8?q?1745793525226629377?= Dear all, we did not recover well from bad expressions in array constructors, especially when there was a typespec and a unary '+' or '-', and when the array constructor was used in an arithmetic expression. The attached patch introduces an ARITH_INVALID_TYPE that is used when we try to recover from these errors, and tries to handle all unary and binary arithmetic expressions. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From ad892a270c504def2f8f84494d5c7bcba9aef27f Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Tue, 4 Oct 2022 23:04:06 +0200 Subject: [PATCH] Fortran: error recovery for invalid types in array constructors [PR107000] gcc/fortran/ChangeLog: PR fortran/107000 * arith.cc (gfc_arith_error): Define error message for ARITH_INVALID_TYPE. (reduce_unary): Catch arithmetic expressions with invalid type. (reduce_binary_ac): Likewise. (reduce_binary_ca): Likewise. (reduce_binary_aa): Likewise. (gfc_real2complex): Source expression must be of type REAL. * gfortran.h (enum arith): Add ARITH_INVALID_TYPE. gcc/testsuite/ChangeLog: PR fortran/107000 * gfortran.dg/pr107000.f90: New test. --- gcc/fortran/arith.cc | 19 ++++++++++ gcc/fortran/gfortran.h | 2 +- gcc/testsuite/gfortran.dg/pr107000.f90 | 50 ++++++++++++++++++++++++++ 3 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gfortran.dg/pr107000.f90 diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc index d57059a375f..e6e35ef3c42 100644 --- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -118,6 +118,9 @@ gfc_arith_error (arith code) case ARITH_WRONGCONCAT: p = G_("Illegal type in character concatenation at %L"); break; + case ARITH_INVALID_TYPE: + p = G_("Invalid type in arithmetic operation at %L"); + break; default: gfc_internal_error ("gfc_arith_error(): Bad error code"); @@ -1261,6 +1264,9 @@ reduce_unary (arith (*eval) (gfc_expr *, gfc_expr **), gfc_expr *op, gfc_expr *r; arith rc; + if (op->expr_type == EXPR_OP && op->ts.type == BT_UNKNOWN) + return ARITH_INVALID_TYPE; + if (op->expr_type == EXPR_CONSTANT) return eval (op, result); @@ -1302,6 +1308,9 @@ reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **), gfc_expr *r; arith rc = ARITH_OK; + if (op1->expr_type == EXPR_OP && op1->ts.type == BT_UNKNOWN) + return ARITH_INVALID_TYPE; + head = gfc_constructor_copy (op1->value.constructor); for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c)) { @@ -1354,6 +1363,9 @@ reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **), gfc_expr *r; arith rc = ARITH_OK; + if (op2->expr_type == EXPR_OP && op2->ts.type == BT_UNKNOWN) + return ARITH_INVALID_TYPE; + head = gfc_constructor_copy (op2->value.constructor); for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c)) { @@ -1414,6 +1426,10 @@ reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_expr *, gfc_expr **), if (!gfc_check_conformance (op1, op2, _("elemental binary operation"))) return ARITH_INCOMMENSURATE; + if ((op1->expr_type == EXPR_OP && op1->ts.type == BT_UNKNOWN) + || (op2->expr_type == EXPR_OP && op2->ts.type == BT_UNKNOWN)) + return ARITH_INVALID_TYPE; + head = gfc_constructor_copy (op1->value.constructor); for (c = gfc_constructor_first (head), d = gfc_constructor_first (op2->value.constructor); @@ -2238,6 +2254,9 @@ gfc_real2complex (gfc_expr *src, int kind) arith rc; bool did_warn = false; + if (src->ts.type != BT_REAL) + return NULL; + result = gfc_get_constant_expr (BT_COMPLEX, kind, &src->where); mpc_set_fr (result->value.complex, src->value.real, GFC_MPC_RND_MODE); diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index 4babd77924b..fc0aa51df57 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -226,7 +226,7 @@ enum gfc_intrinsic_op enum arith { ARITH_OK = 1, ARITH_OVERFLOW, ARITH_UNDERFLOW, ARITH_NAN, ARITH_DIV0, ARITH_INCOMMENSURATE, ARITH_ASYMMETRIC, ARITH_PROHIBIT, - ARITH_WRONGCONCAT + ARITH_WRONGCONCAT, ARITH_INVALID_TYPE }; /* Statements. */ diff --git a/gcc/testsuite/gfortran.dg/pr107000.f90 b/gcc/testsuite/gfortran.dg/pr107000.f90 new file mode 100644 index 00000000000..c13627f556b --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr107000.f90 @@ -0,0 +1,50 @@ +! { dg-do compile } +! PR fortran/107000 - ICE in gfc_real2complex, reduce_unary, reduce_binary_* +! Contributed by G.Steinmetz + +program p + real :: y(1) + complex :: x(1) + x = (1.0, 2.0) * [real :: -'1'] ! { dg-error "Operand of unary numeric operator" } + x = (1.0, 2.0) * [complex :: +'1'] ! { dg-error "Invalid type" } + x = [complex :: -'1'] * (1.0, 2.0) ! { dg-error "Invalid type" } + y = [complex :: -'1'] * 2 ! { dg-error "Invalid type" } + y = 2 * [complex :: -'1'] ! { dg-error "Invalid type" } + y = 2 * [complex :: -(.true.)] ! { dg-error "Invalid type" } + y = [complex :: -(.true.)] * 2 ! { dg-error "Invalid type" } + print *, - [real :: -'1' ] ! { dg-error "Invalid type" } + print *, - [real :: [-'1']] ! { dg-error "Invalid type" } + print *, - [real :: +(.true.) ] ! { dg-error "Invalid type" } + print *, - [real :: [+(.true.)]] ! { dg-error "Invalid type" } + print *, 2 * [real :: -'1' ] ! { dg-error "Invalid type" } + print *, 2 * [real :: (-'1')] ! { dg-error "Invalid type" } + print *, [real :: -'1' ] * 2 ! { dg-error "Invalid type" } + print *, [real :: (-'1')] * 2 ! { dg-error "Invalid type" } + print *, 2 * [integer :: -('1')] ! { dg-error "Invalid type" } + print *, [integer :: -('1')] * 2 ! { dg-error "Invalid type" } + print *, 2 * [real :: 0, (-'1')] ! { dg-error "Invalid type" } + print *, [real :: 0, (-'1')] * 2 ! { dg-error "Invalid type" } + print *, 2 * [real :: 0, -'1'] ! { dg-error "Invalid type" } + print *, [real :: 0, -'1'] * 2 ! { dg-error "Invalid type" } + print *, 2 * [real :: 0, 1+'1'] ! { dg-error "Invalid type" } + print *, [real :: 0, 1+'1'] * 2 ! { dg-error "Invalid type" } + print *, [real :: 1, +(.true.)] ! { dg-error "Operand of unary numeric operator" } + print *, [real :: 1, -(.true.)] ! { dg-error "Operand of unary numeric operator" } + print *, 2 * [real :: 1, +(.true.)] ! { dg-error "Invalid type" } + print *, [real :: 1, +(.true.)] * 2 ! { dg-error "Invalid type" } + print *, [1, 2] * [real :: 1, +(.true.)] ! { dg-error "Operand of unary numeric operator" } + print *, [real :: 1, +(.true.)] * [1, 2] ! { dg-error "Operand of unary numeric operator" } + print *, [real :: 1, 2] * [real :: 1, +(.true.)] ! { dg-error "operands are incommensurate" } + print *, [real :: 1, +(.true.)] * [real :: 1, 2] ! { dg-error "operands are incommensurate" } + print *, [real :: 0, -'1'] * [real :: 1, +(+(.true.))] ! { dg-error "operands are incommensurate" } + print *, [real :: 1, [(+(.true.))]] * [real :: 0, [(-'1')]] ! { dg-error "operands are incommensurate" } + + ! Legal: + print *, 2 * [real :: 1, [2], 3] + print *, [real :: 1, [2], 3] * 2 + print *, [real :: 1, [2], 3] * [real :: 1, [2], 3] + print *, [real :: 1, [2], 3] * [integer :: 1, [2], 3] + print *, [real :: 1, [2], 3] * [1, [2], 3] + print *, [real :: 1, huge(2.0)] * [real :: 1, real(1.0)] + print *, [real :: 1, -(huge(2.0))] * [real :: 1, +(real(1))] +end -- 2.35.3