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

commit ade76488cb1241bf104e5efbd3601bbf4de23989
Author: Pierre-Emmanuel Patry <pierre-emmanuel.pa...@embecosm.com>
Date:   Thu Apr 25 16:34:44 2024 +0200

    Change crate name retrieval function return types
    
    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>

Diff:
---
 gcc/rust/checks/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 d76d02cdc9b1..a3068fbfb282 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 65472f4e59fd..5dab77265b4d 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 d359df5cd599..24c8d618fdfe 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 015c3c245ef2..6bcda0a52bf3 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;

Reply via email to