From 7eaa61e7f8e6928a11a440a91f61830c24e509ea Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Tue, 29 Oct 2024 20:22:22 +0100 Subject: [PATCH 1/9] Fix privacy check for module paths --- .../src/semantic_analysis/namespace/root.rs | 40 +++++++++++++------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 7d4e22b7265..644d3904ffd 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -691,19 +691,33 @@ impl Root { 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, - }); - } - } - } + // Ignore visibility of common ancestor modules + let mut ignored_prefixes = 0; + + for (src_prefix, dst_prefix) in src.iter().zip(dst) { + if src_prefix != dst_prefix { + break; + } + ignored_prefixes += 1; + } + + // 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.require_module(handler, &prefix.to_vec())?; + 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(()) } From 506a5bb0b34de52d53736095d7f955abb2343f25 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Tue, 29 Oct 2024 20:23:04 +0100 Subject: [PATCH 2/9] Fix privacy check for module paths --- sway-core/src/semantic_analysis/namespace/root.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 644d3904ffd..4a12e5d9813 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -690,10 +690,10 @@ impl Root { src: &ModulePath, dst: &ModulePath, ) -> Result<(), ErrorEmitted> { - // you are always allowed to access your ancestor's symbols // Ignore visibility of common ancestor modules let mut ignored_prefixes = 0; + // Ignore visibility of common ancestor modules for (src_prefix, dst_prefix) in src.iter().zip(dst) { if src_prefix != dst_prefix { break; From 67b48b1e25b558ad4f985a1f300117341192ecb7 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Nov 2024 14:23:37 +0100 Subject: [PATCH 3/9] Fix renamed method --- sway-core/src/semantic_analysis/namespace/root.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 4a12e5d9813..bc37afd3516 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -708,7 +708,7 @@ impl Root { // Check visibility of remaining submodules in the source path for prefix in iter_prefixes(src).skip(ignored_prefixes) { - let module = self.require_module(handler, &prefix.to_vec())?; + 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 { From 4095af47b88d4953ae6d75aa98d431aeb4ffe95f Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Nov 2024 14:25:09 +0100 Subject: [PATCH 4/9] Fixed tests --- .../language/reexport/aliases/src/main.sw | 24 +++++++++---------- .../src/main.sw | 6 ++--- .../src/main.sw | 24 +++++++++---------- .../reexport/simple_glob_import/src/main.sw | 8 +++---- .../reexport/simple_item_import/src/main.sw | 10 ++++---- .../should_pass/dca/unused_enum/src/main.sw | 2 +- .../should_pass/dca/unused_trait/src/main.sw | 2 +- .../language/generic_traits/src/main.sw | 4 ++-- .../import_method_from_other_file/src/main.sw | 2 +- .../src/main.sw | 2 +- .../match_expressions_constants/src/main.sw | 2 +- .../language/module_dep/src/lib.sw | 4 ++-- .../language/module_dep_multiple/src/lib.sw | 4 ++-- .../language/reexport/aliases/src/main.sw | 20 ++++++++-------- .../src/main.sw | 24 +++++++++---------- .../reexport/reexport_paths/src/main.sw | 20 ++++++++-------- .../src/main.sw | 24 +++++++++---------- .../reexport/simple_glob_import/src/main.sw | 8 +++---- .../reexport/simple_item_import/src/main.sw | 8 +++---- .../language/reexport/visibility/src/main.sw | 12 +++++----- .../language/use_absolute_path/src/main.sw | 2 +- .../should_pass/stdlib/option/src/main.sw | 2 +- .../should_pass/stdlib/result/src/main.sw | 2 +- .../test_contracts/return_struct/src/main.sw | 2 +- 24 files changed, 110 insertions(+), 108 deletions(-) diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/aliases/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/aliases/src/main.sw index d84157a1bce..a8597cf626a 100755 --- a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/aliases/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/aliases/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/multiple_imports_of_same_reexport/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/multiple_imports_of_same_reexport/src/main.sw index c4f06aaf84f..29853e98b33 100755 --- a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/multiple_imports_of_same_reexport/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/multiple_imports_of_same_reexport/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/shadowing_in_reexporting_module/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/shadowing_in_reexporting_module/src/main.sw index a2961173628..c0290b47faf 100755 --- a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/shadowing_in_reexporting_module/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/shadowing_in_reexporting_module/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_glob_import/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_glob_import/src/main.sw index 57262307323..fe1ea19f9c1 100755 --- a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_glob_import/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_glob_import/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_item_import/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_item_import/src/main.sw index 57262307323..8856faf59f6 100755 --- a/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_item_import/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_fail/language/reexport/simple_item_import/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_enum/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_enum/src/main.sw index ef39fb605a9..5fc24d795cf 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_enum/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_enum/src/main.sw @@ -1,6 +1,6 @@ script; -mod r#enum; +pub mod r#enum; mod utils; fn main() { diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_trait/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_trait/src/main.sw index 2607dbb987a..7a95e67d0a5 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_trait/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/dca/unused_trait/src/main.sw @@ -1,6 +1,6 @@ script; -mod r#trait; +pub mod r#trait; mod utils; use r#trait::Trait; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_traits/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_traits/src/main.sw index 3e5e72a76e3..f050a4cc0fc 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_traits/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/generic_traits/src/main.sw @@ -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; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/import_method_from_other_file/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/import_method_from_other_file/src/main.sw index 136515fc5c3..1f0fbad59be 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/import_method_from_other_file/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/import_method_from_other_file/src/main.sw @@ -1,7 +1,7 @@ script; mod context; -mod asset; +pub mod asset; mod utils; use context::Context; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/import_with_different_callpaths/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/import_with_different_callpaths/src/main.sw index 29e6fe78ddf..d672a617d81 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/import_with_different_callpaths/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/import_with_different_callpaths/src/main.sw @@ -1,6 +1,6 @@ script; -mod data_structures; +pub mod data_structures; mod eq_impls; use eq_impls::*; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/match_expressions_constants/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/match_expressions_constants/src/main.sw index ffa92ace5e7..df1535977a6 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/match_expressions_constants/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/match_expressions_constants/src/main.sw @@ -1,6 +1,6 @@ script; -mod lib; +pub mod lib; mod top_level; mod in_structs; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/lib.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/lib.sw index e59f07dba62..94d40f22f5b 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/lib.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/lib.sw @@ -1,7 +1,7 @@ library; -mod a; -mod b; +pub mod a; +pub mod b; fn main() -> u32 { 1 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep_multiple/src/lib.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep_multiple/src/lib.sw index 06ceb2f6e8b..db23337b474 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep_multiple/src/lib.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep_multiple/src/lib.sw @@ -1,8 +1,8 @@ library; mod c; -mod a; -mod b; +pub mod a; +pub mod b; fn main() -> u32 { 1 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/aliases/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/aliases/src/main.sw index 18a842fa725..b1a1ee9b298 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/aliases/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/aliases/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/multiple_imports_of_same_reexport/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/multiple_imports_of_same_reexport/src/main.sw index 4ae46fbab15..01cd29bb68c 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/multiple_imports_of_same_reexport/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/multiple_imports_of_same_reexport/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/reexport_paths/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/reexport_paths/src/main.sw index 84a4dffa3df..738e0d7b893 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/reexport_paths/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/reexport_paths/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/shadowing_in_reexporting_module/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/shadowing_in_reexporting_module/src/main.sw index a2961173628..c0290b47faf 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/shadowing_in_reexporting_module/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/shadowing_in_reexporting_module/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_glob_import/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_glob_import/src/main.sw index 57262307323..fe1ea19f9c1 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_glob_import/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_glob_import/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_item_import/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_item_import/src/main.sw index 0b3777176d7..6470ef09668 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_item_import/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/simple_item_import/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/visibility/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/visibility/src/main.sw index 078a2114541..55d7e81f033 100755 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/visibility/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/reexport/visibility/src/main.sw @@ -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 diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/language/use_absolute_path/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/language/use_absolute_path/src/main.sw index ce2c888d964..4dd52fdef97 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/language/use_absolute_path/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/language/use_absolute_path/src/main.sw @@ -1,6 +1,6 @@ script; -mod r#trait; +pub mod r#trait; mod foo; use ::foo::*; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option/src/main.sw index f37ed2f5bff..ea66b079683 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/option/src/main.sw @@ -1,6 +1,6 @@ script; -mod data_structures; +pub mod data_structures; mod tests; use tests::*; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/main.sw index f37ed2f5bff..ea66b079683 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/stdlib/result/src/main.sw @@ -1,6 +1,6 @@ script; -mod data_structures; +pub mod data_structures; mod tests; use tests::*; diff --git a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/return_struct/src/main.sw b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/return_struct/src/main.sw index 1dd7e0293ea..92e93915701 100644 --- a/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/return_struct/src/main.sw +++ b/test/src/e2e_vm_tests/test_programs/should_pass/test_contracts/return_struct/src/main.sw @@ -1,6 +1,6 @@ contract; -mod data_structures; +pub mod data_structures; mod interface; use interface::MyContract; From 607a6ddf00470fb4abd4b0a4d27b4208edd6ef48 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Nov 2024 14:42:52 +0100 Subject: [PATCH 5/9] fmt --- .../src/semantic_analysis/namespace/root.rs | 46 +++++++++---------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index bc37afd3516..6a408b5f6fa 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -690,34 +690,34 @@ impl Root { src: &ModulePath, dst: &ModulePath, ) -> Result<(), ErrorEmitted> { - // Ignore visibility of common ancestor modules - let mut ignored_prefixes = 0; - - // Ignore visibility of common ancestor modules - for (src_prefix, dst_prefix) in src.iter().zip(dst) { - if src_prefix != dst_prefix { - break; - } - ignored_prefixes += 1; - } - - // 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() { + // Ignore visibility of common ancestor modules + let mut ignored_prefixes = 0; + + // Ignore visibility of common ancestor modules + for (src_prefix, dst_prefix) in src.iter().zip(dst) { + if src_prefix != dst_prefix { + break; + } + ignored_prefixes += 1; + } + + // 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(()) } From 756e77183ad3e2083d638b38faabca95e7be5478 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Nov 2024 15:04:11 +0100 Subject: [PATCH 6/9] Fixed test --- examples/enums/src/main.sw | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/enums/src/main.sw b/examples/enums/src/main.sw index b6804891143..87dbdf428cb 100644 --- a/examples/enums/src/main.sw +++ b/examples/enums/src/main.sw @@ -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; From 1eb6113eee618888536fc5d2736fc8400c765f54 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Nov 2024 21:34:04 +0100 Subject: [PATCH 7/9] Addressed review comments --- .../src/semantic_analysis/namespace/root.rs | 22 +++++++++---------- 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 6a408b5f6fa..ea0d0f7bb80 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -690,23 +690,15 @@ impl Root { src: &ModulePath, dst: &ModulePath, ) -> Result<(), ErrorEmitted> { - // Ignore visibility of common ancestor modules - let mut ignored_prefixes = 0; - - // Ignore visibility of common ancestor modules - for (src_prefix, dst_prefix) in src.iter().zip(dst) { - if src_prefix != dst_prefix { - break; - } - ignored_prefixes += 1; - } - + // Ignore visibility of common ancestors + let mut ignored_prefixes = common_ancestor_count(src, dst); + // 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 + // 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() { @@ -838,6 +830,12 @@ impl From for Root { } } +/// Iterates through two module paths and returns the number of common prefixes of src and dst, +/// i.e., the number of common ancestors of the src and dst modules. +fn common_ancestor_count(src: &ModulePath, dst: &ModulePath) -> usize { + src.iter().zip(dst).position(|(src_id, dst_id)| src_id != dst_id).unwrap_or(dst.len()) +} + fn is_ancestor(src: &ModulePath, dst: &ModulePath) -> bool { dst.len() >= src.len() && src.iter().zip(dst).all(|(src, dst)| src == dst) } From efc37698f94551ecc66cd4b521663187a88d83d8 Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Fri, 1 Nov 2024 21:40:35 +0100 Subject: [PATCH 8/9] fmt --- sway-core/src/semantic_analysis/namespace/root.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index ea0d0f7bb80..215b2ea6eda 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -690,15 +690,15 @@ impl Root { src: &ModulePath, dst: &ModulePath, ) -> Result<(), ErrorEmitted> { - // Ignore visibility of common ancestors - let mut ignored_prefixes = common_ancestor_count(src, dst); - + // Ignore visibility of common ancestors + let mut ignored_prefixes = common_ancestor_count(src, dst); + // 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 + // 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() { @@ -833,7 +833,10 @@ impl From for Root { /// Iterates through two module paths and returns the number of common prefixes of src and dst, /// i.e., the number of common ancestors of the src and dst modules. fn common_ancestor_count(src: &ModulePath, dst: &ModulePath) -> usize { - src.iter().zip(dst).position(|(src_id, dst_id)| src_id != dst_id).unwrap_or(dst.len()) + src.iter() + .zip(dst) + .position(|(src_id, dst_id)| src_id != dst_id) + .unwrap_or(dst.len()) } fn is_ancestor(src: &ModulePath, dst: &ModulePath) -> bool { From c104a667b2a04273d64c67efa535fa0053a6957a Mon Sep 17 00:00:00 2001 From: Jacob Johannsen Date: Wed, 6 Nov 2024 09:56:56 +0100 Subject: [PATCH 9/9] Inline calculation of common ancestor count --- .../src/semantic_analysis/namespace/root.rs | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/sway-core/src/semantic_analysis/namespace/root.rs b/sway-core/src/semantic_analysis/namespace/root.rs index 215b2ea6eda..086d91b31aa 100644 --- a/sway-core/src/semantic_analysis/namespace/root.rs +++ b/sway-core/src/semantic_analysis/namespace/root.rs @@ -683,6 +683,10 @@ 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, @@ -690,8 +694,15 @@ impl Root { src: &ModulePath, dst: &ModulePath, ) -> Result<(), ErrorEmitted> { + // Calculate the number of src prefixes whose privacy is ignored. + let mut ignored_prefixes = 0; + // Ignore visibility of common ancestors - let mut ignored_prefixes = common_ancestor_count(src, dst); + 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 { @@ -830,15 +841,6 @@ impl From for Root { } } -/// Iterates through two module paths and returns the number of common prefixes of src and dst, -/// i.e., the number of common ancestors of the src and dst modules. -fn common_ancestor_count(src: &ModulePath, dst: &ModulePath) -> usize { - src.iter() - .zip(dst) - .position(|(src_id, dst_id)| src_id != dst_id) - .unwrap_or(dst.len()) -} - fn is_ancestor(src: &ModulePath, dst: &ModulePath) -> bool { dst.len() >= src.len() && src.iter().zip(dst).all(|(src, dst)| src == dst) }