https://gcc.gnu.org/g:d424b0b467901e05bf17c2d168f1701ec81b7c08

commit d424b0b467901e05bf17c2d168f1701ec81b7c08
Author: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
Date:   Fri Nov 8 12:01:15 2024 +0100

    Fix Generic type retrieval
    
    gcc/rust/ChangeLog:
    
            * hir/rust-ast-lower-type.cc (ASTLowerGenericParam::visit): Forward
            an optional to the constructor.
            * hir/tree/rust-hir-item.cc (TypeParam::TypeParam): Use an optional
            in the constructor.
            (TypeParam::operator=): Ensure the TypeParam has a type properly.
            (TypeParam::get_type_mappings): Likewise.
            * hir/tree/rust-hir-item.h: Wrap the type smart pointer into an
            optional.
            * hir/tree/rust-hir.cc (TypeParam::as_string): Unwrap optional type
            correctly.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>

Diff:
---
 gcc/rust/hir/rust-ast-lower-type.cc | 10 +++++-----
 gcc/rust/hir/tree/rust-hir-item.cc  | 19 +++++++++++--------
 gcc/rust/hir/tree/rust-hir-item.h   | 14 ++++++++------
 gcc/rust/hir/tree/rust-hir.cc       |  2 +-
 4 files changed, 25 insertions(+), 20 deletions(-)

diff --git a/gcc/rust/hir/rust-ast-lower-type.cc 
b/gcc/rust/hir/rust-ast-lower-type.cc
index cc7cb937f7cc..c1bfe213aa17 100644
--- a/gcc/rust/hir/rust-ast-lower-type.cc
+++ b/gcc/rust/hir/rust-ast-lower-type.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-ast-lower-type.h"
+#include "optional.h"
 #include "rust-attribute-values.h"
 
 namespace Rust {
@@ -502,9 +503,9 @@ ASTLowerGenericParam::visit (AST::TypeParam &param)
        }
     }
 
-  HIR::Type *type = param.has_type ()
-                     ? ASTLoweringType::translate (param.get_type ())
-                     : nullptr;
+  auto type = param.has_type () ? tl::optional (std::unique_ptr<HIR::Type> (
+               ASTLoweringType::translate (param.get_type ())))
+                               : tl::nullopt;
 
   auto crate_num = mappings.get_current_crate ();
   Analysis::NodeMapping mapping (crate_num, param.get_node_id (),
@@ -514,8 +515,7 @@ ASTLowerGenericParam::visit (AST::TypeParam &param)
   translated
     = new HIR::TypeParam (mapping, param.get_type_representation (),
                          param.get_locus (), std::move (type_param_bounds),
-                         std::unique_ptr<Type> (type),
-                         param.get_outer_attrs ());
+                         std::move (type), param.get_outer_attrs ());
 }
 
 HIR::TypeParamBound *
diff --git a/gcc/rust/hir/tree/rust-hir-item.cc 
b/gcc/rust/hir/tree/rust-hir-item.cc
index f81f1eae1343..cff06d35269d 100644
--- a/gcc/rust/hir/tree/rust-hir-item.cc
+++ b/gcc/rust/hir/tree/rust-hir-item.cc
@@ -17,6 +17,7 @@
 // <http://www.gnu.org/licenses/>.
 
 #include "rust-hir-item.h"
+#include "optional.h"
 
 namespace Rust {
 namespace HIR {
@@ -25,7 +26,7 @@ TypeParam::TypeParam (
   Analysis::NodeMapping mappings, Identifier type_representation,
   location_t locus,
   std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds,
-  std::unique_ptr<Type>, AST::AttrVec outer_attrs)
+  tl::optional<std::unique_ptr<Type>> type, AST::AttrVec outer_attrs)
   : GenericParam (mappings), outer_attrs (std::move (outer_attrs)),
     type_representation (std::move (type_representation)),
     type_param_bounds (std::move (type_param_bounds)), type (std::move (type)),
@@ -37,8 +38,10 @@ TypeParam::TypeParam (TypeParam const &other)
     type_representation (other.type_representation), locus (other.locus)
 {
   // guard to prevent null pointer dereference
-  if (other.type != nullptr)
-    type = other.type->clone_type ();
+  if (other.has_type ())
+    type = {other.type.value ()->clone_type ()};
+  else
+    type = tl::nullopt;
 
   type_param_bounds.reserve (other.type_param_bounds.size ());
   for (const auto &e : other.type_param_bounds)
@@ -54,10 +57,10 @@ TypeParam::operator= (TypeParam const &other)
   mappings = other.mappings;
 
   // guard to prevent null pointer dereference
-  if (other.type != nullptr)
-    type = other.type->clone_type ();
+  if (other.has_type ())
+    type = {other.type.value ()->clone_type ()};
   else
-    type = nullptr;
+    type = tl::nullopt;
 
   type_param_bounds.reserve (other.type_param_bounds.size ());
   for (const auto &e : other.type_param_bounds)
@@ -69,8 +72,8 @@ TypeParam::operator= (TypeParam const &other)
 Analysis::NodeMapping
 TypeParam::get_type_mappings () const
 {
-  rust_assert (type != nullptr);
-  return type->get_mappings ();
+  rust_assert (type.has_value ());
+  return type.value ()->get_mappings ();
 }
 
 std::vector<std::unique_ptr<TypeParamBound>> &
diff --git a/gcc/rust/hir/tree/rust-hir-item.h 
b/gcc/rust/hir/tree/rust-hir-item.h
index 567432ecb325..4f8821855513 100644
--- a/gcc/rust/hir/tree/rust-hir-item.h
+++ b/gcc/rust/hir/tree/rust-hir-item.h
@@ -19,6 +19,7 @@
 #ifndef RUST_HIR_ITEM_H
 #define RUST_HIR_ITEM_H
 
+#include "optional.h"
 #include "rust-abi.h"
 #include "rust-hir-stmt.h"
 #include "rust-common.h"
@@ -102,14 +103,13 @@ class TypeParam : public GenericParam
   std::vector<std::unique_ptr<TypeParamBound>>
     type_param_bounds; // inlined form
 
-  // bool has_type;
-  std::unique_ptr<Type> type;
+  tl::optional<std::unique_ptr<Type>> type;
 
   location_t locus;
 
 public:
   // Returns whether the type of the type param has been specified.
-  bool has_type () const { return type != nullptr; }
+  bool has_type () const { return type.has_value (); }
 
   // Returns whether the type param has type param bounds.
   bool has_type_param_bounds () const { return !type_param_bounds.empty (); }
@@ -122,7 +122,7 @@ public:
             location_t locus = UNDEF_LOCATION,
             std::vector<std::unique_ptr<TypeParamBound>> type_param_bounds
             = std::vector<std::unique_ptr<TypeParamBound>> (),
-            std::unique_ptr<Type> type = nullptr,
+            tl::optional<std::unique_ptr<Type>> type = tl::nullopt,
             AST::AttrVec outer_attrs = std::vector<AST::Attribute> ());
 
   // Copy constructor uses clone
@@ -130,8 +130,10 @@ public:
 
   // Overloaded assignment operator to clone
   TypeParam &operator= (TypeParam const &other);
+
   // move constructors
   TypeParam (TypeParam &&other) = default;
+
   TypeParam &operator= (TypeParam &&other) = default;
 
   std::string as_string () const override;
@@ -144,8 +146,8 @@ public:
 
   Type &get_type ()
   {
-    rust_assert (type);
-    return *type;
+    rust_assert (*type);
+    return *type.value ();
   }
 
   Analysis::NodeMapping get_type_mappings () const;
diff --git a/gcc/rust/hir/tree/rust-hir.cc b/gcc/rust/hir/tree/rust-hir.cc
index 764619115d70..509a897ce39c 100644
--- a/gcc/rust/hir/tree/rust-hir.cc
+++ b/gcc/rust/hir/tree/rust-hir.cc
@@ -2178,7 +2178,7 @@ TypeParam::as_string () const
     }
   else
     {
-      str += type->as_string ();
+      str += type.value ()->as_string ();
     }
 
   return str;

Reply via email to