Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle multiple imports in the same file #3903

Merged
merged 6 commits into from
Jan 4, 2024
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
64 changes: 33 additions & 31 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,37 +259,39 @@ impl DefCollector {
);
}

// Resolve unresolved imports collected from the crate
let (resolved, unresolved_imports) =
resolve_imports(crate_id, def_collector.collected_imports, &context.def_maps);

{
let current_def_map = context.def_maps.get(&crate_id).unwrap();
errors.extend(vecmap(unresolved_imports, |(error, module_id)| {
let file_id = current_def_map.file_id(module_id);
let error = DefCollectorErrorKind::PathResolutionError(error);
(error.into(), file_id)
}));
};

// Populate module namespaces according to the imports used
let current_def_map = context.def_maps.get_mut(&crate_id).unwrap();
for resolved_import in resolved {
let name = resolved_import.name;
for ns in resolved_import.resolved_namespace.iter_defs() {
let result = current_def_map.modules[resolved_import.module_scope.0].import(
name.clone(),
ns,
resolved_import.is_prelude,
);

if let Err((first_def, second_def)) = result {
let err = DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Import,
first_def,
second_def,
};
errors.push((err.into(), root_file_id));
// Resolve unresolved imports collected from the crate, one by one.
for collected_import in def_collector.collected_imports {
let (resolved, unresolved_imports) =
resolve_imports(crate_id, vec![collected_import], &context.def_maps);
jfecher marked this conversation as resolved.
Show resolved Hide resolved

{
let current_def_map = context.def_maps.get(&crate_id).unwrap();
errors.extend(vecmap(unresolved_imports, |(error, module_id)| {
let file_id = current_def_map.file_id(module_id);
let error = DefCollectorErrorKind::PathResolutionError(error);
(error.into(), file_id)
}));
};

// Populate module namespaces according to the imports used
let current_def_map = context.def_maps.get_mut(&crate_id).unwrap();
for resolved_import in resolved {
let name = resolved_import.name;
for ns in resolved_import.resolved_namespace.iter_defs() {
let result = current_def_map.modules[resolved_import.module_scope.0].import(
name.clone(),
ns,
resolved_import.is_prelude,
);

if let Err((first_def, second_def)) = result {
let err = DefCollectorErrorKind::Duplicate {
typ: DuplicateType::Import,
first_def,
second_def,
};
errors.push((err.into(), root_file_id));
}
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions test_programs/execution_success/regression_3889/Nargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "regression_3889"
version = "0.1.0"
type = "bin"
authors = [""]

[dependencies]
10 changes: 10 additions & 0 deletions test_programs/execution_success/regression_3889/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[works]
a = "5"

[fails]
a = "6"


[also_fails]
a = "7"

23 changes: 23 additions & 0 deletions test_programs/execution_success/regression_3889/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
mod Foo {
struct NewType{
a: Field,
}
}

mod Bar {
use crate::Foo::NewType as BarStruct;
use crate::Foo::NewType;
}

mod Baz {
struct Works {
a: Field,
}
use crate::Bar::BarStruct;
use crate::Bar::NewType;
}


fn main(works: Baz::Works, fails: Baz::BarStruct, also_fails: Bar::NewType) -> pub Field {
works.a + fails.a + also_fails.a
}
Loading