From ac738b21bc19181b021f909a8e60752dff5ac713 Mon Sep 17 00:00:00 2001 From: jfecher Date: Tue, 2 Jul 2024 07:31:29 -0500 Subject: [PATCH] fix: Replace expects in interpreter with errors (#5383) # Description ## Problem\* Resolves ## Summary\* ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [x] I have tested the changes locally. - [x] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- .../src/hir/comptime/interpreter.rs | 27 +++++++++++++++---- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs index d2b98569bbb..d8f17e40f08 100644 --- a/compiler/noirc_frontend/src/hir/comptime/interpreter.rs +++ b/compiler/noirc_frontend/src/hir/comptime/interpreter.rs @@ -882,7 +882,10 @@ impl<'a> Interpreter<'a> { 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, @@ -1209,8 +1212,15 @@ impl<'a> Interpreter<'a> { } } 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)) @@ -1254,9 +1264,16 @@ impl<'a> Interpreter<'a> { } } 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 {