Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use name resolver 2.0 for module descendance checks #3224

Merged
merged 1 commit into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "rust-hir-stmt.h"
#include "rust-hir-item.h"
#include "rust-attribute-values.h"
#include "rust-immutable-name-resolution-context.h"

namespace Rust {
namespace Privacy {
Expand Down Expand Up @@ -93,6 +94,14 @@ static bool
is_child_module (Analysis::Mappings &mappings, NodeId parent,
NodeId possible_child)
{
if (flag_name_resolution_2_0)
{
auto &nr_ctx
= Resolver2_0::ImmutableNameResolutionContext::get ().resolver ();

return nr_ctx.values.is_module_descendant (parent, possible_child);
}

auto children = mappings.lookup_module_children (parent);

if (!children)
Expand Down
10 changes: 10 additions & 0 deletions gcc/rust/resolve/rust-forever-stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,12 @@ template <Namespace N> class ForeverStack

std::string as_debug_string ();

/**
* Used to check if a module is a descendant of another module
* Intended for use in the privacy checker
*/
bool is_module_descendant (NodeId parent, NodeId child) const;

private:
/**
* A link between two Nodes in our trie data structure. This class represents
Expand Down Expand Up @@ -635,6 +641,10 @@ template <Namespace N> class ForeverStack
tl::optional<Rib &> dfs_rib (Node &starting_point, NodeId to_find);
tl::optional<const Rib &> dfs_rib (const Node &starting_point,
NodeId to_find) const;
// FIXME: Documentation
tl::optional<Node &> dfs_node (Node &starting_point, NodeId to_find);
tl::optional<const Node &> dfs_node (const Node &starting_point,
NodeId to_find) const;
};

} // namespace Resolver2_0
Expand Down
41 changes: 34 additions & 7 deletions gcc/rust/resolve/rust-forever-stack.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -625,13 +625,33 @@ ForeverStack<N>::to_canonical_path (NodeId id) const
template <Namespace N>
tl::optional<Rib &>
ForeverStack<N>::dfs_rib (ForeverStack<N>::Node &starting_point, NodeId to_find)
{
return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & {
return x.rib;
});
}

template <Namespace N>
tl::optional<const Rib &>
ForeverStack<N>::dfs_rib (const ForeverStack<N>::Node &starting_point,
NodeId to_find) const
{
return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & {
return x.rib;
});
}

template <Namespace N>
tl::optional<typename ForeverStack<N>::Node &>
ForeverStack<N>::dfs_node (ForeverStack<N>::Node &starting_point,
NodeId to_find)
{
if (starting_point.id == to_find)
return starting_point.rib;
return starting_point;

for (auto &child : starting_point.children)
{
auto candidate = dfs_rib (child.second, to_find);
auto candidate = dfs_node (child.second, to_find);

if (candidate.has_value ())
return candidate;
Expand All @@ -641,16 +661,16 @@ ForeverStack<N>::dfs_rib (ForeverStack<N>::Node &starting_point, NodeId to_find)
}

template <Namespace N>
tl::optional<const Rib &>
ForeverStack<N>::dfs_rib (const ForeverStack<N>::Node &starting_point,
NodeId to_find) const
tl::optional<const typename ForeverStack<N>::Node &>
ForeverStack<N>::dfs_node (const ForeverStack<N>::Node &starting_point,
NodeId to_find) const
{
if (starting_point.id == to_find)
return starting_point.rib;
return starting_point;

for (auto &child : starting_point.children)
{
auto candidate = dfs_rib (child.second, to_find);
auto candidate = dfs_node (child.second, to_find);

if (candidate.has_value ())
return candidate;
Expand Down Expand Up @@ -737,6 +757,13 @@ ForeverStack<N>::as_debug_string ()
return stream.str ();
}

template <Namespace N>
bool
ForeverStack<N>::is_module_descendant (NodeId parent, NodeId child) const
{
return dfs_node (dfs_node (root, parent).value (), child).has_value ();
}

// FIXME: Can we add selftests?

} // namespace Resolver2_0
Expand Down
Loading