From patchwork Sun Oct 16 18:46:38 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 3146 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp1118834wrs; Sun, 16 Oct 2022 11:47:45 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7QcUX85P3oU3RKYRGntRvreTCJ/0aMRX2AARhUmgG+jyRxlpSpSqFceojsXycVcraKRJ57 X-Received: by 2002:a17:906:cc4f:b0:78d:fdd5:9ea9 with SMTP id mm15-20020a170906cc4f00b0078dfdd59ea9mr6000311ejb.57.1665946065686; Sun, 16 Oct 2022 11:47:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665946065; cv=none; d=google.com; s=arc-20160816; b=0VBtOKQ/J/muk/WLLbqG4uCcRCFYuzGWhiY5gRPZRJY91U0nIh/pPIFv/wbHtuBhME EOYYhCGSa2Pk48auO3ljQJq3+zfplACS2m7ZrKDTkavk7a965q77Ow7V8r2anSPjDk89 bNWbejDU+iaL74yJl1sHezPjxhZ8/nwdQqjqLzgay11DzuzT13mZ+VYtD7Q0CByKIbWv h+Lz5eogJC1LnQzw7lMCmsoyCMHmNYwhiMdvY5DNUzmb4doOApSK6lCysEIcCxfa36UZ 0jO2kl/T+yAgaWCvGtsMnqVNPcGNLzU0mVE95bFyRHZf/Ym3d8hS4x/qPbWtSDn8Hl4C lG8A== 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=iMyahpfFAjQpapWJ4PnyAxHmOmpcZ/xJpB6E+dzKxcA=; b=HOTf3O77IJXFWAZ4ZCoVY0ir6mxynf5L10a7PTepaqiIWQGIUU4EjMTumtm0rywrT+ ToGuo1hwtCGj2X0hWzB8RKlF0dzwMzmacE5XO9spZ5bXVs7h/Rw3c8+4+zWNufl3VzWR mxNa5a2Pic9piLVMs356F6Vf4J3bQL4ayyfIJJjbIkI/3CTGkYwZY1KrWARTXghdE1Ko AYEJ2ktavip30FKFfDY/QA/bcue6IWm7mxBmUANF9kRR9cmzG8fuX2ALlC463gueTsHq m8lQMZG0xFVdwxQVc7uH+yF0MATHM0xLlH3Aak5YC+BW4nEEKIrOb9/3ezvUBZB7i8dS 7zCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=tqhoRkFT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id di8-20020a170906730800b00783de095de7si7677988ejc.847.2022.10.16.11.47.45 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 16 Oct 2022 11:47:45 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=tqhoRkFT; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 76F963858C55 for ; Sun, 16 Oct 2022 18:47:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 76F963858C55 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665946064; bh=iMyahpfFAjQpapWJ4PnyAxHmOmpcZ/xJpB6E+dzKxcA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=tqhoRkFTAfft0Krm7ZudCtfzUrOTwA5UPJqy8tk52eI9V8GxA7+gGwu15BGarf23X UMqanTeW90xDbKXU3+s5kq9qT7yELTH9XXquK/dryJ1TArJ1ieA/ICvPAXGNBFwSai mGbI5yrgU2+0dlvhswO3RWANxbFfneiGBFYjRS+c= 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.17.21]) by sourceware.org (Postfix) with ESMTPS id CE90E3858D3C; Sun, 16 Oct 2022 18:46:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CE90E3858D3C X-UI-Sender-Class: 01bb95c1-4bf8-414a-932a-4f6e2808ef9c Received: from [79.251.10.58] ([79.251.10.58]) by web-mail.gmx.net (3c-app-gmx-bap23.server.lan [172.19.172.93]) (via HTTP); Sun, 16 Oct 2022 20:46:38 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: check type of operands of logical operations, comparisons [PR107272] Date: Sun, 16 Oct 2022 20:46:38 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:PRxa6lf4Y18zuL+j+Kzm+eIv5Wvw6U/1iULb2X5zSnWmCpcLDfO+vcIoIsm/Ne5tNaAuc NV/RvdnkpIUVduWVRK8MKleZZeNxS9VZfA4l2QzuIz+9uKpm67RgXKNHPMyLCf+CR8bYLRmIYv8D 0ZNxL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lWtf9kPmn3329ISHlhZ3i zEuD+1GE0NdLC68lGvX0M+tusUVTGZaqVWJ3iC394+f7GbYcd/sNiLegbusnQUaIrLrEurW7S xGqVoteDIQcourZpFCPB8XSMdhMB+bIljt4e9FNrQjRawcggCsb+ee+B9jXlJJhPEuMG9E1Iq dDTWSfV6bEzx45xtVMe6Vv/577fIppDrUWqQKk5g5PvU0Q6zZ3DIel1paaqX8U/JN0VSa/Pi9 9pM2kLQ2oklwVIhQGfUf/aQQCb6M3CZ1sYaj3loPgmEE+0WK2TbsuKgB8k1ZJ+R6CKzPQ0t4E WCfxU4QwOuqso9t+amhmjnnlcEC1yM5NRkSqSrcZB62XD3W31FZMHMdqNAWU/Otj7tJipb3Sz S7oppwj1jb0eRIwL4ECc0ttMYTqaNW+64gDyIGyym7V+adtuJonVP+G1yM+Ki8t/mZl0qe2mj oF5MyEWU X-Spam-Status: No, score=-12.4 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?1746871061968749526?= X-GMAIL-MSGID: =?utf-8?q?1746871061968749526?= Dear all, this PR is actually very related to PR107217 that addressed ICEs with bad array constructors with typespec when used in arithmetic expressions. The present patch extends the checking to logical operations and to comparisons and catches several ICE-on-invalid as well as a few cases of accepts-invalid. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 779baf06888f3adef13c12c468c0a5ef0a45f93e Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Sun, 16 Oct 2022 20:32:27 +0200 Subject: [PATCH] Fortran: check type of operands of logical operations, comparisons [PR107272] gcc/fortran/ChangeLog: PR fortran/107272 * arith.cc (gfc_arith_not): Operand must be of type BT_LOGICAL. (gfc_arith_and): Likewise. (gfc_arith_or): Likewise. (gfc_arith_eqv): Likewise. (gfc_arith_neqv): Likewise. (gfc_arith_eq): Compare consistency of types of operands. (gfc_arith_ne): Likewise. (gfc_arith_gt): Likewise. (gfc_arith_ge): Likewise. (gfc_arith_lt): Likewise. (gfc_arith_le): Likewise. gcc/testsuite/ChangeLog: PR fortran/107272 * gfortran.dg/pr107272.f90: New test. --- gcc/fortran/arith.cc | 33 ++++++++++++++++++++++++++ gcc/testsuite/gfortran.dg/pr107272.f90 | 21 ++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 gcc/testsuite/gfortran.dg/pr107272.f90 diff --git a/gcc/fortran/arith.cc b/gcc/fortran/arith.cc index c8e882badab..fc9224ebc5c 100644 --- a/gcc/fortran/arith.cc +++ b/gcc/fortran/arith.cc @@ -422,6 +422,9 @@ gfc_arith_not (gfc_expr *op1, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != BT_LOGICAL) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, op1->ts.kind, &op1->where); result->value.logical = !op1->value.logical; *resultp = result; @@ -435,6 +438,9 @@ gfc_arith_and (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2), &op1->where); result->value.logical = op1->value.logical && op2->value.logical; @@ -449,6 +455,9 @@ gfc_arith_or (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2), &op1->where); result->value.logical = op1->value.logical || op2->value.logical; @@ -463,6 +472,9 @@ gfc_arith_eqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2), &op1->where); result->value.logical = op1->value.logical == op2->value.logical; @@ -477,6 +489,9 @@ gfc_arith_neqv (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != BT_LOGICAL || op2->ts.type != BT_LOGICAL) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_kind_max (op1, op2), &op1->where); result->value.logical = op1->value.logical != op2->value.logical; @@ -1187,6 +1202,9 @@ gfc_arith_eq (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind, &op1->where); result->value.logical = (op1->ts.type == BT_COMPLEX) @@ -1203,6 +1221,9 @@ gfc_arith_ne (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind, &op1->where); result->value.logical = (op1->ts.type == BT_COMPLEX) @@ -1219,6 +1240,9 @@ gfc_arith_gt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind, &op1->where); result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GT) > 0); @@ -1233,6 +1257,9 @@ gfc_arith_ge (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind, &op1->where); result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_GE) >= 0); @@ -1247,6 +1274,9 @@ gfc_arith_lt (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind, &op1->where); result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LT) < 0); @@ -1261,6 +1291,9 @@ gfc_arith_le (gfc_expr *op1, gfc_expr *op2, gfc_expr **resultp) { gfc_expr *result; + if (op1->ts.type != op2->ts.type) + return ARITH_INVALID_TYPE; + result = gfc_get_constant_expr (BT_LOGICAL, gfc_default_logical_kind, &op1->where); result->value.logical = (gfc_compare_expr (op1, op2, INTRINSIC_LE) <= 0); diff --git a/gcc/testsuite/gfortran.dg/pr107272.f90 b/gcc/testsuite/gfortran.dg/pr107272.f90 new file mode 100644 index 00000000000..4b5c6a0f844 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/pr107272.f90 @@ -0,0 +1,21 @@ +! { dg-do compile } +! PR fortran/107272 - followup of PR/107217 for non-numeric types + +program p + print *, 2 <= [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, 2 < [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, 2 == [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, 2 /= [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, 2 >= [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, 2 > [real :: (['1'])] ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] >= 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] > 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] == 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] /= 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] <= 2 ! { dg-error "Cannot convert" } + print *, [real :: (['1'])] < 2 ! { dg-error "Cannot convert" } + print *, [logical :: (['1'])] .and. .true. ! { dg-error "Cannot convert" } + print *, [logical :: (['1'])] .or. .true. ! { dg-error "Cannot convert" } + print *, [logical :: (['1'])] .eqv. .true. ! { dg-error "Cannot convert" } + print *, [logical :: (['1'])] .neqv. .true. ! { dg-error "Cannot convert" } +end -- 2.35.3