Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions crates/rue-compiler/src/compile/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn construct_imports(
exported: bool,
) -> Vec<ImportId> {
let mut has_non_super = false;
let mut has_super = false;

if let Some(segment) = segments.first()
&& let Some(separator) = segment.separator()
Expand All @@ -59,6 +60,8 @@ fn construct_imports(
} else {
ctx.diagnostic(&name, DiagnosticKind::UnresolvedSuper);
}

has_super = true;
} else {
path.push(ctx.local_name(&name));
has_non_super = true;
Expand Down Expand Up @@ -88,6 +91,7 @@ fn construct_imports(
items: Items::Named(name),
exported,
declarations: Vec::new(),
has_super,
})]
} else if let Some(star) = last.star() {
let star = ctx.local_name(&star);
Expand All @@ -99,6 +103,7 @@ fn construct_imports(
items: Items::All(star),
exported,
declarations: Vec::new(),
has_super,
})]
} else if let items = last.items().collect::<Vec<_>>()
&& !items.is_empty()
Expand All @@ -124,7 +129,7 @@ fn construct_imports(

#[derive(Debug, Default)]
pub struct ImportCache {
scopes: HashMap<Vec<String>, (ScopeId, SymbolId)>,
scopes: HashMap<(ScopeId, Vec<String>), (ScopeId, SymbolId)>,
unused_imports: IndexMap<ImportId, IndexMap<String, Name>>,
glob_import_counts: IndexMap<ImportId, (Name, usize)>,
}
Expand Down Expand Up @@ -214,6 +219,7 @@ fn resolve_import(
missing_imports: &mut IndexMap<ImportId, IndexMap<String, Name>>,
) -> bool {
let import = ctx.import(import_id).clone();
let has_super = import.has_super;
let source = import.source.clone();

let mut base = None;
Expand All @@ -227,7 +233,7 @@ fn resolve_import(
.map(|t| t.text().to_string())
.collect::<Vec<_>>();

if let Some(cached) = cache.scopes.get(&subpath) {
if let Some(cached) = cache.scopes.get(&(import_scope, subpath.clone())) {
base = Some(cached.0);
path_so_far = subpath;

Expand Down Expand Up @@ -300,9 +306,12 @@ fn resolve_import(

base = Some(module.scope);
path_so_far.push(name.text().to_string());
cache
.scopes
.insert(path_so_far.clone(), (module.scope, symbol));

if !has_super {
cache
.scopes
.insert((import_scope, path_so_far.clone()), (module.scope, symbol));
}
}

let mut updated = false;
Expand Down
1 change: 1 addition & 0 deletions crates/rue-hir/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Import {
pub items: Items,
pub exported: bool,
pub declarations: Vec<(String, Declaration)>,
pub has_super: bool,
}

#[derive(Debug, Clone)]
Expand Down
3 changes: 3 additions & 0 deletions tests/projects/implicit_root_import.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
diagnostics:
- Undeclared symbol `utils` at a/b/inner.rue:1:8
- Undeclared symbol `greet` at a/b/inner.rue:4:5
5 changes: 5 additions & 0 deletions tests/projects/implicit_root_import/a/b/inner.rue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import utils::greet;

test fn inner() -> String {
greet("world inner")
}
5 changes: 5 additions & 0 deletions tests/projects/implicit_root_import/main.rue
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import utils::greet;

fn main() -> String {
greet("world main")
}
3 changes: 3 additions & 0 deletions tests/projects/implicit_root_import/utils.rue
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export fn greet(name: String) -> String {
"Hello, " + name + "!"
}