Skip to content

Commit

Permalink
Change crate name retrieval function return types
Browse files Browse the repository at this point in the history
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.patry@embecosm.com>
  • Loading branch information
P-E-P committed May 1, 2024
1 parent c8af160 commit f5e097e
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
5 changes: 2 additions & 3 deletions gcc/rust/checks/errors/borrowck/rust-borrow-checker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,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 ());
}

FunctionCollector collector;
Expand Down
4 changes: 1 addition & 3 deletions gcc/rust/resolve/rust-ast-resolve.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
Expand Down
16 changes: 6 additions & 10 deletions gcc/rust/util/rust-hir-map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/util/rust-hir-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ class Mappings
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;
Expand Down

0 comments on commit f5e097e

Please sign in to comment.