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 module visibility check #6685

Merged
merged 12 commits into from
Nov 15, 2024
2 changes: 1 addition & 1 deletion examples/enums/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ library;

mod basic_enum;
mod enum_of_structs;
mod enum_of_enums;
pub mod enum_of_enums;
mod enums_avoid;
mod enums_preferred;
41 changes: 29 additions & 12 deletions sway-core/src/semantic_analysis/namespace/root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -683,27 +683,44 @@ impl Root {
Ok(())
}

/// Check that all accessed modules in the src path are visible from the dst path.
/// If src and dst have a common ancestor module that is private, this privacy modifier is
/// ignored for visibility purposes, since src and dst are both behind that private visibility
/// modifier. Additionally, items in a private module are visible to its immediate parent.
fn check_module_privacy(
&self,
handler: &Handler,
engines: &Engines,
src: &ModulePath,
dst: &ModulePath,
) -> Result<(), ErrorEmitted> {
// you are always allowed to access your ancestor's symbols
if !is_ancestor(src, dst) {
// we don't check the first prefix because direct children are always accessible
for prefix in iter_prefixes(src).skip(1) {
let module = self.module.lookup_submodule(handler, engines, prefix)?;
if module.visibility().is_private() {
let prefix_last = prefix[prefix.len() - 1].clone();
handler.emit_err(CompileError::ImportPrivateModule {
span: prefix_last.span(),
name: prefix_last,
});
}
// Calculate the number of src prefixes whose privacy is ignored.
let mut ignored_prefixes = 0;

// Ignore visibility of common ancestors
ignored_prefixes += src
.iter()
.zip(dst)
.position(|(src_id, dst_id)| src_id != dst_id)
.unwrap_or(dst.len());

// Ignore visibility of direct submodules of the destination module
if dst.len() == ignored_prefixes {
ignored_prefixes += 1;
}

// Check visibility of remaining submodules in the source path
for prefix in iter_prefixes(src).skip(ignored_prefixes) {
let module = self.module.lookup_submodule(handler, engines, prefix)?;
if module.visibility().is_private() {
let prefix_last = prefix[prefix.len() - 1].clone();
handler.emit_err(CompileError::ImportPrivateModule {
span: prefix_last.span(),
name: prefix_last,
});
}
}

Ok(())
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
script;

mod items_1;
mod lib_1; // Aliased item reexports of items_1
mod items_2;
mod lib_2; // Aliased item reexports of items_2
mod items_3;
mod lib_3; // Aliased item reexports of items_3
mod items_4;
mod lib_4_1; // Aliased item reexports of items_4
mod lib_4_2; // Aliased item reexports of lib_4_1
mod items_5;
mod lib_5_1; // Aliased trait reexports from items_5
mod lib_5_2; // Aliased trait reexports from items_5
pub mod items_1;
pub mod lib_1; // Aliased item reexports of items_1
pub mod items_2;
pub mod lib_2; // Aliased item reexports of items_2
pub mod items_3;
pub mod lib_3; // Aliased item reexports of items_3
pub mod items_4;
pub mod lib_4_1; // Aliased item reexports of items_4
pub mod lib_4_2; // Aliased item reexports of lib_4_1
pub mod items_5;
pub mod lib_5_1; // Aliased trait reexports from items_5
pub mod lib_5_2; // Aliased trait reexports from items_5

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
script;

mod items_1;
mod lib_1_1; // Item reexports of items_1
mod lib_1_2; // Item reexports of items_1
pub mod items_1;
pub mod lib_1_1; // Item reexports of items_1
pub mod lib_1_2; // Item reexports of items_1

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
script;

mod items_1;
mod lib_1; // Item reexports of items_1
mod items_2_1;
mod items_2_2;
mod lib_2; // Item reexports of items_2_1 and items_2_2
mod items_3_1;
mod lib_3; // Item reexports of items_3_1 and items_3_2
mod items_4_1;
mod items_4_2;
mod items_4_3;
mod items_4_4;
mod lib_4; // Item reexports of items_4_1 and items_4_2
pub mod items_1;
pub mod lib_1; // Item reexports of items_1
pub mod items_2_1;
pub mod items_2_2;
pub mod lib_2; // Item reexports of items_2_1 and items_2_2
pub mod items_3_1;
pub mod lib_3; // Item reexports of items_3_1 and items_3_2
pub mod items_4_1;
pub mod items_4_2;
pub mod items_4_3;
pub mod items_4_4;
pub mod lib_4; // Item reexports of items_4_1 and items_4_2

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
script;

mod items_1;
mod lib_1; // Item reexports of items_1
mod items_2;
mod lib_2; // Item reexports of items_1
pub mod items_1;
pub mod lib_1; // Item reexports of items_1
pub mod items_2;
pub mod lib_2; // Item reexports of items_1

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
script;

mod items_1;
mod lib_1; // Item reexports of items_1
mod items_2;
mod lib_2; // Item reexports of items_1
pub mod items_1;
pub mod lib_1; // Item reexports of items_1
pub mod items_2;
pub mod lib_2; // Item reexports of items_2
pub mod items_3;
pub mod lib_3; // Item reexports of items_3

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

mod r#enum;
pub mod r#enum;
mod utils;

fn main() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

mod r#trait;
pub mod r#trait;
mod utils;

use r#trait::Trait;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ script;
// but until then, multiple methods with the same name is undefined behavior.
// https://doc.rust-lang.org/rust-by-example/trait/disambiguating.html

mod my_double;
pub mod my_double;
mod my_point;
mod my_triple;
pub mod my_triple;

use my_point::MyPoint;
use my_triple::MyTriple;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
script;

mod context;
mod asset;
pub mod asset;
mod utils;

use context::Context;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

mod data_structures;
pub mod data_structures;
mod eq_impls;

use eq_impls::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
script;

mod lib;
pub mod lib;
mod top_level;
mod in_structs;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
library;

mod a;
mod b;
pub mod a;
pub mod b;

fn main() -> u32 {
1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
library;

mod c;
mod a;
mod b;
pub mod a;
pub mod b;

fn main() -> u32 {
1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
script;

mod items_1;
mod lib_1; // Aliased item reexports of items_1
mod items_2;
mod lib_2; // Aliased item reexports of items_2
mod items_3;
mod lib_3_1; // Aliased item reexports of items_3
mod lib_3_2; // Aliased item reexports of lib_3_1
mod items_4;
mod lib_4_1; // Aliased item reexports of items_4
mod lib_4_2; // Aliased item reexports of items_4 with different aliases
pub mod items_1;
pub mod lib_1; // Aliased item reexports of items_1
pub mod items_2;
pub mod lib_2; // Aliased item reexports of items_2
pub mod items_3;
pub mod lib_3_1; // Aliased item reexports of items_3
pub mod lib_3_2; // Aliased item reexports of lib_3_1
pub mod items_4;
pub mod lib_4_1; // Aliased item reexports of items_4
pub mod lib_4_2; // Aliased item reexports of items_4 with different aliases

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
script;

mod items_1;
mod lib_1_1; // Item reexports of items_1
mod lib_1_2; // Item reexports of items_1
mod items_2;
mod lib_2_1; // Star reexports of items_2
mod lib_2_2; // Star reexports of items_2
mod items_3;
mod lib_3_1; // Star reexports of items_3
mod lib_3_2; // Item reexports of items_3
mod items_4;
mod lib_4_1; // Item reexports of items_4
mod lib_4_2; // Star reexports of items_4
pub mod items_1;
pub mod lib_1_1; // Item reexports of items_1
pub mod lib_1_2; // Item reexports of items_1
pub mod items_2;
pub mod lib_2_1; // Star reexports of items_2
pub mod lib_2_2; // Star reexports of items_2
pub mod items_3;
pub mod lib_3_1; // Star reexports of items_3
pub mod lib_3_2; // Item reexports of items_3
pub mod items_4;
pub mod lib_4_1; // Item reexports of items_4
pub mod lib_4_2; // Star reexports of items_4

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
script;

mod items_1;
mod lib_1_1; // Reexports of items_1
mod lib_1_2; // Reexports of lib_1_1
mod lib_2; // Reexports of std::hash::Hasher, which is not part of the std prelude
mod lib_3_1; // Reexports of std::hash::Hash, which is not part of the std prelude
mod lib_3_2; // Reexports of std::hash::Hash, which is not part of the std prelude
mod lib_4; // Reexport of std::registers::global_gas
mod lib_5; // Reexport of core::codec::*
//mod lib_6_1; // Reexports of std::address::Address from the std prelude
mod lib_6_2; // Reexports of std::address::Address directly from std::address
pub mod items_1;
pub mod lib_1_1; // Reexports of items_1
pub mod lib_1_2; // Reexports of lib_1_1
pub mod lib_2; // Reexports of std::hash::Hasher, which is not part of the std prelude
pub mod lib_3_1; // Reexports of std::hash::Hash, which is not part of the std prelude
pub mod lib_3_2; // Reexports of std::hash::Hash, which is not part of the std prelude
pub mod lib_4; // Reexport of std::registers::global_gas
pub mod lib_5; // Reexport of core::codec::*
//pub mod lib_6_1; // Reexports of std::address::Address from the std prelude
pub mod lib_6_2; // Reexports of std::address::Address directly from std::address

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
script;

mod items_1;
mod lib_1; // Item reexports of items_1
mod items_2_1;
mod items_2_2;
mod lib_2; // Item reexports of items_2_1 and items_2_2
mod items_3_1;
mod lib_3; // Item reexports of items_3_1 and items_3_2
mod items_4_1;
mod items_4_2;
mod items_4_3;
mod items_4_4;
mod lib_4; // Item reexports of items_4_1 and items_4_2
pub mod items_1;
pub mod lib_1; // Item reexports of items_1
pub mod items_2_1;
pub mod items_2_2;
pub mod lib_2; // Item reexports of items_2_1 and items_2_2
pub mod items_3_1;
pub mod lib_3; // Item reexports of items_3_1 and items_3_2
pub mod items_4_1;
pub mod items_4_2;
pub mod items_4_3;
pub mod items_4_4;
pub mod lib_4; // Item reexports of items_4_1 and items_4_2

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
script;

mod items_1;
mod lib_1; // Item reexports of items_1
mod items_2;
mod lib_2; // Item reexports of items_1
pub mod items_1;
pub mod lib_1; // Item reexports of items_1
pub mod items_2;
pub mod lib_2; // Item reexports of items_1

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
script;

mod items_1;
mod lib_1; // Item reexports of items_1
mod items_2;
mod lib_2; // Item reexports of items_2
pub mod items_1;
pub mod lib_1; // Item reexports of items_1
pub mod items_2;
pub mod lib_2; // Item reexports of items_2

mod tests; // All tests

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
script;

mod items_1;
mod lib_1_1; // Item reexports of items_1
mod lib_1_2; // Item imports without reexport of items_1
mod items_2;
mod lib_2_1; // Item imports without reexport of items_1
mod lib_2_2; // Item reexports of items_1
pub mod items_1;
pub mod lib_1_1; // Item reexports of items_1
pub mod lib_1_2; // Item imports without reexport of items_1
pub mod items_2;
pub mod lib_2_1; // Item imports without reexport of items_1
pub mod lib_2_2; // Item reexports of items_1

mod tests; // All tests

Expand Down
Loading
Loading