From: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>

Change the function's return type to wrap it within an optional.

gcc/rust/ChangeLog:

        * backend/rust-compile-base.cc: Change call site to accomodate new
        return type.
        * backend/rust-compile-base.h: Change parameter to use a reference.
        * backend/rust-compile-extern.h: Likewise.
        * backend/rust-compile-implitem.cc (CompileTraitItem::visit):
        Change call site for new return type.
        * backend/rust-compile-item.cc (CompileItem::visit): Likewise.
        * resolve/rust-ast-resolve-type.cc (ResolveTypeToCanonicalPath::visit):
        Likewise.
        * typecheck/rust-hir-type-check-enumitem.cc (TypeCheckEnumItem::visit):
        Likewise.
        * typecheck/rust-hir-type-check-implitem.cc (TypeCheckImplItem::visit):
        Likewise.
        * typecheck/rust-hir-type-check-item.cc (TypeCheckItem::visit):
        Likewise.
        * typecheck/rust-hir-type-check.cc 
(TraitItemReference::get_type_from_fn):
        Likewise.
        * util/rust-hir-map.h: Update the function's prototype and change the
        function's return type.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
---
 gcc/rust/backend/rust-compile-base.cc         | 10 ++--
 gcc/rust/backend/rust-compile-base.h          |  4 +-
 gcc/rust/backend/rust-compile-extern.h        |  6 +--
 gcc/rust/backend/rust-compile-implitem.cc     | 16 +++---
 gcc/rust/backend/rust-compile-item.cc         | 26 ++++------
 gcc/rust/resolve/rust-ast-resolve-type.cc     |  3 +-
 .../typecheck/rust-hir-type-check-enumitem.cc | 24 +++------
 .../typecheck/rust-hir-type-check-implitem.cc |  7 +--
 .../typecheck/rust-hir-type-check-item.cc     | 49 +++++++------------
 gcc/rust/typecheck/rust-hir-type-check.cc     |  6 +--
 gcc/rust/util/rust-hir-map.h                  | 11 ++---
 11 files changed, 61 insertions(+), 101 deletions(-)

diff --git a/gcc/rust/backend/rust-compile-base.cc 
b/gcc/rust/backend/rust-compile-base.cc
index add173c50a9..8166c3ae224 100644
--- a/gcc/rust/backend/rust-compile-base.cc
+++ b/gcc/rust/backend/rust-compile-base.cc
@@ -650,11 +650,11 @@ HIRCompileBase::compile_function (
   std::vector<HIR::FunctionParam> &function_params,
   const HIR::FunctionQualifiers &qualifiers, HIR::Visibility &visibility,
   AST::AttrVec &outer_attrs, location_t locus, HIR::BlockExpr *function_body,
-  const Resolver::CanonicalPath *canonical_path, TyTy::FnType *fntype)
+  const Resolver::CanonicalPath &canonical_path, TyTy::FnType *fntype)
 {
   tree compiled_fn_type = TyTyResolveCompile::compile (ctx, fntype);
   std::string ir_symbol_name
-    = canonical_path->get () + fntype->subst_as_string ();
+    = canonical_path.get () + fntype->subst_as_string ();
 
   // we don't mangle the main fn since we haven't implemented the main shim
   bool is_main_fn = fn_name.compare ("main") == 0;
@@ -677,7 +677,7 @@ HIRCompileBase::compile_function (
   // conditionally mangle the function name
   bool should_mangle = should_mangle_item (fndecl);
   if (!is_main_fn && should_mangle)
-    asm_name = ctx->mangle_item (fntype, *canonical_path);
+    asm_name = ctx->mangle_item (fntype, canonical_path);
   SET_DECL_ASSEMBLER_NAME (fndecl,
                           get_identifier_with_length (asm_name.data (),
                                                       asm_name.length ()));
@@ -767,10 +767,10 @@ HIRCompileBase::compile_function (
 
 tree
 HIRCompileBase::compile_constant_item (
-  TyTy::BaseType *resolved_type, const Resolver::CanonicalPath *canonical_path,
+  TyTy::BaseType *resolved_type, const Resolver::CanonicalPath &canonical_path,
   HIR::Expr *const_value_expr, location_t locus)
 {
-  const std::string &ident = canonical_path->get ();
+  const std::string &ident = canonical_path.get ();
 
   tree type = TyTyResolveCompile::compile (ctx, resolved_type);
   tree const_type = build_qualified_type (type, TYPE_QUAL_CONST);
diff --git a/gcc/rust/backend/rust-compile-base.h 
b/gcc/rust/backend/rust-compile-base.h
index 465a4caa720..eeb3ff02619 100644
--- a/gcc/rust/backend/rust-compile-base.h
+++ b/gcc/rust/backend/rust-compile-base.h
@@ -91,7 +91,7 @@ protected:
                              TyTy::BaseType *fn_return_ty);
 
   tree compile_constant_item (TyTy::BaseType *resolved_type,
-                             const Resolver::CanonicalPath *canonical_path,
+                             const Resolver::CanonicalPath &canonical_path,
                              HIR::Expr *const_value_expr, location_t locus);
 
   tree compile_function (const std::string &fn_name, HIR::SelfParam 
&self_param,
@@ -99,7 +99,7 @@ protected:
                         const HIR::FunctionQualifiers &qualifiers,
                         HIR::Visibility &visibility, AST::AttrVec &outer_attrs,
                         location_t locus, HIR::BlockExpr *function_body,
-                        const Resolver::CanonicalPath *canonical_path,
+                        const Resolver::CanonicalPath &canonical_path,
                         TyTy::FnType *fntype);
 
   static tree unit_expression (location_t locus);
diff --git a/gcc/rust/backend/rust-compile-extern.h 
b/gcc/rust/backend/rust-compile-extern.h
index f596ed2fcaa..bacd1c044ec 100644
--- a/gcc/rust/backend/rust-compile-extern.h
+++ b/gcc/rust/backend/rust-compile-extern.h
@@ -134,10 +134,8 @@ public:
     if (fntype->get_abi () == ABI::RUST)
       {
        // then we need to get the canonical path of it and mangle it
-       const Resolver::CanonicalPath *canonical_path = nullptr;
-       bool ok = ctx->get_mappings ().lookup_canonical_path (
-         function.get_mappings ().get_nodeid (), &canonical_path);
-       rust_assert (ok);
+       auto canonical_path = ctx->get_mappings ().lookup_canonical_path (
+         function.get_mappings ().get_nodeid ());
 
        ir_symbol_name = canonical_path->get () + fntype->subst_as_string ();
        asm_name = ctx->mangle_item (fntype, *canonical_path);
diff --git a/gcc/rust/backend/rust-compile-implitem.cc 
b/gcc/rust/backend/rust-compile-implitem.cc
index 0f24417ca1d..4c7d8e821a1 100644
--- a/gcc/rust/backend/rust-compile-implitem.cc
+++ b/gcc/rust/backend/rust-compile-implitem.cc
@@ -27,14 +27,12 @@ CompileTraitItem::visit (HIR::TraitItemConst &constant)
   rust_assert (concrete != nullptr);
   TyTy::BaseType *resolved_type = concrete;
 
-  const Resolver::CanonicalPath *canonical_path = nullptr;
-  bool ok = ctx->get_mappings ().lookup_canonical_path (
-    constant.get_mappings ().get_nodeid (), &canonical_path);
-  rust_assert (ok);
+  auto canonical_path = ctx->get_mappings ().lookup_canonical_path (
+    constant.get_mappings ().get_nodeid ());
 
   HIR::Expr *const_value_expr = constant.get_expr ().get ();
   tree const_expr
-    = compile_constant_item (resolved_type, canonical_path, const_value_expr,
+    = compile_constant_item (resolved_type, *canonical_path, const_value_expr,
                             constant.get_locus ());
   ctx->push_const (const_expr);
   ctx->insert_const_decl (constant.get_mappings ().get_hirid (), const_expr);
@@ -77,10 +75,8 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
       fntype->override_context ();
     }
 
-  const Resolver::CanonicalPath *canonical_path = nullptr;
-  bool ok = ctx->get_mappings ().lookup_canonical_path (
-    func.get_mappings ().get_nodeid (), &canonical_path);
-  rust_assert (ok);
+  auto canonical_path = ctx->get_mappings ().lookup_canonical_path (
+    func.get_mappings ().get_nodeid ());
 
   // FIXME: How do we get the proper visibility here?
   auto vis = HIR::Visibility (HIR::Visibility::VisType::PUBLIC);
@@ -90,7 +86,7 @@ CompileTraitItem::visit (HIR::TraitItemFunc &func)
                        function.get_self (), function.get_function_params (),
                        function.get_qualifiers (), vis,
                        func.get_outer_attrs (), func.get_locus (),
-                       func.get_block_expr ().get (), canonical_path, fntype);
+                       func.get_block_expr ().get (), *canonical_path, fntype);
   reference = address_expression (fndecl, ref_locus);
 }
 
diff --git a/gcc/rust/backend/rust-compile-item.cc 
b/gcc/rust/backend/rust-compile-item.cc
index e7b3a17edf7..c0cac686e8d 100644
--- a/gcc/rust/backend/rust-compile-item.cc
+++ b/gcc/rust/backend/rust-compile-item.cc
@@ -42,14 +42,12 @@ CompileItem::visit (HIR::StaticItem &var)
 
   tree type = TyTyResolveCompile::compile (ctx, resolved_type);
 
-  const Resolver::CanonicalPath *canonical_path = nullptr;
-  ok = ctx->get_mappings ().lookup_canonical_path (
-    var.get_mappings ().get_nodeid (), &canonical_path);
-  rust_assert (ok);
+  auto canonical_path = ctx->get_mappings ().lookup_canonical_path (
+    var.get_mappings ().get_nodeid ());
 
   HIR::Expr *const_value_expr = var.get_expr ().get ();
   ctx->push_const_context ();
-  tree value = compile_constant_item (resolved_type, canonical_path,
+  tree value = compile_constant_item (resolved_type, *canonical_path,
                                      const_value_expr, var.get_locus ());
   ctx->pop_const_context ();
 
@@ -102,17 +100,15 @@ CompileItem::visit (HIR::ConstantItem &constant)
     }
   else
     {
-      const Resolver::CanonicalPath *canonical_path_ptr = nullptr;
-      ok = ctx->get_mappings ().lookup_canonical_path (mappings.get_nodeid (),
-                                                      &canonical_path_ptr);
-      rust_assert (ok);
-      canonical_path = *canonical_path_ptr;
+      canonical_path = ctx->get_mappings ()
+                        .lookup_canonical_path (mappings.get_nodeid ())
+                        .value ();
     }
 
   HIR::Expr *const_value_expr = constant.get_expr ().get ();
   ctx->push_const_context ();
   tree const_expr
-    = compile_constant_item (resolved_type, &canonical_path, const_value_expr,
+    = compile_constant_item (resolved_type, canonical_path, const_value_expr,
                             constant.get_locus ());
   ctx->pop_const_context ();
 
@@ -178,10 +174,8 @@ CompileItem::visit (HIR::Function &function)
     }
   else
     {
-      const Resolver::CanonicalPath *path = nullptr;
-      bool ok = ctx->get_mappings ().lookup_canonical_path (
-       function.get_mappings ().get_nodeid (), &path);
-      rust_assert (ok);
+      auto path = ctx->get_mappings ().lookup_canonical_path (
+       function.get_mappings ().get_nodeid ());
 
       canonical_path = *path;
     }
@@ -213,7 +207,7 @@ CompileItem::visit (HIR::Function &function)
                        function.get_function_params (),
                        function.get_qualifiers (), function.get_visibility (),
                        function.get_outer_attrs (), function.get_locus (),
-                       function.get_definition ().get (), &canonical_path,
+                       function.get_definition ().get (), canonical_path,
                        fntype);
   reference = address_expression (fndecl, ref_locus);
 
diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc 
b/gcc/rust/resolve/rust-ast-resolve-type.cc
index 1d7bcc997ac..934d6ea3fbd 100644
--- a/gcc/rust/resolve/rust-ast-resolve-type.cc
+++ b/gcc/rust/resolve/rust-ast-resolve-type.cc
@@ -376,8 +376,7 @@ ResolveTypeToCanonicalPath::visit (AST::TypePath &path)
   if (resolved_node == UNKNOWN_NODEID)
     return;
 
-  const CanonicalPath *type_path = nullptr;
-  if (mappings.lookup_canonical_path (resolved_node, &type_path))
+  if (auto type_path = mappings.lookup_canonical_path (resolved_node))
     {
       auto &final_seg = path.get_segments ().back ();
       switch (final_seg->get_type ())
diff --git a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc 
b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
index 7bade03e64a..72d8791fe56 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-enumitem.cc
@@ -75,10 +75,8 @@ TypeCheckEnumItem::visit (HIR::EnumItem &item)
   rust_assert (ok);
   context->insert_type (mapping, isize);
 
-  const CanonicalPath *canonical_path = nullptr;
-  ok = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid (),
-                                      &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ());
 
   RustIdent ident{*canonical_path, item.get_locus ()};
   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
@@ -106,10 +104,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemDiscriminant &item)
              TyTy::TyWithLocation (expected_ty),
              TyTy::TyWithLocation (capacity_type), item.get_locus ());
 
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid (),
-                                           &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ());
 
   RustIdent ident{*canonical_path, item.get_locus ()};
   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
@@ -155,10 +151,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemTuple &item)
   rust_assert (ok);
   context->insert_type (mapping, isize);
 
-  const CanonicalPath *canonical_path = nullptr;
-  ok = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid (),
-                                      &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ());
 
   RustIdent ident{*canonical_path, item.get_locus ()};
   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
@@ -203,10 +197,8 @@ TypeCheckEnumItem::visit (HIR::EnumItemStruct &item)
   rust_assert (ok);
   context->insert_type (mapping, isize);
 
-  const CanonicalPath *canonical_path = nullptr;
-  ok = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid (),
-                                      &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (item.get_mappings ().get_nodeid ());
 
   RustIdent ident{*canonical_path, item.get_locus ()};
   variant = new TyTy::VariantDef (item.get_mappings ().get_hirid (),
diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc 
b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
index f0f3cfa219e..58d59d97cec 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.cc
@@ -461,11 +461,8 @@ TypeCheckImplItem::visit (HIR::Function &function)
       TypeCheckPattern::Resolve (param.get_param_name ().get (), param_tyty);
     }
 
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok
-    = mappings.lookup_canonical_path (function.get_mappings ().get_nodeid (),
-                                     &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (function.get_mappings ().get_nodeid ());
 
   RustIdent ident{*canonical_path, function.get_locus ()};
   auto fnType = new TyTy::FnType (
diff --git a/gcc/rust/typecheck/rust-hir-type-check-item.cc 
b/gcc/rust/typecheck/rust-hir-type-check-item.cc
index 3896abd24a7..317d16700aa 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-item.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check-item.cc
@@ -198,19 +198,17 @@ TypeCheckItem::visit (HIR::TupleStruct &struct_decl)
     {
       auto nr_ctx
        = Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();
-      auto canonical_path = nr_ctx.values.to_canonical_path (
-       struct_decl.get_mappings ().get_nodeid ());
 
-      path = canonical_path.value ();
+      path = nr_ctx.values
+              .to_canonical_path (struct_decl.get_mappings ().get_nodeid ())
+              .value ();
     }
   else
     {
-      const CanonicalPath *canonical_path = nullptr;
-      bool ok = mappings.lookup_canonical_path (
-       struct_decl.get_mappings ().get_nodeid (), &canonical_path);
-      rust_assert (ok);
-
-      path = *canonical_path;
+      path
+       = mappings
+           .lookup_canonical_path (struct_decl.get_mappings ().get_nodeid ())
+           .value ();
     }
 
   RustIdent ident{path, struct_decl.get_locus ()};
@@ -288,12 +286,10 @@ TypeCheckItem::visit (HIR::StructStruct &struct_decl)
     }
   else
     {
-      const CanonicalPath *canonical_path = nullptr;
-      bool ok = mappings.lookup_canonical_path (
-       struct_decl.get_mappings ().get_nodeid (), &canonical_path);
-      rust_assert (ok);
-
-      path = *canonical_path;
+      path
+       = mappings
+           .lookup_canonical_path (struct_decl.get_mappings ().get_nodeid ())
+           .value ();
     }
 
   RustIdent ident{path, struct_decl.get_locus ()};
@@ -347,11 +343,8 @@ TypeCheckItem::visit (HIR::Enum &enum_decl)
     }
 
   // get the path
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok
-    = mappings.lookup_canonical_path (enum_decl.get_mappings ().get_nodeid (),
-                                     &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (enum_decl.get_mappings ().get_nodeid ());
   RustIdent ident{*canonical_path, enum_decl.get_locus ()};
 
   // multi variant ADT
@@ -397,11 +390,8 @@ TypeCheckItem::visit (HIR::Union &union_decl)
     }
 
   // get the path
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok
-    = mappings.lookup_canonical_path (union_decl.get_mappings ().get_nodeid (),
-                                     &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (union_decl.get_mappings ().get_nodeid 
());
   RustIdent ident{*canonical_path, union_decl.get_locus ()};
 
   // there is only a single variant
@@ -567,12 +557,9 @@ TypeCheckItem::visit (HIR::Function &function)
     }
   else
     {
-      const CanonicalPath *canonical_path = nullptr;
-      bool ok = mappings.lookup_canonical_path (
-       function.get_mappings ().get_nodeid (), &canonical_path);
-      rust_assert (ok);
-
-      path = *canonical_path;
+      path = mappings
+              .lookup_canonical_path (function.get_mappings ().get_nodeid ())
+              .value ();
     }
 
   RustIdent ident{path, function.get_locus ()};
diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc 
b/gcc/rust/typecheck/rust-hir-type-check.cc
index 9ee2ff25fa3..45cb75d87fe 100644
--- a/gcc/rust/typecheck/rust-hir-type-check.cc
+++ b/gcc/rust/typecheck/rust-hir-type-check.cc
@@ -299,10 +299,8 @@ TraitItemReference::get_type_from_fn (/*const*/ 
HIR::TraitItemFunc &fn) const
     }
 
   auto &mappings = Analysis::Mappings::get ();
-  const CanonicalPath *canonical_path = nullptr;
-  bool ok = mappings.lookup_canonical_path (fn.get_mappings ().get_nodeid (),
-                                           &canonical_path);
-  rust_assert (ok);
+  auto canonical_path
+    = mappings.lookup_canonical_path (fn.get_mappings ().get_nodeid ());
 
   RustIdent ident{*canonical_path, fn.get_locus ()};
   auto resolved = new TyTy::FnType (
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index c556d766b35..eafa2ae15cc 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -229,8 +229,7 @@ public:
 
   void insert_canonical_path (NodeId id, const Resolver::CanonicalPath path)
   {
-    const Resolver::CanonicalPath *p = nullptr;
-    if (lookup_canonical_path (id, &p))
+    if (auto p = lookup_canonical_path (id))
       {
        // if we have already stored a canonical path this is ok so long as
        // this new path is equal or is smaller that the existing one but in
@@ -247,14 +246,14 @@ public:
     paths.emplace (id, std::move (path));
   }
 
-  bool lookup_canonical_path (NodeId id, const Resolver::CanonicalPath **path)
+  tl::optional<const Resolver::CanonicalPath &>
+  lookup_canonical_path (NodeId id)
   {
     auto it = paths.find (id);
     if (it == paths.end ())
-      return false;
+      return tl::nullopt;
 
-    *path = &it->second;
-    return true;
+    return it->second;
   }
 
   void insert_lang_item (LangItem::Kind item_type, DefId id)
-- 
2.45.2

Reply via email to