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: remove panic for adding an invalid crate name in wasm compiler #3977

Merged
merged 1 commit into from
Jan 8, 2024
Merged
Changes from all 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
31 changes: 18 additions & 13 deletions compiler/wasm/src/compile_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ impl CompilerContext {
crate_name: String,
from: &CrateIDWrapper,
to: &CrateIDWrapper,
) {
let parsed_crate_name: CrateName = crate_name
.parse()
.unwrap_or_else(|_| panic!("Failed to parse crate name {}", crate_name));
) -> Result<(), JsCompileError> {
let parsed_crate_name: CrateName =
crate_name.parse().map_err(|err_string| JsCompileError::new(err_string, Vec::new()))?;

add_dep(&mut self.context, from.0, to.0, parsed_crate_name);
Ok(())
}

pub fn compile_program(
Expand Down Expand Up @@ -188,7 +189,7 @@ pub fn compile_(
crate_names.insert(lib_name.clone(), crate_id);

// Add the dependency edges
compiler_context.add_dependency_edge(lib_name_string, &root_id, &crate_id);
compiler_context.add_dependency_edge(lib_name_string, &root_id, &crate_id)?;
}

// Process the transitive dependencies of the root
Expand All @@ -207,7 +208,11 @@ pub fn compile_(
.entry(dependency_name.clone())
.or_insert_with(|| add_noir_lib(&mut compiler_context, dependency_name));

compiler_context.add_dependency_edge(dependency_name_string, &crate_id, dep_crate_id);
compiler_context.add_dependency_edge(
dependency_name_string,
&crate_id,
dep_crate_id,
)?;
}
}

Expand Down Expand Up @@ -278,8 +283,8 @@ mod test {
let lib1_crate_id = context.process_dependency_crate("lib1/lib.nr".to_string());
let root_crate_id = context.root_crate_id();

context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id);
context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id);
context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id).unwrap();
context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id).unwrap();

assert_eq!(context.crate_graph().number_of_crates(), 3);
}
Expand All @@ -303,9 +308,9 @@ mod test {
let lib3_crate_id = context.process_dependency_crate("lib3/lib.nr".to_string());
let root_crate_id = context.root_crate_id();

context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id);
context.add_dependency_edge("lib2".to_string(), &lib1_crate_id, &lib2_crate_id);
context.add_dependency_edge("lib3".to_string(), &lib2_crate_id, &lib3_crate_id);
context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id).unwrap();
context.add_dependency_edge("lib2".to_string(), &lib1_crate_id, &lib2_crate_id).unwrap();
context.add_dependency_edge("lib3".to_string(), &lib2_crate_id, &lib3_crate_id).unwrap();

assert_eq!(context.crate_graph().number_of_crates(), 5);
}
Expand All @@ -328,8 +333,8 @@ mod test {
let lib3_crate_id = context.process_dependency_crate("lib3/lib.nr".to_string());
let root_crate_id = context.root_crate_id();

context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id);
context.add_dependency_edge("lib3".to_string(), &lib2_crate_id, &lib3_crate_id);
context.add_dependency_edge("lib1".to_string(), &root_crate_id, &lib1_crate_id).unwrap();
context.add_dependency_edge("lib3".to_string(), &lib2_crate_id, &lib3_crate_id).unwrap();

assert_eq!(context.crate_graph().number_of_crates(), 5);
}
Expand Down
Loading