Skip to content

Commit

Permalink
fix: message formatting for assert statement (#4323)
Browse files Browse the repository at this point in the history
# Description

Due to our use of Display for message formatting, we were generating
invalid code.

Output before the PR:
```rust
assert(x, plain::message);
```
Output after the PR:
```rust
assert(x, message);
```

## 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
gvozdvmozgu authored Feb 9, 2024
1 parent 69b8912 commit 3972ead
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
5 changes: 4 additions & 1 deletion tooling/nargo_fmt/src/visitor/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ impl super::FmtVisitor<'_> {

nested_shape.indent.block_indent(self.config);

let message = message.map_or(String::new(), |message| format!(", {message}"));
let message = message.map_or(String::new(), |message| {
let message = rewrite::sub_expr(self, nested_shape, message);
format!(", {message}")
});

let (callee, args) = match kind {
ConstrainKind::Assert | ConstrainKind::Constrain => {
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/expected/assert.nr
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fn main(x: Field) {
assert(x == 0, "with a message");
assert_eq(x, 1);
assert(x, message);
}
1 change: 1 addition & 0 deletions tooling/nargo_fmt/tests/input/assert.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ fn main(x: Field) {
x,
1
);
assert( x, message );
}

0 comments on commit 3972ead

Please sign in to comment.