From 87df9d126ddc3e45d48f1def39422b95f995f931 Mon Sep 17 00:00:00 2001 From: Magamedrasul Ibragimov Date: Sun, 16 Jun 2024 12:50:01 +0300 Subject: [PATCH 1/3] refactor: fix clippy warnings --- src/compiler.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/compiler.rs b/src/compiler.rs index 682e072..c3cfd83 100644 --- a/src/compiler.rs +++ b/src/compiler.rs @@ -438,7 +438,7 @@ impl Compiler { let mut next_wire_id = 0; // First inputs - for (_, node_id) in &input_to_node_id { + for node_id in input_to_node_id.values() { node_id_to_wire_id.insert(*node_id, next_wire_id); next_wire_id += 1; } @@ -492,7 +492,7 @@ impl Compiler { } // Assign wire ids to output nodes - for (_, node_id) in &output_to_node_id { + for node_id in output_to_node_id.values() { node_id_to_wire_id.insert(*node_id, next_wire_id); next_wire_id += 1; } From 6dfd890ca2a5c52b4d71793d0065fe1fa00cdcc5 Mon Sep 17 00:00:00 2001 From: Magamedrasul Ibragimov Date: Sun, 16 Jun 2024 14:51:38 +0300 Subject: [PATCH 2/3] chore: add error_handler module --- src/error_handler.rs | 22 ++++++++++++++++++++++ src/lib.rs | 1 + src/process.rs | 9 +++++++-- src/runtime.rs | 4 ++-- 4 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 src/error_handler.rs diff --git a/src/error_handler.rs b/src/error_handler.rs new file mode 100644 index 0000000..7c233e5 --- /dev/null +++ b/src/error_handler.rs @@ -0,0 +1,22 @@ +use std::fs; + +pub fn get_line_and_column(file_path: &str, index: usize) -> Option { + let content = fs::read_to_string(file_path).ok()?; + let line_num: usize; + let col_num: usize; + let mut current_index = 0; + + for (line_idx, line) in content.lines().enumerate() { + let line_length = line.len() + 1; + if current_index + line_length > index { + line_num = line_idx + 1; + col_num = index - current_index + 1; + return Some(format!( + "file {} , line {}, column {}", + file_path, line_num, col_num + )); + } + current_index += line_length; + } + None +} diff --git a/src/lib.rs b/src/lib.rs index c6b822f..5310982 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ pub mod arithmetic_circuit; pub mod circom; pub mod cli; pub mod compiler; +pub mod error_handler; pub mod process; pub mod program; pub mod runtime; diff --git a/src/process.rs b/src/process.rs index 71b13cf..ac1d49e 100644 --- a/src/process.rs +++ b/src/process.rs @@ -3,6 +3,7 @@ //! Handles execution of statements and expressions for arithmetic circuit generation within a `Runtime` environment. use crate::compiler::{AGateType, Compiler}; +use crate::error_handler::get_line_and_column; use crate::program::ProgramError; use crate::runtime::{ generate_u32, increment_indices, u32_to_access, Context, DataAccess, DataType, NestedValue, @@ -170,15 +171,19 @@ pub fn process_statement( Ok(()) } - Statement::Assert { arg, .. } => { + Statement::Assert { meta, arg, .. } => { let access = process_expression(ac, runtime, program_archive, arg)?; let result = runtime .current_context()? .get_variable_value(&access)? .ok_or(ProgramError::EmptyDataItem)?; + let message = get_line_and_column(meta.file_id.unwrap(), meta.start).unwrap(); + if result == 0 { - return Err(ProgramError::RuntimeError(RuntimeError::AssertionFailed)); + return Err(ProgramError::RuntimeError(RuntimeError::AssertionFailed( + message, + ))); } Ok(()) diff --git a/src/runtime.rs b/src/runtime.rs index 5075e79..3bc07cf 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -824,8 +824,8 @@ pub enum RuntimeError { NotAValue, #[error("Unsupported data type")] UnsupportedDataType, - #[error("Assertion failed")] - AssertionFailed, + #[error("Assertion Failed in {0}")] + AssertionFailed(String), } impl From for ProgramError { From 20ac7f83ac7cb5d19365a1d61559d677deebe149 Mon Sep 17 00:00:00 2001 From: Magamedrasul Ibragimov Date: Sun, 16 Jun 2024 15:08:11 +0300 Subject: [PATCH 3/3] chore: fix the error function --- src/process.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/process.rs b/src/process.rs index ac1d49e..15b9119 100644 --- a/src/process.rs +++ b/src/process.rs @@ -178,7 +178,14 @@ pub fn process_statement( .get_variable_value(&access)? .ok_or(ProgramError::EmptyDataItem)?; - let message = get_line_and_column(meta.file_id.unwrap(), meta.start).unwrap(); + let file_path = program_archive + .file_library + .get_files() + .get(meta.file_id.unwrap()) + .unwrap() + .name(); + + let message = get_line_and_column(&file_path, meta.start).unwrap(); if result == 0 { return Err(ProgramError::RuntimeError(RuntimeError::AssertionFailed(