Skip to content

Commit

Permalink
fix: simplify constant assert messages into ConstrainError::Static (#…
Browse files Browse the repository at this point in the history
…4287)

# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## 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.
  • Loading branch information
TomAFrench committed Feb 7, 2024
1 parent 37f149c commit fd15052
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 6 additions & 0 deletions compiler/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 9 additions & 6 deletions compiler/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1211,12 +1211,14 @@ impl<'a> Resolver<'a> {
span: Span,
condition: Expression,
) -> Option<ExprId> {
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 {
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions tooling/debugger/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit fd15052

Please sign in to comment.