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

Change their return type to a const reference in order to avoid copies
when possible. Also wrap this new return type into an optional.

gcc/rust/ChangeLog:

        * checks/errors/borrowck/rust-borrow-checker.cc (BorrowChecker::go):
        Adapt the code to the new return types.
        * resolve/rust-ast-resolve.cc (NameResolution::go): Likewise.
        * util/rust-hir-map.cc (Mappings::get_crate_name): Change return type
        to const string reference optional.
        (Mappings::get_current_crate_name): Likewise.
        * util/rust-hir-map.h: Update function prototypes.

Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
---
 .../errors/borrowck/rust-borrow-checker.cc       |  6 ++----
 gcc/rust/resolve/rust-ast-resolve.cc             |  4 +---
 gcc/rust/util/rust-hir-map.cc                    | 16 ++++++----------
 gcc/rust/util/rust-hir-map.h                     |  4 ++--
 4 files changed, 11 insertions(+), 19 deletions(-)

diff --git a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc 
b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
index da4738c05b9..6eec021abca 100644
--- a/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
+++ b/gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
@@ -65,10 +65,8 @@ BorrowChecker::go (HIR::Crate &crate)
     {
       mkdir_wrapped ("bir_dump");
       auto &mappings = Analysis::Mappings::get ();
-      bool ok = mappings.get_crate_name (crate.get_mappings ().get_crate_num 
(),
-                                        crate_name);
-      rust_assert (ok);
-
+      crate_name
+       = *mappings.get_crate_name (crate.get_mappings ().get_crate_num ());
       mkdir_wrapped ("nll_facts_gccrs");
     }
 
diff --git a/gcc/rust/resolve/rust-ast-resolve.cc 
b/gcc/rust/resolve/rust-ast-resolve.cc
index bb8bcc4c239..a467d1e38b4 100644
--- a/gcc/rust/resolve/rust-ast-resolve.cc
+++ b/gcc/rust/resolve/rust-ast-resolve.cc
@@ -63,9 +63,7 @@ NameResolution::go (AST::Crate &crate)
 {
   // lookup current crate name
   CrateNum cnum = mappings.get_current_crate ();
-  std::string crate_name;
-  bool ok = mappings.get_crate_name (cnum, crate_name);
-  rust_assert (ok);
+  const auto &crate_name = mappings.get_crate_name (cnum).value ();
 
   // setup the ribs
   NodeId scope_node_id = crate.get_node_id ();
diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc
index 76642a1b132..cb4e95ae8f0 100644
--- a/gcc/rust/util/rust-hir-map.cc
+++ b/gcc/rust/util/rust-hir-map.cc
@@ -138,15 +138,14 @@ Mappings::get_current_crate () const
   return currentCrateNum;
 }
 
-bool
-Mappings::get_crate_name (CrateNum crate_num, std::string &name) const
+tl::optional<const std::string &>
+Mappings::get_crate_name (CrateNum crate_num) const
 {
   auto it = crate_names.find (crate_num);
   if (it == crate_names.end ())
-    return false;
+    return tl::nullopt;
 
-  name.assign (it->second);
-  return true;
+  return it->second;
 }
 
 void
@@ -155,13 +154,10 @@ Mappings::set_crate_name (CrateNum crate_num, const 
std::string &name)
   crate_names[crate_num] = name;
 }
 
-std::string
+const std::string &
 Mappings::get_current_crate_name () const
 {
-  std::string name;
-  bool ok = get_crate_name (get_current_crate (), name);
-  rust_assert (ok);
-  return name;
+  return get_crate_name (get_current_crate ()).value ();
 }
 
 tl::optional<CrateNum>
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 8181abe00a9..4ac719ffea7 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -80,9 +80,9 @@ public:
   CrateNum get_next_crate_num (const std::string &name);
   void set_current_crate (CrateNum crateNum);
   CrateNum get_current_crate () const;
-  bool get_crate_name (CrateNum crate_num, std::string &name) const;
+  tl::optional<const std::string &> get_crate_name (CrateNum crate_num) const;
   void set_crate_name (CrateNum crate_num, const std::string &name);
-  std::string get_current_crate_name () const;
+  const std::string &get_current_crate_name () const;
   tl::optional<CrateNum>
   lookup_crate_name (const std::string &crate_name) const;
   tl::optional<NodeId> crate_num_to_nodeid (const CrateNum &crate_num) const;
-- 
2.45.2

Reply via email to