From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.net; s=badeba3b8450; t=1664918396; bh=2kOMAew3YgM3PoKt6jaoFuOi/WBmY39oCi+0Odjz6yg=; h=X-UI-Sender-Class:From:To:Subject:Date; b=MwNIEFhFLDIp6lcWsh6mHcW/7I20q4YUhkjgfCQKtV68OSdhncq18dfNeDNC2b3QM xT0d8glyVMaXHNqRgYi4EVifMvDsD08Re7ZQtH/l67N8iYOB+P5RwvyZyzgbGsUBZN lMyX+y/6s7MJV0l1iQxk+cRe0SsBC8jUYsbi8pxU= 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: From: Harald Anlauf To: fortran , gcc-patches Subject: [PATCH] Fortran: error recovery for invalid types in array constructors [PR107000] Content-Type: multipart/mixed; boundary=nika-d5d4f990-2926-47c6-9294-ae673ae9af30 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 List-Id: --nika-d5d4f990-2926-47c6-9294-ae673ae9af30 Content-Type: text/plain; charset=UTF-8 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 --nika-d5d4f990-2926-47c6-9294-ae673ae9af30 Content-Type: text/x-patch Content-Disposition: attachment; filename=pr107000.diff Content-Transfer-Encoding: quoted-printable =46rom 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. =2D-- 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 =2D-- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -118,6 +118,9 @@ gfc_arith_error (arith code) case ARITH_WRONGCONCAT: p =3D G_("Illegal type in character concatenation at %L"); break; + case ARITH_INVALID_TYPE: + p =3D 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 =3D=3D EXPR_OP && op->ts.type =3D=3D BT_UNKNOWN) + return ARITH_INVALID_TYPE; + if (op->expr_type =3D=3D EXPR_CONSTANT) return eval (op, result); @@ -1302,6 +1308,9 @@ reduce_binary_ac (arith (*eval) (gfc_expr *, gfc_exp= r *, gfc_expr **), gfc_expr *r; arith rc =3D ARITH_OK; + if (op1->expr_type =3D=3D EXPR_OP && op1->ts.type =3D=3D BT_UNKNOWN) + return ARITH_INVALID_TYPE; + head =3D gfc_constructor_copy (op1->value.constructor); for (c =3D gfc_constructor_first (head); c; c =3D gfc_constructor_next = (c)) { @@ -1354,6 +1363,9 @@ reduce_binary_ca (arith (*eval) (gfc_expr *, gfc_exp= r *, gfc_expr **), gfc_expr *r; arith rc =3D ARITH_OK; + if (op2->expr_type =3D=3D EXPR_OP && op2->ts.type =3D=3D BT_UNKNOWN) + return ARITH_INVALID_TYPE; + head =3D gfc_constructor_copy (op2->value.constructor); for (c =3D gfc_constructor_first (head); c; c =3D gfc_constructor_next = (c)) { @@ -1414,6 +1426,10 @@ reduce_binary_aa (arith (*eval) (gfc_expr *, gfc_ex= pr *, gfc_expr **), if (!gfc_check_conformance (op1, op2, _("elemental binary operation"))) return ARITH_INCOMMENSURATE; + if ((op1->expr_type =3D=3D EXPR_OP && op1->ts.type =3D=3D BT_UNKNOWN) + || (op2->expr_type =3D=3D EXPR_OP && op2->ts.type =3D=3D BT_UNKNOWN= )) + return ARITH_INVALID_TYPE; + head =3D gfc_constructor_copy (op1->value.constructor); for (c =3D gfc_constructor_first (head), d =3D gfc_constructor_first (op2->value.constructor); @@ -2238,6 +2254,9 @@ gfc_real2complex (gfc_expr *src, int kind) arith rc; bool did_warn =3D false; + if (src->ts.type !=3D BT_REAL) + return NULL; + result =3D 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 =2D-- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -226,7 +226,7 @@ enum gfc_intrinsic_op enum arith { ARITH_OK =3D 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/gfortr= an.dg/pr107000.f90 new file mode 100644 index 00000000000..c13627f556b =2D-- /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_binar= y_* +! Contributed by G.Steinmetz + +program p + real :: y(1) + complex :: x(1) + x =3D (1.0, 2.0) * [real :: -'1'] ! { dg-error "Operand of unary numeri= c operator" } + x =3D (1.0, 2.0) * [complex :: +'1'] ! { dg-error "Invalid type" } + x =3D [complex :: -'1'] * (1.0, 2.0) ! { dg-error "Invalid type" } + y =3D [complex :: -'1'] * 2 ! { dg-error "Invalid type" } + y =3D 2 * [complex :: -'1'] ! { dg-error "Invalid type" } + y =3D 2 * [complex :: -(.true.)] ! { dg-error "Invalid type" } + y =3D [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 "op= erands are incommensurate" } + print *, [real :: 1, [(+(.true.))]] * [real :: 0, [(-'1')]] ! { dg-erro= r "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 =2D- 2.35.3 --nika-d5d4f990-2926-47c6-9294-ae673ae9af30--