[gcc r14-7405] gccrs: privacy: Check for pub(crate) when resolving visibility path.
Arthur Cohen
cohenarthur@gcc.gnu.org
Tue Jan 16 17:31:54 GMT 2024
https://gcc.gnu.org/g:d1e329af3117ba191c45d15aff783fb712d5092d
commit r14-7405-gd1e329af3117ba191c45d15aff783fb712d5092d
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date: Tue Mar 28 14:22:21 2023 +0200
gccrs: privacy: Check for pub(crate) when resolving visibility path.
This causes the function to return true and the checks to pass, but it
requires more thinking - how do we deal with pub(crate) in the current system?
Should we simply treat it as a pub item in the current crate, but export it as
a private item in the metadata?
gcc/rust/ChangeLog:
* ast/rust-item.h: Fix `Visibility::has_path()` implementation.
* checks/errors/privacy/rust-visibility-resolver.cc
(VisibilityResolver::resolve_module_path): Check if we are dealing with pub(crate) properly.
gcc/testsuite/ChangeLog:
* rust/compile/privacy8.rs: New test.
Diff:
---
gcc/rust/ast/rust-item.h | 2 +-
gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc | 10 ++++++++++
gcc/testsuite/rust/compile/privacy8.rs | 1 +
3 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 883b010ccb6..7f177e54dbd 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -642,7 +642,7 @@ public:
}
// Returns whether a visibility has a path
- bool has_path () const { return !(is_error ()) && vis_type == PUB_IN_PATH; }
+ bool has_path () const { return !is_error () && vis_type >= PUB_CRATE; }
// Returns whether visibility is public or not.
bool is_public () const { return vis_type != PRIV && !is_error (); }
diff --git a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
index e970375d2db..49524222c86 100644
--- a/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
+++ b/gcc/rust/checks/errors/privacy/rust-visibility-resolver.cc
@@ -74,6 +74,16 @@ VisibilityResolver::resolve_module_path (const HIR::SimplePath &restriction,
HirId ref;
rust_assert (mappings.lookup_node_to_hir (ref_node_id, &ref));
+ auto crate = mappings.get_ast_crate (mappings.get_current_crate ());
+
+ // we may be dealing with pub(crate)
+ if (ref_node_id == crate.get_node_id ())
+ // FIXME: What do we do here? There isn't a DefId for the Crate, so can we
+ // actually do anything?
+ // We basically want to return true always but just when exporting export
+ // these items as private?
+ return true;
+
auto module = mappings.lookup_module (ref);
if (!module)
{
diff --git a/gcc/testsuite/rust/compile/privacy8.rs b/gcc/testsuite/rust/compile/privacy8.rs
new file mode 100644
index 00000000000..6cf8f5b2f6e
--- /dev/null
+++ b/gcc/testsuite/rust/compile/privacy8.rs
@@ -0,0 +1 @@
+pub(crate) struct Foo; // { dg-warning "struct is never constructed" }
More information about the Gcc-cvs
mailing list