Skip to content

Commit

Permalink
fix: follow dependencies when looking for a struct (#3405)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
2 people authored and TomAFrench committed Nov 14, 2023
1 parent bd4a43f commit 84d7dde
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 9 deletions.
35 changes: 26 additions & 9 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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<Vec<String>> {
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)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[workspace]
members = [
"library",
"library2",
"binary"
]
default-member = "binary"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "binary"
type = "bin"
authors = [""]
[dependencies]
library = { path = "../library" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
use dep::library::ReExportMeFromAnotherLib;
fn main(_x : ReExportMeFromAnotherLib) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "library"
type = "lib"
authors = [""]

[dependencies]
library2 = { path = "../library2"}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Re-export
use dep::library2::ReExportMeFromAnotherLib;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
name = "library"
type = "lib"
authors = [""]
[dependencies]
Original file line number Diff line number Diff line change
@@ -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,
}

0 comments on commit 84d7dde

Please sign in to comment.