diff --git a/compiler/noirc_frontend/src/hir/mod.rs b/compiler/noirc_frontend/src/hir/mod.rs index aabb0e82daa..5a28d7b779a 100644 --- a/compiler/noirc_frontend/src/hir/mod.rs +++ b/compiler/noirc_frontend/src/hir/mod.rs @@ -7,7 +7,7 @@ pub mod type_check; #[cfg(feature = "aztec")] pub(crate) mod aztec_library; -use crate::graph::{CrateGraph, CrateId, Dependency}; +use crate::graph::{CrateGraph, CrateId}; use crate::hir_def::function::FuncMeta; use crate::node_interner::{FuncId, NodeInterner, StructId}; use def_map::{Contract, CrateDefMap}; @@ -119,18 +119,35 @@ impl Context { if &module_id.krate == crate_id { module_path } else { - let crate_name = &self.crate_graph[crate_id] - .dependencies - .iter() - .find_map(|dep| match dep { - Dependency { name, crate_id } if crate_id == &module_id.krate => Some(name), - _ => None, - }) + let crates = self + .find_dependencies(crate_id, &module_id.krate) .expect("The Struct was supposed to be defined in a dependency"); - format!("{crate_name}::{module_path}") + crates.join("::") + "::" + &module_path } } + /// Recursively walks down the crate dependency graph from crate_id until we reach requested crate + /// This is needed in case a library (lib1) re-export a structure defined in another library (lib2) + /// In that case, we will get [lib1,lib2] when looking for a struct defined in lib2, + /// re-exported by lib1 and used by the main crate. + /// Returns the path from crate_id to target_crate_id + fn find_dependencies( + &self, + crate_id: &CrateId, + target_crate_id: &CrateId, + ) -> Option> { + for dep in &self.crate_graph[crate_id].dependencies { + if &dep.crate_id == target_crate_id { + return Some(vec![dep.name.to_string()]); + } + if let Some(mut path) = self.find_dependencies(&dep.crate_id, target_crate_id) { + path.insert(0, dep.name.to_string()); + return Some(path); + } + } + None + } + pub fn function_meta(&self, func_id: &FuncId) -> FuncMeta { self.def_interner.function_meta(func_id) } diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/Nargo.toml b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/Nargo.toml new file mode 100644 index 00000000000..ea2ffc0d2eb --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/Nargo.toml @@ -0,0 +1,7 @@ +[workspace] +members = [ + "library", + "library2", + "binary" +] +default-member = "binary" \ No newline at end of file diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/binary/Nargo.toml b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/binary/Nargo.toml new file mode 100644 index 00000000000..90d8321589b --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/binary/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "binary" +type = "bin" +authors = [""] +[dependencies] +library = { path = "../library" } \ No newline at end of file diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/binary/src/main.nr b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/binary/src/main.nr new file mode 100644 index 00000000000..742ec4187af --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/binary/src/main.nr @@ -0,0 +1,2 @@ +use dep::library::ReExportMeFromAnotherLib; +fn main(_x : ReExportMeFromAnotherLib) {} diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library/Nargo.toml b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library/Nargo.toml new file mode 100644 index 00000000000..88831bada4e --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "library" +type = "lib" +authors = [""] + +[dependencies] +library2 = { path = "../library2"} \ No newline at end of file diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library/src/lib.nr b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library/src/lib.nr new file mode 100644 index 00000000000..41f7b2aeffd --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library/src/lib.nr @@ -0,0 +1,2 @@ +// Re-export +use dep::library2::ReExportMeFromAnotherLib; \ No newline at end of file diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library2/Nargo.toml b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library2/Nargo.toml new file mode 100644 index 00000000000..f2c20c0bf4a --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library2/Nargo.toml @@ -0,0 +1,5 @@ +[package] +name = "library" +type = "lib" +authors = [""] +[dependencies] diff --git a/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library2/src/lib.nr b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library2/src/lib.nr new file mode 100644 index 00000000000..354558183e7 --- /dev/null +++ b/tooling/nargo_cli/tests/compile_success_empty/workspace_reexport_bug/library2/src/lib.nr @@ -0,0 +1,5 @@ +// When we re-export this type from another library and then use it in +// main, we get a panic +struct ReExportMeFromAnotherLib { + x : Field, +} \ No newline at end of file