From 3972ead2593cd1d3f61c3311e948ec27bd9b1491 Mon Sep 17 00:00:00 2001 From: gvozdvmozgu <131762131+gvozdvmozgu@users.noreply.github.com> Date: Fri, 9 Feb 2024 23:07:30 +0200 Subject: [PATCH] fix: message formatting for assert statement (#4323) # 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. --- tooling/nargo_fmt/src/visitor/stmt.rs | 5 ++++- tooling/nargo_fmt/tests/expected/assert.nr | 1 + tooling/nargo_fmt/tests/input/assert.nr | 1 + 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/tooling/nargo_fmt/src/visitor/stmt.rs b/tooling/nargo_fmt/src/visitor/stmt.rs index b414e5ec5c3..44c5dad6b5d 100644 --- a/tooling/nargo_fmt/src/visitor/stmt.rs +++ b/tooling/nargo_fmt/src/visitor/stmt.rs @@ -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 => { diff --git a/tooling/nargo_fmt/tests/expected/assert.nr b/tooling/nargo_fmt/tests/expected/assert.nr index 1f38e56b799..805e069c9a7 100644 --- a/tooling/nargo_fmt/tests/expected/assert.nr +++ b/tooling/nargo_fmt/tests/expected/assert.nr @@ -1,4 +1,5 @@ fn main(x: Field) { assert(x == 0, "with a message"); assert_eq(x, 1); + assert(x, message); } diff --git a/tooling/nargo_fmt/tests/input/assert.nr b/tooling/nargo_fmt/tests/input/assert.nr index f41e396c041..d0259da0e24 100644 --- a/tooling/nargo_fmt/tests/input/assert.nr +++ b/tooling/nargo_fmt/tests/input/assert.nr @@ -4,4 +4,5 @@ fn main(x: Field) { x, 1 ); + assert( x, message ); }