diff mbox series

[044/125] gccrs: Replace reference to unique pointer with reference

Message ID 20240801145809.366388-46-arthur.cohen@embecosm.com
State New
Headers show
Series [001/125] Rust: Make 'tree'-level 'MAIN_NAME_P' work | expand

Commit Message

Arthur Cohen Aug. 1, 2024, 2:56 p.m. UTC
From: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

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 <pierre-emmanuel.patry@embecosm.com>
---
 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 mbox series

Patch

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<Param> &get_self_param ()
+  Param &get_self_param ()
   {
     rust_assert (has_self_param ());
-    return function_params[0];
+    return *function_params[0];
   }
-  const std::unique_ptr<Param> &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 (),
       "%<self%> 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<AST::Param> &param)
+ASTLoweringBase::lower_self (AST::Param &param)
 {
-  rust_assert (param->is_self ());
+  rust_assert (param.is_self ());
 
-  auto self = static_cast<AST::SelfParam *> (param.get ());
+  auto self = static_cast<AST::SelfParam &> (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<HIR::Type> (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<HIR::Type> (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<AST::Param> &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<AST::Param> &s_param = function.get_self_param ();
-      auto self_param = static_cast<AST::SelfParam *> (s_param.get ());
+      AST::Param &s_param = function.get_self_param ();
+      auto &self_param = static_cast<AST::SelfParam &> (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<AST::Pattern> (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<std::unique_ptr<AST::TypePathSegment>> segments;
 	  segments.push_back (std::unique_ptr<AST::TypePathSegment> (
-	    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);
 	}
     }