Skip to content

Commit ad9dbd2

Browse files
authored
Add tests of visibility of private direct submodules (#6849)
## Description #5765 was solved by an earlier PR, but we don't seem to have regression tests for that situation. This PR adds those tests. ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] If my change requires substantial documentation changes, I have [requested support from the DevRel team](https://github.com/FuelLabs/devrel-requests/issues/new/choose) - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
1 parent 0a56930 commit ad9dbd2

File tree

12 files changed

+110
-0
lines changed

12 files changed

+110
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[package]]
2+
name = "submodule_visibility"
3+
source = "member"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
entry = "main.sw"
4+
license = "Apache-2.0"
5+
name = "submodule_visibility"
6+
implicit-std = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
library;
2+
3+
// other is a submodule that declares a private submodule lib
4+
// lib contains a declaration of the public struct S, but since lib is private it is not visible here.
5+
// It is visible inside other, though.
6+
pub mod other;
7+
8+
// lib is private, and not a direct submodule of the current module, so this should fail
9+
use other::lib::S;
10+
11+
pub fn foo() {
12+
let my_struct = S { val: 0 };
13+
14+
// lib is private, and not a direct submodule of the current module, so this should fail
15+
let my_other_struct = lib::T { val: 1 };
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
library;
2+
3+
mod lib; // private submodule
4+
5+
// lib is private, but since it is a direct submodule we can access its public items.
6+
use lib::S;
7+
8+
// Public function
9+
pub fn foo() {
10+
let my_struct = S { val: 0 };
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
library;
2+
3+
pub struct S {
4+
pub val: u64,
5+
}
6+
7+
pub struct T {
8+
pub val: u64,
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
category = "fail"
2+
3+
# check: $()error
4+
# check: $()Module "lib" is private.
5+
6+
# check: $()error
7+
# check: $()Module "lib" could not be found.
8+
9+
# check: $()Aborting due to 2 errors.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[[package]]
2+
name = "submodule_visibility"
3+
source = "member"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[project]
2+
authors = ["Fuel Labs <contact@fuel.sh>"]
3+
entry = "main.sw"
4+
license = "Apache-2.0"
5+
name = "submodule_visibility"
6+
implicit-std = false
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
library;
2+
3+
// other is a submodule that declares a private submodule lib
4+
// lib contains a declaration of the public struct S, but since lib is private it is not visible here.
5+
// It is visible inside other, though.
6+
pub mod other;
7+
8+
// other reexports other::lib::U, so we can access it as other::U
9+
use other::U;
10+
11+
// Public function
12+
pub fn foo() {
13+
let my_struct = U { val: 0 };
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
library;
2+
3+
mod lib; // private submodule
4+
5+
// lib is private, but since it is a direct submodule we can access its public items.
6+
use lib::S;
7+
8+
// lib is private, but since it is a direct submodule we can access its public items.
9+
// Reexporting it makes it visible from here, but not from lib
10+
pub use lib::U;
11+
12+
// Public function
13+
pub fn foo() {
14+
let my_struct = S { val: 0 };
15+
16+
// lib is private, but since it is a direct submodule we can access its public items.
17+
let my_other_struct = lib::T { val: 1 };
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
library;
2+
3+
pub struct S {
4+
pub val: u64,
5+
}
6+
7+
pub struct T {
8+
pub val: u64,
9+
}
10+
11+
pub struct U {
12+
pub val: u64,
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
category = "compile"
2+
expected_warnings = 3

0 commit comments

Comments
 (0)