From 197f0770ab30df28ef7e0c11e8e78e382d51d3e8 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Sat, 26 Oct 2024 20:52:31 -0400 Subject: [PATCH] Use name resolver 2.0 for module descendance checks gcc/rust/ChangeLog: * checks/errors/privacy/rust-privacy-reporter.cc: Include rust-immutable-name-resolution-context.h. (is_child_module): Use ForeverStack::is_module_descendant if name resolution 2.0 is enabled. * resolve/rust-forever-stack.h (ForeverStack::is_module_descendant): Add. (ForeverStack::dfs_node): Add. * resolve/rust-forever-stack.hxx (ForeverStack::dfs_rib): Use ForeverStack::dfs_node. (ForeverStack::dfs_node): Add. (ForeverStack::is_module_descendant): Add. Signed-off-by: Owen Avery --- .../errors/privacy/rust-privacy-reporter.cc | 9 ++++ gcc/rust/resolve/rust-forever-stack.h | 10 +++++ gcc/rust/resolve/rust-forever-stack.hxx | 41 +++++++++++++++---- 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 9e4b0073db4..b7abed25629 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -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 { @@ -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) diff --git a/gcc/rust/resolve/rust-forever-stack.h b/gcc/rust/resolve/rust-forever-stack.h index b084807dd32..b6062d4e165 100644 --- a/gcc/rust/resolve/rust-forever-stack.h +++ b/gcc/rust/resolve/rust-forever-stack.h @@ -521,6 +521,12 @@ template 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 @@ -635,6 +641,10 @@ template class ForeverStack tl::optional dfs_rib (Node &starting_point, NodeId to_find); tl::optional dfs_rib (const Node &starting_point, NodeId to_find) const; + // FIXME: Documentation + tl::optional dfs_node (Node &starting_point, NodeId to_find); + tl::optional dfs_node (const Node &starting_point, + NodeId to_find) const; }; } // namespace Resolver2_0 diff --git a/gcc/rust/resolve/rust-forever-stack.hxx b/gcc/rust/resolve/rust-forever-stack.hxx index 42a63f6fd70..45f89d58e48 100644 --- a/gcc/rust/resolve/rust-forever-stack.hxx +++ b/gcc/rust/resolve/rust-forever-stack.hxx @@ -625,13 +625,33 @@ ForeverStack::to_canonical_path (NodeId id) const template tl::optional ForeverStack::dfs_rib (ForeverStack::Node &starting_point, NodeId to_find) +{ + return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & { + return x.rib; + }); +} + +template +tl::optional +ForeverStack::dfs_rib (const ForeverStack::Node &starting_point, + NodeId to_find) const +{ + return dfs_node (starting_point, to_find).map ([] (Node &x) -> Rib & { + return x.rib; + }); +} + +template +tl::optional::Node &> +ForeverStack::dfs_node (ForeverStack::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; @@ -641,16 +661,16 @@ ForeverStack::dfs_rib (ForeverStack::Node &starting_point, NodeId to_find) } template -tl::optional -ForeverStack::dfs_rib (const ForeverStack::Node &starting_point, - NodeId to_find) const +tl::optional::Node &> +ForeverStack::dfs_node (const ForeverStack::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; @@ -737,6 +757,13 @@ ForeverStack::as_debug_string () return stream.str (); } +template +bool +ForeverStack::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