From patchwork Thu Aug 1 14:56:40 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967767 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=Nm2NpShv; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; 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 [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 4WZXdw1Qysz1yZv for ; Fri, 2 Aug 2024 01:17:56 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 623F1385E837 for ; Thu, 1 Aug 2024 15:17:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ej1-x636.google.com (mail-ej1-x636.google.com [IPv6:2a00:1450:4864:20::636]) by sourceware.org (Postfix) with ESMTPS id 14567385DDEC for ; Thu, 1 Aug 2024 14:59:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 14567385DDEC 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 14567385DDEC Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::636 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524376; cv=none; b=kJMnDiidtIx4nPnHflV6icdRXpKH8gsjWQKoL5Xrsda+7hYF7SL27rIiAR57JD72h4dUI6uHAbzmImbvIqWILoCS401S9QhWrPEOm6gfv6GbUQOba75Q3ni7Rd5tCF4uFJmJkAcfwlvnkXGeKS7V639haQ6BKtChVHlIzzyke6Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524376; c=relaxed/simple; bh=tYNInYlFqHbhHq9lzqHuzr2Dr2O+hvRF8h4ECgm0vog=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=UVUJzn1d6tTXdS3H9FskalSMvN+YF0MN9I9zFgKZtrdD2+yvy/Ua44LEHRKHl3zOXCS40mQ6+mk62kRyWtp9ZJrKNRYj8pPepQHsHMZO8ts1dm7H2HRDAFZUoPw6eLbPCtIvggpbwYZrn4XA0no2PGcBR3Sd0qg2fMbI+qyLZ3E= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-a7aa212c1c9so910533466b.2 for ; Thu, 01 Aug 2024 07:59:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524345; x=1723129145; 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=eoR3VO2CPjA3Pl8H9skfdlumixULaq60YjvSu1MV1/o=; b=Nm2NpShvSXfCoNOPZQtZF96NOD56NPFVczekzey8CRNXWLveg1OBVl8m12sj849tLw 04fuo0TpWs3QK0EpOqdrlHrYBvV3IdPcUcPJ5XRz13jG1jM0GgwsecSiiAzeHx6ZxpKf 1l6wGGh9pYDKKkkmzmuhRxgf+LXAjXvrfNwY5J4PG52tjdO+EoMe9g/QklpJN/RrL5uR JGHlh0iPwTEj7FhFKjpp5mQ0V2zCLvaGGlxuEZhwmlNuBNFNnFzPPcDIHWDNi4tvJ/cW b2ojJDE1IshOTURb2M7QoY6t0nQ1zSYVGUCRk4JZUi+lmkujWE3jWUmT1SePltU+XGHR e6zg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524345; x=1723129145; 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=eoR3VO2CPjA3Pl8H9skfdlumixULaq60YjvSu1MV1/o=; b=qqW5sqE6c7l0X68RhR08atukeHMAlBRVtctzJa9S2qK2yBNiwotBbKKP90tV41BwWg xZN+T7lJkMt7BBB6PsLak4NM7MNComDthXvN6E/0z+yD9hEjrJI9J2Gm7N4Q+75eF5Xf In0QXnz4wMakYkqIEG/IdIezGOlO/hsXw5ORGtSX3byg8pQohR/y8fjdsOUmcEJD54Fp J0I2DxjvulFB8C5eILh0sSBZDWl15uJVDyvyU+d/pRSZEWeecr4n9vTnnrPQfc5w5Obv EaVfx9STv6MPtkW4B2awsSYqOjWzOe2FDWNYLIoCVYMn7iu20+etZ1sUklzxmBTwuoNe r9Nw== X-Gm-Message-State: AOJu0YzLq0FF9Uxq51p/ZlTf5Ufstb2ripX2CrRvfcQxaTJ31ZhBhv91 RwSTDNy8cJ9a6WzBQzv1zjkazlDigY+B4FhA/RvqpqV9lQcQV4gmGM5Lww1yPy4vOokoVYfJsrJ 06Vr6 X-Google-Smtp-Source: AGHT+IGubjacZuA2bCJ2R4QPIZkXsFbbuCrnhfPUCQHnbFpxbpBrOieDv1NFmaKtE3EQXbErgo6sjg== X-Received: by 2002:a17:907:7fa8:b0:a77:cdaa:88a7 with SMTP id a640c23a62f3a-a7dc509fc83mr31917666b.48.1722524345340; Thu, 01 Aug 2024 07:59:05 -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.59.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:05 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Pierre-Emmanuel Patry Subject: [PATCH 044/125] gccrs: Replace reference to unique pointer with reference Date: Thu, 1 Aug 2024 16:56:40 +0200 Message-ID: <20240801145809.366388-46-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: Pierre-Emmanuel Patry Reference to unique pointers are a known anti pattern, only the element shall be taken by reference instead of the whole wrapper. gcc/rust/ChangeLog: * ast/rust-item.h: Change getter function prototype to return a reference directly instead of a reference to the wrapper type. * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Fix the code to accept references instead. * hir/rust-ast-lower-base.cc (ASTLoweringBase::lower_self): Change function implementation to return a reference. * hir/rust-ast-lower-base.h: Accept a reference instead of a unique pointer reference. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Adapt the code to a reference instead of a unique pointer. Signed-off-by: Pierre-Emmanuel Patry --- gcc/rust/ast/rust-item.h | 8 +++---- gcc/rust/checks/errors/rust-ast-validation.cc | 2 +- gcc/rust/hir/rust-ast-lower-base.cc | 24 +++++++++---------- gcc/rust/hir/rust-ast-lower-base.h | 2 +- gcc/rust/resolve/rust-ast-resolve-item.cc | 19 +++++++-------- 5 files changed, 27 insertions(+), 28 deletions(-) diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 573888bea5a..d09f4550062 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -1415,15 +1415,15 @@ public: return return_type; } - std::unique_ptr &get_self_param () + Param &get_self_param () { rust_assert (has_self_param ()); - return function_params[0]; + return *function_params[0]; } - const std::unique_ptr &get_self_param () const + const Param &get_self_param () const { rust_assert (has_self_param ()); - return function_params[0]; + return *function_params[0]; } // ExternalItem::node_id is same as Stmt::node_id diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index d1edb890ae6..d5892087889 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -100,7 +100,7 @@ ASTValidation::visit (AST::Function &function) && context.back () != Context::INHERENT_IMPL && function.has_self_param ()) rust_error_at ( - function.get_self_param ()->get_locus (), + function.get_self_param ().get_locus (), "% parameter is only allowed in associated functions"); if (function.is_external ()) diff --git a/gcc/rust/hir/rust-ast-lower-base.cc b/gcc/rust/hir/rust-ast-lower-base.cc index ff6ef25a348..652530ae7a5 100644 --- a/gcc/rust/hir/rust-ast-lower-base.cc +++ b/gcc/rust/hir/rust-ast-lower-base.cc @@ -648,31 +648,31 @@ ASTLoweringBase::lower_generic_args (AST::GenericArgs &args) } HIR::SelfParam -ASTLoweringBase::lower_self (std::unique_ptr ¶m) +ASTLoweringBase::lower_self (AST::Param ¶m) { - rust_assert (param->is_self ()); + rust_assert (param.is_self ()); - auto self = static_cast (param.get ()); + auto self = static_cast (param); auto crate_num = mappings->get_current_crate (); - Analysis::NodeMapping mapping (crate_num, self->get_node_id (), + Analysis::NodeMapping mapping (crate_num, self.get_node_id (), mappings->get_next_hir_id (crate_num), mappings->get_next_localdef_id (crate_num)); - if (self->has_type ()) + if (self.has_type ()) { - HIR::Type *type = ASTLoweringType::translate (self->get_type ().get ()); + HIR::Type *type = ASTLoweringType::translate (self.get_type ().get ()); return HIR::SelfParam (mapping, std::unique_ptr (type), - self->get_is_mut (), self->get_locus ()); + self.get_is_mut (), self.get_locus ()); } - else if (!self->get_has_ref ()) + else if (!self.get_has_ref ()) { return HIR::SelfParam (mapping, std::unique_ptr (nullptr), - self->get_is_mut (), self->get_locus ()); + self.get_is_mut (), self.get_locus ()); } - AST::Lifetime l = self->get_lifetime (); - return HIR::SelfParam (mapping, lower_lifetime (l), self->get_is_mut (), - self->get_locus ()); + AST::Lifetime l = self.get_lifetime (); + return HIR::SelfParam (mapping, lower_lifetime (l), self.get_is_mut (), + self.get_locus ()); } HIR::Type * diff --git a/gcc/rust/hir/rust-ast-lower-base.h b/gcc/rust/hir/rust-ast-lower-base.h index c19e9c03b5f..c01c7c85767 100644 --- a/gcc/rust/hir/rust-ast-lower-base.h +++ b/gcc/rust/hir/rust-ast-lower-base.h @@ -278,7 +278,7 @@ protected: HIR::GenericArgsBinding lower_binding (AST::GenericArgsBinding &binding); - HIR::SelfParam lower_self (std::unique_ptr &self); + HIR::SelfParam lower_self (AST::Param &self); HIR::Type *lower_type_no_bounds (AST::TypeNoBounds *type); diff --git a/gcc/rust/resolve/rust-ast-resolve-item.cc b/gcc/rust/resolve/rust-ast-resolve-item.cc index a3f27b3e4a0..c65f112ea3b 100644 --- a/gcc/rust/resolve/rust-ast-resolve-item.cc +++ b/gcc/rust/resolve/rust-ast-resolve-item.cc @@ -489,32 +489,31 @@ ResolveItem::visit (AST::Function &function) if (function.has_self_param ()) { // self turns into (self: Self) as a function param - std::unique_ptr &s_param = function.get_self_param (); - auto self_param = static_cast (s_param.get ()); + AST::Param &s_param = function.get_self_param (); + auto &self_param = static_cast (s_param); // FIXME: which location should be used for Rust::Identifier `self`? AST::IdentifierPattern self_pattern ( - self_param->get_node_id (), {"self"}, self_param->get_locus (), - self_param->get_has_ref (), self_param->get_is_mut (), + self_param.get_node_id (), {"self"}, self_param.get_locus (), + self_param.get_has_ref (), self_param.get_is_mut (), std::unique_ptr (nullptr)); PatternDeclaration::go (&self_pattern, Rib::ItemType::Param); - if (self_param->has_type ()) + if (self_param.has_type ()) { // This shouldn't happen the parser should already error for this - rust_assert (!self_param->get_has_ref ()); - ResolveType::go (self_param->get_type ().get ()); + rust_assert (!self_param.get_has_ref ()); + ResolveType::go (self_param.get_type ().get ()); } else { // here we implicitly make self have a type path of Self std::vector> segments; segments.push_back (std::unique_ptr ( - new AST::TypePathSegment ("Self", false, - self_param->get_locus ()))); + new AST::TypePathSegment ("Self", false, self_param.get_locus ()))); AST::TypePath self_type_path (std::move (segments), - self_param->get_locus ()); + self_param.get_locus ()); ResolveType::go (&self_type_path); } }