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: follow dependencies when looking for a struct #3405

Merged
merged 10 commits into from
Nov 3, 2023
36 changes: 26 additions & 10 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, CrateName};
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,34 @@ 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,
})
.expect("The Struct was supposed to be defined in a dependency");
format!("{crate_name}::{module_path}")
let crates = self.find_dependencies(crate_id, &module_id.krate);
assert!(!crates.is_empty(), "The Struct was supposed to be defined in a dependency");
let mut fqn = String::new();
for name in crates {
fqn += &name.to_string();
fqn += "::";
}
fqn += &module_path;
fqn
guipublic marked this conversation as resolved.
Show resolved Hide resolved
}
}

/// Recursively walks down the crate dependency graph from crate_id until we reach requested crate
/// returns the path from crate_id to target_crate_id
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
fn find_dependencies(&self, crate_id: &CrateId, target_crate_id: &CrateId) -> Vec<CrateName> {
for dep in &self.crate_graph[crate_id].dependencies {
if &dep.crate_id == target_crate_id {
return vec![dep.name.clone()];
}
let mut fqn = self.find_dependencies(&dep.crate_id, target_crate_id);
if !fqn.is_empty() {
fqn.insert(0, dep.name.clone());
return fqn;
}
}
Vec::new()
}
guipublic marked this conversation as resolved.
Show resolved Hide resolved

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,
}
Loading