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(ssa refactor): Ignore array out of bounds errors when enable_side_effects is false #1797

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ impl AcirContext {
self.add_data(var_data)
}

/// True if the given AcirVar refers to a constant zero value
pub(crate) fn is_constant_zero(&self, var: &AcirVar) -> bool {
match self.vars[var] {
AcirVarData::Const(field) => field.is_zero(),
_ => false,
}
}

/// Adds a new Variable to context whose value will
/// be constrained to be the negation of `var`.
///
Expand Down
19 changes: 19 additions & 0 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,20 @@ impl Context {
.try_to_u64()
.expect("Expected array index to fit into a u64") as usize;

if index >= array.len() {
// Ignore the error if side effects are disabled.
if let Some(var) = self.current_side_effects_enabled_var {
if self.acir_context.is_constant_zero(&var) {
let result_type = dfg.type_of_value(dfg.instruction_results(instruction)[0]);
let value = self.create_default_value(result_type);
self.define_result(dfg, instruction, value);
return;
}
}
// TODO: Can we save a source Location for this error?
panic!("Index {} is out of bounds for array of length {}", index, array.len());
}

let value = match store_value {
Some(store_value) => {
let store_value = self.convert_value(store_value, dfg);
Expand Down Expand Up @@ -680,6 +694,11 @@ impl Context {
}
}
}

/// Creates a default, meaningless value meant only to be a valid value of the given type.
fn create_default_value(&mut self, result_type: Type) -> AcirValue {
self.convert_ssa_block_param(&result_type)
}
}

#[cfg(test)]
Expand Down