From patchwork Thu Aug 1 14:56:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967749 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; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=Q2DgzvfQ; 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 4WZXX365bYz1ybV for ; Fri, 2 Aug 2024 01:12:51 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1BB5E3861837 for ; Thu, 1 Aug 2024 15:12:50 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-lj1-x236.google.com (mail-lj1-x236.google.com [IPv6:2a00:1450:4864:20::236]) by sourceware.org (Postfix) with ESMTPS id 4ECA4385F025 for ; Thu, 1 Aug 2024 14:59:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4ECA4385F025 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4ECA4385F025 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::236 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524363; cv=none; b=cbjDcmJjq46b/LQO1X+FVDVrMr47LLA0RZlYA+vOCYb5KBZvZUJmk5hP2Gf6apbdDh0RCgyf6QJKuinReTi23liwBkk2Poz2C7sPsBtMJqOHjP0aEM6jyIH7HHXegyr6TjpKXxjTQ4pbArHPkw/HKUVRPlVQkp+0zPeD6y2sasU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524363; c=relaxed/simple; bh=Ghaf+twuh20Sy9ZF486qDGuHy9hItWDFyZiCgy23kos=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=EVs6XDyWupdWoI8NodVBkNLgEzETlc8q2iDUkKJK5AqbwVE7c8gz+Oqw1/J5eKrYBHjyxk3W8bZMx2ZnqOl2MFV4KVbZ9mwh/SqqRIlFWJ8zOOg3lTHV1HG//vq3ZQoTTIW1BMNx5tH6MObRcmplwTCdkPQjyNUzJwv0VIKauqY= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-lj1-x236.google.com with SMTP id 38308e7fff4ca-2ef2c56da6cso80607071fa.1 for ; Thu, 01 Aug 2024 07:59:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524339; x=1723129139; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=9f6wOY+wskL427UQe1VrdRfrmJ4PxokBZt30okOszZ4=; b=Q2DgzvfQqwRTlrifmFiBURuoSJov75EEzEj1dxA75PKt7YdJPkDLA15/MTXjGmtkyJ ZkPSdKeI6YM7aXx9RucVBL7TOnXU7DzaqnYmXkKopY765TGWShz/BREzSttQ4G4azBiA FBU8fMyZjQTMNioscxE3MKmboWyL5dZPXj7MZEEPxQIE5z3Nn7ww3AnkqcMm7I0Po17N fs2BGN9y/llIZ3oBcGn9sOQ9riZe1ArUMBDlk4EEIgtSlOrFodfb+PDKeHJROBcgzOWB FVmOpvjQKrUQSpa8yeU2m4ykuZKCy39ExsHNgTO/HkByYI1wY7iqxo6Uz+DgjnsPHuJc lTQA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524339; x=1723129139; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=9f6wOY+wskL427UQe1VrdRfrmJ4PxokBZt30okOszZ4=; b=aE6Gr+J8swYJWKD6L7HOvu1WtkbEwApw4sZKgREJ6b0wRlAlSWQaUHIKhA/lK8/+Eu geW4PaDSh4SdWK7xiMKcHEwTxtWuVGFtQwbStZgu8ZC3kDtWvPTFt7k1ieV2nzqEGzrb FoXg191DeqjysCO3bWISUQbMTWZuK4N+4lO8fwBHpgE/xs+GOV15PPdgqa6tkguMZe1h jFe1uiAKbv98kcWFoqT3Asq1QCBBrNkGeJUJu2GuA5I5IwSd482UrSVydTG25NNTMGjP YOWvrcT1YobuNVlD+MSFHZHb2X6vvAc9cI+rwZBTz4BfZqFIq2nZiznbKZbw8TBA/5h5 uwvg== X-Gm-Message-State: AOJu0YwukDOWg3COLaxPinOxK7mzfGMcn6ZE6aZgSA1Vha61P6LbEZ+E /fG7MVMNJ/+3bBtRHOhZrWbkQmSii4IZW3VCD6BQMqIBcw3vjx7lwEJDMv/kTW69oRyHlVZGhuL +Cpjt X-Google-Smtp-Source: AGHT+IE4Ze+5GX6DBipgdIp+joY514idCSHjjZxmFQXmHlfHaoGmlUgq8fudEShzp48HRDLoP9L4zg== X-Received: by 2002:a05:6512:3f14:b0:52e:bf53:1c13 with SMTP id 2adb3069b0e04-530bb39d245mr94279e87.7.1722524339345; Thu, 01 Aug 2024 07:58:59 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.58.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:58:59 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Owen Avery Subject: [PATCH 036/125] gccrs: Ensure TupleStructPattern and TuplePattern have items Date: Thu, 1 Aug 2024 16:56:32 +0200 Message-ID: <20240801145809.366388-38-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable 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 From: Owen Avery Note that instances of both classes which have been moved from will have (items == nullptr). gcc/rust/ChangeLog: * ast/rust-pattern.h (class TupleStructPattern): Assert that items != nullptr. (class TuplePattern): Likewise. (TupleStructPattern::has_items): Remove. (TuplePattern::has_tuple_pattern_items): Likewise. * parse/rust-parse-impl.h (Parser::parse_ident_leading_pattern): Prevent construction of TupleStructPattern with (items == nullptr). (Parser::parse_pattern_no_alt): Likewise. * ast/rust-ast-collector.cc (TokenCollector::visit): Remove usage of TupleStructPattern::has_items. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Likewise. gcc/testsuite/ChangeLog: * rust/compile/pattern-struct.rs: Fix test. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-ast-collector.cc | 3 +- gcc/rust/ast/rust-ast-visitor.cc | 3 +- gcc/rust/ast/rust-pattern.h | 46 ++++++++++---------- gcc/rust/parse/rust-parse-impl.h | 16 ------- gcc/rust/resolve/rust-early-name-resolver.cc | 10 ----- gcc/testsuite/rust/compile/pattern-struct.rs | 2 +- 6 files changed, 26 insertions(+), 54 deletions(-) diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index c0e8e774824..744d0eb9d28 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -2503,8 +2503,7 @@ TokenCollector::visit (TupleStructPattern &pattern) { visit (pattern.get_path ()); push (Rust::Token::make (LEFT_PAREN, pattern.get_locus ())); - if (pattern.has_items ()) - visit (pattern.get_items ()); + visit (pattern.get_items ()); push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION)); } diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index c72e2d72f6d..697c2726309 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -1225,8 +1225,7 @@ void DefaultASTVisitor::visit (AST::TupleStructPattern &pattern) { visit (pattern.get_path ()); - if (pattern.has_items ()) - visit (pattern.get_items ()); + visit (pattern.get_items ()); } void diff --git a/gcc/rust/ast/rust-pattern.h b/gcc/rust/ast/rust-pattern.h index 6a90b536175..96f09355fae 100644 --- a/gcc/rust/ast/rust-pattern.h +++ b/gcc/rust/ast/rust-pattern.h @@ -1123,22 +1123,22 @@ class TupleStructPattern : public Pattern public: std::string as_string () const override; - // Returns whether the pattern has tuple struct items. - bool has_items () const { return items != nullptr; } - TupleStructPattern (PathInExpression tuple_struct_path, std::unique_ptr items) : path (std::move (tuple_struct_path)), items (std::move (items)), node_id (Analysis::Mappings::get ()->get_next_node_id ()) - {} + { + rust_assert (this->items != nullptr); + } // Copy constructor required to clone TupleStructPattern (TupleStructPattern const &other) : path (other.path) { // guard to protect from null dereference + rust_assert (other.items != nullptr); + node_id = other.node_id; - if (other.items != nullptr) - items = other.items->clone_tuple_struct_items (); + items = other.items->clone_tuple_struct_items (); } // Operator overload assignment operator to clone @@ -1148,10 +1148,9 @@ public: node_id = other.node_id; // guard to protect from null dereference - if (other.items != nullptr) - items = other.items->clone_tuple_struct_items (); - else - items = nullptr; + rust_assert (other.items != nullptr); + + items = other.items->clone_tuple_struct_items (); return *this; } @@ -1164,7 +1163,11 @@ public: void accept_vis (ASTVisitor &vis) override; - std::unique_ptr &get_items () { return items; } + std::unique_ptr &get_items () + { + rust_assert (items != nullptr); + return items; + } PathInExpression &get_path () { return path; } const PathInExpression &get_path () const { return path; } @@ -1358,7 +1361,6 @@ protected: // AST node representing a tuple pattern class TuplePattern : public Pattern { - // bool has_tuple_pattern_items; std::unique_ptr items; location_t locus; NodeId node_id; @@ -1366,21 +1368,21 @@ class TuplePattern : public Pattern public: std::string as_string () const override; - // Returns true if the tuple pattern has items - bool has_tuple_pattern_items () const { return items != nullptr; } - TuplePattern (std::unique_ptr items, location_t locus) : items (std::move (items)), locus (locus), node_id (Analysis::Mappings::get ()->get_next_node_id ()) - {} + { + rust_assert (this->items != nullptr); + } // Copy constructor requires clone TuplePattern (TuplePattern const &other) : locus (other.locus) { // guard to prevent null dereference + rust_assert (other.items != nullptr); + node_id = other.node_id; - if (other.items != nullptr) - items = other.items->clone_tuple_pattern_items (); + items = other.items->clone_tuple_pattern_items (); } // Overload assignment operator to clone @@ -1390,11 +1392,9 @@ public: node_id = other.node_id; // guard to prevent null dereference - if (other.items != nullptr) - items = other.items->clone_tuple_pattern_items (); - else - items = nullptr; + rust_assert (other.items != nullptr); + items = other.items->clone_tuple_pattern_items (); return *this; } @@ -1405,7 +1405,7 @@ public: // TODO: seems kinda dodgy. Think of better way. std::unique_ptr &get_items () { - rust_assert (has_tuple_pattern_items ()); + rust_assert (items != nullptr); return items; } diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index ac1754542d4..9d9722e9714 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -10631,14 +10631,6 @@ Parser::parse_pattern_no_alt () // tuple struct lexer.skip_token (); - // check if empty tuple - if (lexer.peek_token ()->get_id () == RIGHT_PAREN) - { - lexer.skip_token (); - return std::unique_ptr ( - new AST::TupleStructPattern (std::move (path), nullptr)); - } - // parse items std::unique_ptr items = parse_tuple_struct_items (); @@ -11094,14 +11086,6 @@ Parser::parse_ident_leading_pattern () // DEBUG rust_debug ("parsing tuple struct pattern"); - // check if empty tuple - if (lexer.peek_token ()->get_id () == RIGHT_PAREN) - { - lexer.skip_token (); - return std::unique_ptr ( - new AST::TupleStructPattern (std::move (path), nullptr)); - } - // parse items std::unique_ptr items = parse_tuple_struct_items (); diff --git a/gcc/rust/resolve/rust-early-name-resolver.cc b/gcc/rust/resolve/rust-early-name-resolver.cc index d70f9ca9806..5447084cfdd 100644 --- a/gcc/rust/resolve/rust-early-name-resolver.cc +++ b/gcc/rust/resolve/rust-early-name-resolver.cc @@ -558,16 +558,6 @@ EarlyNameResolver::visit (AST::StructPattern &) void EarlyNameResolver::visit (AST::TupleStructPattern &pattern) { - if (!pattern.has_items ()) - { - rich_location rich_locus (line_table, pattern.get_locus ()); - rich_locus.add_fixit_replace ( - "function calls are not allowed in patterns"); - rust_error_at ( - rich_locus, ErrorCode::E0164, - "expected tuple struct or tuple variant, found associated function"); - return; - } pattern.get_items ()->accept_vis (*this); } diff --git a/gcc/testsuite/rust/compile/pattern-struct.rs b/gcc/testsuite/rust/compile/pattern-struct.rs index 17275098cd2..db242418c1d 100644 --- a/gcc/testsuite/rust/compile/pattern-struct.rs +++ b/gcc/testsuite/rust/compile/pattern-struct.rs @@ -11,7 +11,7 @@ fn main() { fn bar(foo: A) { match foo { A::new() => (), - // { dg-error "expected tuple struct or tuple variant, found associated function" "" { target *-*-* } .-1 } + // { dg-error "expected tuple struct or tuple variant, found function" "" { target *-*-* } .-1 } _ => {} } }