public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Thomas Schwinge <tschwinge@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc/devel/rust/master] privacy: Handle calls to functions defined in previous ancestors
Date: Wed,  8 Jun 2022 12:47:42 +0000 (GMT)	[thread overview]
Message-ID: <20220608124742.420973BF90F1@sourceware.org> (raw)

https://gcc.gnu.org/g:7ebe376bd68eb0d9aae9cef6308f3ec15407862d

commit 7ebe376bd68eb0d9aae9cef6308f3ec15407862d
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Tue May 17 14:01:19 2022 +0200

    privacy: Handle calls to functions defined in previous ancestors
    
    Previously, we would only check if `current_module` was a direct
    descendant of the item's module. However, we also need to visit each of this
    item's module's children recursively.

Diff:
---
 gcc/rust/privacy/rust-privacy-reporter.cc | 19 ++++++++++++-------
 gcc/testsuite/rust/compile/privacy2.rs    | 13 +++++++++++++
 2 files changed, 25 insertions(+), 7 deletions(-)

diff --git a/gcc/rust/privacy/rust-privacy-reporter.cc b/gcc/rust/privacy/rust-privacy-reporter.cc
index 4c18adb8615..a974de039c7 100644
--- a/gcc/rust/privacy/rust-privacy-reporter.cc
+++ b/gcc/rust/privacy/rust-privacy-reporter.cc
@@ -20,15 +20,22 @@ PrivacyReporter::go (HIR::Crate &crate)
 }
 
 static bool
-is_child_module (NodeId current_module,
-		 Optional<std::vector<NodeId> &> children)
+is_child_module (Analysis::Mappings &mappings, NodeId parent,
+		 NodeId possible_child)
 {
+  auto children = mappings.lookup_module_children (parent);
+
   if (!children)
     return false;
 
-  // FIXME: This checks for one step - we need to go deeper
+  // Visit all toplevel children
   for (auto &child : *children)
-    if (child == current_module)
+    if (child == possible_child)
+      return true;
+
+  // Now descend recursively in the child module tree
+  for (auto &child : *children)
+    if (is_child_module (mappings, child, possible_child))
       return true;
 
   return false;
@@ -72,11 +79,9 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,
 	if (mod_node_id == current_module.get ())
 	  break;
 
-	auto children = mappings.lookup_module_children (mod_node_id);
-
 	// FIXME: This needs a LOT of TLC: hinting about the definition, a
 	// string to say if it's a module, function, type, etc...
-	if (!is_child_module (current_module.get (), children))
+	if (!is_child_module (mappings, mod_node_id, current_module.get ()))
 	  valid = false;
       }
       break;
diff --git a/gcc/testsuite/rust/compile/privacy2.rs b/gcc/testsuite/rust/compile/privacy2.rs
new file mode 100644
index 00000000000..3c0744928b1
--- /dev/null
+++ b/gcc/testsuite/rust/compile/privacy2.rs
@@ -0,0 +1,13 @@
+// { dg-additional-options "-w" }
+
+mod orange {
+    fn tangerine() {}
+
+    mod green {
+        mod blue {
+            fn berry() {
+                tangerine();
+            }
+        }
+    }
+}


                 reply	other threads:[~2022-06-08 12:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220608124742.420973BF90F1@sourceware.org \
    --to=tschwinge@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).