Skip to content

Commit

Permalink
Fix privacy for items inside modules. Fix several tests.
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* checks/errors/privacy/rust-privacy-reporter.cc
	(PrivacyReporter::check_for_privacy_violation):
	Update privacy check function to check for item's privacy inside of modules.

gcc/testsuite/ChangeLog:

	* rust/compile/issue-850.rs: Fix private enum.
	* rust/compile/iterators1.rs: Fix private enum.
	* rust/execute/torture/extern_mod4/modules/mod.rs: Fix private function.
	* rust/execute/torture/iter1.rs: Fix private enum.
	* rust/compile/issue-2636.rs: New test.

Signed-off-by: dave <[email protected]>
  • Loading branch information
dme2 committed Oct 9, 2023
1 parent 007166c commit 7e74245
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
28 changes: 18 additions & 10 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,20 +138,28 @@ PrivacyReporter::check_for_privacy_violation (const NodeId &use_id,

switch (vis.get_kind ())
{
case ModuleVisibility::Public:
break;
case ModuleVisibility::Public: {
rust_debug("PUBLIC");
break;
}
case ModuleVisibility::Restricted: {
// If we are in the crate, everything is restricted correctly, but we
// can't get a module for it
if (!current_module.has_value ())
return;
rust_debug("RESTRICTED");
// If we are in the crate, everything is restricted correctly, but we
// can't get a module for it

auto module = mappings.lookup_defid(vis.get_module_id());
// rust_assert (module != nullptr);
if (!current_module.has_value() && module == nullptr)
break;

auto module = mappings.lookup_defid (vis.get_module_id ());
rust_assert (module != nullptr);
auto mod_node_id = module->get_mappings().get_nodeid();

auto mod_node_id = module->get_mappings ().get_nodeid ();
if ((!current_module.has_value()) and mod_node_id) {
valid = false;
break;
}

// We are in the module referenced by the pub(restricted) visibility.
// We are in the module referenced by the pub(restricted) visibility.
// This is valid
if (mod_node_id == current_module.value ())
break;
Expand Down
11 changes: 11 additions & 0 deletions gcc/testsuite/rust/compile/issue-2636.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
pub mod module_a {
const CONST_I32: i32 = 0;
static BAR:i32 = CONST_I32;
}

fn main() {
let a = module_a::CONST_I32;
// { dg-error "definition is private in this context" "" { target *-*-* } .-1 }
let b = module_a::BAR;
// { dg-error "definition is private in this context" "" { target *-*-* } .-1 }
}
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/compile/issue-850.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ extern "C" {
}

mod option {
enum Option<T> {
pub enum Option<T> {
#[lang = "None"]
None,
#[lang = "Some"]
Expand Down
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/compile/iterators1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ mod option {
}

mod result {
enum Result<T, E> {
pub enum Result<T, E> {
Ok(T),
Err(E),
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
fn return_12() -> i32 {
pub fn return_12() -> i32 {
12
}
2 changes: 1 addition & 1 deletion gcc/testsuite/rust/execute/torture/iter1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod option {
}

mod result {
enum Result<T, E> {
pub enum Result<T, E> {
Ok(T),
Err(E),
}
Expand Down

0 comments on commit 7e74245

Please sign in to comment.