From fd150521a480c04ff64f84e3c1a2faf1e8394516 Mon Sep 17 00:00:00 2001 From: Tom French <15848336+TomAFrench@users.noreply.github.com> Date: Wed, 7 Feb 2024 16:09:55 +0000 Subject: [PATCH] fix: simplify constant assert messages into `ConstrainError::Static` (#4287) # Description ## Problem\* Resolves ## Summary\* This PR removes the unnecessary foreign call we're performing for static error messages. ## Additional Context ## Documentation\* Check one: - [x] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** 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. --- compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs | 6 ++++++ .../noirc_frontend/src/hir/resolution/resolver.rs | 15 +++++++++------ tooling/debugger/src/context.rs | 4 ++-- 3 files changed, 17 insertions(+), 8 deletions(-) diff --git a/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs b/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs index 76fefae9864..3780477cf71 100644 --- a/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs +++ b/compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs @@ -688,6 +688,12 @@ impl<'a> FunctionContext<'a> { return Ok(None) }; + if let ast::Expression::Literal(ast::Literal::Str(assert_message)) = + assert_message_expr.as_ref() + { + return Ok(Some(Box::new(ConstrainError::Static(assert_message.to_string())))); + } + let ast::Expression::Call(call) = assert_message_expr.as_ref() else { return Err(InternalError::Unexpected { expected: "Expected a call expression".to_owned(), diff --git a/compiler/noirc_frontend/src/hir/resolution/resolver.rs b/compiler/noirc_frontend/src/hir/resolution/resolver.rs index b899a5a325a..347bf9451f6 100644 --- a/compiler/noirc_frontend/src/hir/resolution/resolver.rs +++ b/compiler/noirc_frontend/src/hir/resolution/resolver.rs @@ -1211,12 +1211,14 @@ impl<'a> Resolver<'a> { span: Span, condition: Expression, ) -> Option { - let mut assert_msg_call_args = if let Some(assert_message_expr) = assert_message_expr { - vec![assert_message_expr.clone()] - } else { - return None; - }; - assert_msg_call_args.push(condition); + let assert_message_expr = assert_message_expr?; + + if matches!( + assert_message_expr, + Expression { kind: ExpressionKind::Literal(Literal::Str(..)), .. } + ) { + return Some(self.resolve_expression(assert_message_expr)); + } let is_in_stdlib = self.path_resolver.module_id().krate.is_stdlib(); let assert_msg_call_path = if is_in_stdlib { @@ -1232,6 +1234,7 @@ impl<'a> Resolver<'a> { span, }) }; + let assert_msg_call_args = vec![assert_message_expr.clone(), condition]; let assert_msg_call_expr = Expression::call( Expression { kind: assert_msg_call_path, span }, assert_msg_call_args, diff --git a/tooling/debugger/src/context.rs b/tooling/debugger/src/context.rs index 3cababe7605..618f00b5ce1 100644 --- a/tooling/debugger/src/context.rs +++ b/tooling/debugger/src/context.rs @@ -530,7 +530,7 @@ mod tests { let initial_witness = BTreeMap::from([(Witness(1), fe_1)]).into(); let foreign_call_executor = - Box::new(DefaultDebugForeignCallExecutor::from_artifact(true, &debug_artifact)); + Box::new(DefaultDebugForeignCallExecutor::from_artifact(true, debug_artifact)); let mut context = DebugContext::new( &StubbedBlackBoxSolver, circuit, @@ -624,7 +624,7 @@ mod tests { let initial_witness = BTreeMap::from([(Witness(1), fe_1), (Witness(2), fe_1)]).into(); let foreign_call_executor = - Box::new(DefaultDebugForeignCallExecutor::from_artifact(true, &debug_artifact)); + Box::new(DefaultDebugForeignCallExecutor::from_artifact(true, debug_artifact)); let mut context = DebugContext::new( &StubbedBlackBoxSolver, circuit,