From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (wildebeest.demon.nl [212.238.236.112]) by sourceware.org (Postfix) with ESMTPS id 4200A3858001 for ; Sat, 14 Aug 2021 18:53:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4200A3858001 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org Received: from reform (83-161-179-12.mobile.xs4all.nl [83.161.179.12]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id 03A14302FB9F; Sat, 14 Aug 2021 20:53:29 +0200 (CEST) Received: by reform (Postfix, from userid 1000) id 483062E83133; Sat, 14 Aug 2021 20:53:17 +0200 (CEST) From: Mark Wielaard To: gcc-rust@gcc.gnu.org Cc: Mark Wielaard Subject: [PATCH] parse if expression with unary minus or not expression Date: Sat, 14 Aug 2021 20:53:09 +0200 Message-Id: <20210814185309.75928-1-mark@klomp.org> X-Mailer: git-send-email 2.32.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_STATUS, 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-rust@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: gcc-rust mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 14 Aug 2021 18:53:50 -0000 An if conditional expression doesn't need brackets, but that means that it doesn't accept struct expressions. Those are not easy to distinquish from the block that follows. What isn't immediately clear from the grammar is that unary conditions like minus '-' or not '!' also shouldn't accept struct expressions (when part of an if conditional expression) because those also cannot be easily distinquished from the block that follows. Add a testcase "ifunaryexpr.rs" that shows a couple of contructs that should be accepted as if conditional expressions and fix the parser to pass the restriction of not accepting struct expressions after a unary expression. --- gcc/rust/parse/rust-parse-impl.h | 4 ++++ .../rust/compile/torture/ifunaryexpr.rs | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 gcc/testsuite/rust/compile/torture/ifunaryexpr.rs diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 731e0b3682f..fa6d409c6dc 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -12547,6 +12547,8 @@ Parser::null_denotation (const_TokenPtr tok, case MINUS: { // unary minus ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; + if (!restrictions.can_be_struct_expr) + entered_from_unary.can_be_struct_expr = false; std::unique_ptr expr = parse_expr (LBP_UNARY_MINUS, {}, entered_from_unary); @@ -12571,6 +12573,8 @@ Parser::null_denotation (const_TokenPtr tok, case EXCLAM: { // logical or bitwise not ParseRestrictions entered_from_unary; entered_from_unary.entered_from_unary = true; + if (!restrictions.can_be_struct_expr) + entered_from_unary.can_be_struct_expr = false; std::unique_ptr expr = parse_expr (LBP_UNARY_EXCLAM, {}, entered_from_unary); diff --git a/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs b/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs new file mode 100644 index 00000000000..8f0bb87f558 --- /dev/null +++ b/gcc/testsuite/rust/compile/torture/ifunaryexpr.rs @@ -0,0 +1,22 @@ +extern "C" +{ + pub fn abort (); +} + +struct B { b: bool } + +pub fn main () +{ + let n = 1; + if 0 > -n { } else { unsafe { abort (); } } + + let b = true; + if !b { unsafe { abort (); } } + if !!b { } else { unsafe { abort (); } } + + let bb = B { b: false }; + + if !bb.b && !b { unsafe { abort (); } } + + if (B { b: true }).b { } else { unsafe { abort (); } } +} -- 2.32.0