https://gcc.gnu.org/g:b1123a29c293fac03da86e2b89f1a6b7a02f6976
commit r17-1882-gb1123a29c293fac03da86e2b89f1a6b7a02f6976 Author: Arthur Cohen <[email protected]> Date: Wed Jun 3 11:28:38 2026 +0200 gccrs: privacy-reporter: Check for resolved nodes in more namespaces And improve code factoring to reduce the complexity of the check_for_privacy_violation function. gcc/rust/ChangeLog: * checks/errors/privacy/rust-privacy-reporter.cc (PrivacyReporter::check_for_privacy_violation): Add new overload for two namespaces. (PrivacyReporter::visit): Call it. (PrivacyReporter::check_violation_inner): New function. * checks/errors/privacy/rust-privacy-reporter.h: Declare it. Diff: --- .../checks/errors/privacy/rust-privacy-reporter.cc | 57 ++++++++++++++++------ .../checks/errors/privacy/rust-privacy-reporter.h | 7 +++ 2 files changed, 48 insertions(+), 16 deletions(-) diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc index 5d3c50ae36c7..9e2238f79dd0 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc @@ -17,6 +17,7 @@ // <http://www.gnu.org/licenses/>. #include "rust-privacy-reporter.h" +#include "rust-hir-map.h" #include "rust-rib.h" #include "rust-session-manager.h" #include "rust-hir-expr.h" @@ -92,21 +93,10 @@ PrivacyReporter::go (HIR::Crate &crate) } } -// FIXME: This function needs a lot of refactoring void -PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, - const location_t locus, - Resolver2_0::Namespace ns) +PrivacyReporter::check_violation_inner (NodeId ref_node_id, + const location_t locus) { - NodeId ref_node_id; - - // FIXME: Assert here. For now, we return since this causes issues when - // checking inferred types (#1260) - if (auto id = resolver.lookup (use_id, ns)) - ref_node_id = *id; - else - return; - auto vis = mappings.lookup_visibility (ref_node_id); // FIXME: Can we really return here if the item has no visibility? @@ -157,6 +147,41 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, } } +void +PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, + const location_t locus, + Resolver2_0::Namespace ns) +{ + NodeId ref_node_id; + + // FIXME: Assert here. For now, we return since this causes issues when + // checking inferred types (#1260) + if (auto id = resolver.lookup (use_id, ns)) + ref_node_id = *id; + else + return; + + check_violation_inner (ref_node_id, locus); +} + +void +PrivacyReporter::check_for_privacy_violation (const NodeId &use_id, + const location_t locus, + Resolver2_0::Namespace ns1, + Resolver2_0::Namespace ns2) +{ + NodeId ref_node_id; + + // FIXME: Assert here. For now, we return since this causes issues when + // checking inferred types (#1260) + if (auto resolved = resolver.lookup (use_id, ns1, ns2)) + ref_node_id = resolved->id; + else + return; + + check_violation_inner (ref_node_id, locus); +} + void PrivacyReporter::check_base_type_privacy (Analysis::NodeMapping &node_mappings, const TyTy::BaseType *ty, @@ -298,9 +323,9 @@ PrivacyReporter::visit (HIR::TypePath &path) void PrivacyReporter::visit (HIR::QualifiedPathInExpression &path) { - check_for_privacy_violation ( - path.get_mappings ().get_nodeid (), path.get_locus (), - Resolver2_0::Namespace::Types /* FIXME: Is that correct? */); + check_for_privacy_violation (path.get_mappings ().get_nodeid (), + path.get_locus (), + Resolver2_0::Namespace::Values); } void diff --git a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h index c735aa44a158..07627b1d6a62 100644 --- a/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h +++ b/gcc/rust/checks/errors/privacy/rust-privacy-reporter.h @@ -57,10 +57,17 @@ private: * @param use_id NodeId of the expression/statement referencing an item with * a visibility * @param locus Location of said expression/statement + * @param ns1, ns2 Namespaces in which to check for visibility */ void check_for_privacy_violation (const NodeId &use_id, const location_t locus, Resolver2_0::Namespace ns); + void check_for_privacy_violation (const NodeId &use_id, + const location_t locus, + Resolver2_0::Namespace ns1, + Resolver2_0::Namespace ns2); + + void check_violation_inner (NodeId ref_node_id, const location_t inner); /** * Internal function used by `check_type_privacy` when dealing with complex
