-
Notifications
You must be signed in to change notification settings - Fork 5.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement order-independent module evaluation (#5300)
## Description This PR implements a new module evaluation system to allow order-independent mod statements when importing modules. It supports these sort of mod patterns to compile: `a.sw`: ```rust library; use ::b::*; pub fn a() { b::b(); } ``` `b.sw`: ```rust library; pub fn b() {} ``` `lib.sw`: ```rust library; mod a; mod b; fn main() -> u32 { b::b(); 0 } ``` To make this work, we now analyze the submodule structure first, creating a dependency graph that we later use to inform the module evaluation order. We also now check for cyclic dependencies using the same graph, and report them when they are found. Closes #409. ## 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] 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.
- Loading branch information
Showing
27 changed files
with
386 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_fail/module_cyclic_reference/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[[package]] | ||
name = "module_cyclic_reference" | ||
source = "member" |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_fail/module_cyclic_reference/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "lib.sw" | ||
license = "Apache-2.0" | ||
name = "module_cyclic_reference" | ||
implicit-std = false |
5 changes: 5 additions & 0 deletions
5
test/src/e2e_vm_tests/test_programs/should_fail/module_cyclic_reference/src/a.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
library; | ||
|
||
use ::b::*; | ||
|
||
pub fn a() { b(); } |
5 changes: 5 additions & 0 deletions
5
test/src/e2e_vm_tests/test_programs/should_fail/module_cyclic_reference/src/b.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
library; | ||
|
||
use ::a::*; | ||
|
||
pub fn b() {} |
9 changes: 9 additions & 0 deletions
9
test/src/e2e_vm_tests/test_programs/should_fail/module_cyclic_reference/src/lib.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
library; | ||
|
||
mod a; | ||
mod b; | ||
|
||
fn main() -> u32 { | ||
a::a(); | ||
0 | ||
} |
4 changes: 4 additions & 0 deletions
4
test/src/e2e_vm_tests/test_programs/should_fail/module_cyclic_reference/test.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
category = "fail" | ||
|
||
# check: $()A cyclic reference was found between the modules: a, b. | ||
|
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/Forc.lock
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[[package]] | ||
name = "module_dep" | ||
source = "member" |
6 changes: 6 additions & 0 deletions
6
test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/Forc.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[project] | ||
authors = ["Fuel Labs <contact@fuel.sh>"] | ||
entry = "lib.sw" | ||
license = "Apache-2.0" | ||
name = "module_dep" | ||
implicit-std = false |
5 changes: 5 additions & 0 deletions
5
test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/a.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
library; | ||
|
||
use ::b::*; | ||
|
||
pub fn a() { b(); } |
3 changes: 3 additions & 0 deletions
3
test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/b.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
library; | ||
|
||
pub fn b() {} |
8 changes: 8 additions & 0 deletions
8
test/src/e2e_vm_tests/test_programs/should_pass/language/module_dep/src/lib.sw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
library; | ||
|
||
mod a; | ||
mod b; | ||
|
||
fn main() -> u32 { | ||
1 | ||
} |
Oops, something went wrong.