Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix privacy access for items in different namespaces #2669

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions gcc/rust/checks/errors/privacy/rust-privacy-reporter.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright (C) 2020-2023 Free Software Foundation, Inc.

//
// This file is part of GCC.

// GCC is free software; you can redistribute it and/or modify it under
Expand Down 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
Loading