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: Replace expects in interpreter with errors #5383

Merged
merged 1 commit into from
Jul 2, 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
27 changes: 22 additions & 5 deletions compiler/noirc_frontend/src/hir/comptime/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@
}
} else {
let name = self.interner.function_name(&function);
unreachable!("Non-builtin, lowlevel or oracle builtin fn '{name}'")

Check warning on line 141 in compiler/noirc_frontend/src/hir/comptime/interpreter.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (lowlevel)
}
}

Expand Down Expand Up @@ -882,7 +882,10 @@

let index = match index {
Value::Field(value) => {
value.try_to_u64().expect("index could not fit into u64") as usize
value.try_to_u64().and_then(|value| value.try_into().ok()).ok_or_else(|| {
let typ = Type::default_int_type();
InterpreterError::IntegerOutOfRangeForType { value, typ, location }
})?
}
Value::I8(value) => value as usize,
Value::I16(value) => value as usize,
Expand Down Expand Up @@ -1209,8 +1212,15 @@
}
}
HirLValue::MemberAccess { object, field_name, field_index, typ: _, location } => {
let index = field_index.expect("The field index should be set after type checking");
match self.evaluate_lvalue(&object)? {
let object_value = self.evaluate_lvalue(&object)?;

let index = field_index.ok_or_else(|| {
let value = object_value.clone();
let field_name = field_name.to_string();
InterpreterError::ExpectedStructToHaveField { value, field_name, location }
})?;

match object_value {
Value::Tuple(mut fields) => {
fields[index] = rhs;
self.store_lvalue(*object, Value::Tuple(fields))
Expand Down Expand Up @@ -1254,9 +1264,16 @@
}
}
HirLValue::MemberAccess { object, field_name, field_index, typ: _, location } => {
let index = field_index.expect("The field index should be set after type checking");
let object_value = self.evaluate_lvalue(object)?;

let index = field_index.ok_or_else(|| {
let value = object_value.clone();
let field_name = field_name.to_string();
let location = *location;
InterpreterError::ExpectedStructToHaveField { value, field_name, location }
})?;

match self.evaluate_lvalue(object)? {
match object_value {
Value::Tuple(mut values) => Ok(values.swap_remove(index)),
Value::Struct(fields, _) => Ok(fields[&field_name.0.contents].clone()),
value => Err(InterpreterError::NonTupleOrStructInMemberAccess {
Expand Down
Loading