mirror of
https://github.com/roc-lang/roc.git
synced 2025-11-01 21:40:58 +00:00
Load and can imports inside defs
After parsing a module, we now recursively traverse the tree to find all imports inside Defs, not just the top-level ones. Previously, imported modules were available in the entire file, but that's no longer the case. Therefore, Scope now keeps track of imported modules and Env::qualified_lookup checks whether a module is available in the provided scope. Note: Unused import warnings are still global and need to be updated.
This commit is contained in:
parent
710d62f754
commit
c617963b22
10 changed files with 434 additions and 84 deletions
|
|
@ -0,0 +1,8 @@
|
|||
interface ExposedUsedOutsideScope exposes [good, bad] imports []
|
||||
|
||||
good =
|
||||
import Dep2 exposing [two]
|
||||
two
|
||||
|
||||
bad =
|
||||
two
|
||||
12
crates/compiler/load_internal/tests/fixtures/build/interface_with_deps/ImportInsideDef.roc
vendored
Normal file
12
crates/compiler/load_internal/tests/fixtures/build/interface_with_deps/ImportInsideDef.roc
vendored
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
interface ImportInsideDef exposes [dep1Str, dep2TwoDobuled] imports []
|
||||
|
||||
dep1Str =
|
||||
import Dep1
|
||||
Dep1.str
|
||||
|
||||
dep2TwoDobuled =
|
||||
2
|
||||
* (
|
||||
import Dep2 exposing [two]
|
||||
two
|
||||
)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
interface ImportUsedOutsideScope exposes [good, bad] imports []
|
||||
|
||||
good =
|
||||
import Dep2
|
||||
Dep2.two
|
||||
|
||||
bad =
|
||||
Dep2.two
|
||||
|
|
@ -438,6 +438,42 @@ fn import_alias() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_inside_def() {
|
||||
let subs_by_module = Default::default();
|
||||
let loaded_module = load_fixture("interface_with_deps", "ImportInsideDef", subs_by_module);
|
||||
|
||||
expect_types(
|
||||
loaded_module,
|
||||
hashmap! {
|
||||
"dep1Str" => "Str",
|
||||
"dep2TwoDobuled" => "Frac *",
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "LookupNotInScope")]
|
||||
fn exposed_used_outside_scope() {
|
||||
let subs_by_module = Default::default();
|
||||
load_fixture(
|
||||
"interface_with_deps",
|
||||
"ExposedUsedOutsideScope",
|
||||
subs_by_module,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
#[should_panic(expected = "ModuleNotImported")]
|
||||
fn import_used_outside_scope() {
|
||||
let subs_by_module = Default::default();
|
||||
load_fixture(
|
||||
"interface_with_deps",
|
||||
"ImportUsedOutsideScope",
|
||||
subs_by_module,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_load_and_typecheck() {
|
||||
let subs_by_module = Default::default();
|
||||
|
|
@ -878,6 +914,15 @@ fn opaque_wrapped_unwrapped_outside_defining_module() {
|
|||
^^^
|
||||
|
||||
Note: Opaque types can only be wrapped and unwrapped in the module they are defined in!
|
||||
|
||||
── UNUSED IMPORT ─── tmp/opaque_wrapped_unwrapped_outside_defining_module/Main ─
|
||||
|
||||
Nothing from Age is used in this module.
|
||||
|
||||
3│ import Age exposing [Age]
|
||||
^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||
|
||||
Since Age isn't used, you don't need to import it.
|
||||
"
|
||||
),
|
||||
"\n{}",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue