Skip to content

Commit

Permalink
Change lookup_visibility's return type
Browse files Browse the repository at this point in the history
Wrap the return type within an optional.

gcc/rust/ChangeLog:

	* checks/errors/privacy/rust-privacy-reporter.cc: Change call site
	to accomodate new return type.
	* checks/errors/privacy/rust-pub-restricted-visitor.cc: Likewise.
	* util/rust-hir-map.cc (Mappings::lookup_visibility): Change the
	function's return type.
	* util/rust-hir-map.h: Update the function's prototype.

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed May 17, 2024
1 parent 435f227 commit f13cb85
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
8 changes: 4 additions & 4 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
if (ref_node_id == UNKNOWN_NODEID)
return;

ModuleVisibility vis;
auto vis = mappings.lookup_visibility (ref_node_id);

// FIXME: Can we really return here if the item has no visibility?
if (!mappings.lookup_visibility (ref_node_id, vis))
if (!vis)
return;

auto valid = true;

switch (vis.get_kind ())
switch (vis->get_kind ())
{
case ModuleVisibility::Public:
break;
Expand All @@ -146,7 +146,7 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
if (!current_module.has_value ())
return;

auto module = mappings.lookup_defid (vis.get_module_id ()).value ();
auto module = mappings.lookup_defid (vis->get_module_id ()).value ();

auto mod_node_id = module->get_mappings ().get_nodeid ();

Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/checks/errors/privacy/rust-pub-restricted-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,16 @@ bool
PubRestrictedVisitor::is_restriction_valid (NodeId item_id,
const location_t locus)
{
ModuleVisibility visibility;
auto visibility = mappings.lookup_visibility (item_id);

// If there is no visibility in the mappings, then the item is private and
// does not contain any restriction
// FIXME: Is that correct?
if (!mappings.lookup_visibility (item_id, visibility))
if (!visibility)
return true;

for (auto mod = module_stack.rbegin (); mod != module_stack.rend (); mod++)
if (*mod == visibility.get_module_id ())
if (*mod == visibility->get_module_id ())
return true;

rust_error_at (locus, "restricted path is not an ancestor of the "
Expand Down
9 changes: 4 additions & 5 deletions gcc/rust/util/rust-hir-map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1102,15 +1102,14 @@ Mappings::insert_visibility (NodeId id, Privacy::ModuleVisibility visibility)
visibility_map.insert ({id, visibility});
}

bool
Mappings::lookup_visibility (NodeId id, Privacy::ModuleVisibility &def)
tl::optional<Privacy::ModuleVisibility &>
Mappings::lookup_visibility (NodeId id)
{
auto it = visibility_map.find (id);
if (it == visibility_map.end ())
return false;
return tl::nullopt;

def = it->second;
return true;
return it->second;
}

void
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/util/rust-hir-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ class Mappings
AttributeProcMacro def);

void insert_visibility (NodeId id, Privacy::ModuleVisibility visibility);
bool lookup_visibility (NodeId id, Privacy::ModuleVisibility &def);
tl::optional<Privacy::ModuleVisibility &> lookup_visibility (NodeId id);

void insert_ast_module (AST::Module *);
tl::optional<AST::Module *> lookup_ast_module (NodeId id);
Expand Down

0 comments on commit f13cb85

Please sign in to comment.