Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: fix wrapping issue for constraints #3590

Merged
merged 1 commit into from Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions tooling/nargo_fmt/src/visitor/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use noirc_frontend::{
ConstrainKind, ConstrainStatement, ExpressionKind, ForRange, Statement, StatementKind,
};

use crate::rewrite;
use crate::{rewrite, visitor::expr::wrap_exprs};

use super::ExpressionType;

Expand Down Expand Up @@ -33,30 +33,44 @@ impl super::FmtVisitor<'_> {
self.push_rewrite(format!("{let_str} {expr_str};"), span);
}
StatementKind::Constrain(ConstrainStatement(expr, message, kind)) => {
let mut nested_shape = self.shape();
let shape = nested_shape;

nested_shape.indent.block_indent(self.config);

let message =
message.map_or(String::new(), |message| format!(", \"{message}\""));
let constrain = match kind {

let (callee, args) = match kind {
ConstrainKind::Assert => {
let assertion = rewrite::sub_expr(self, self.shape(), expr);
let assertion = rewrite::sub_expr(self, nested_shape, expr);
let args = format!("{assertion}{message}");

format!("assert({assertion}{message});")
("assert", args)
}
ConstrainKind::AssertEq => {
if let ExpressionKind::Infix(infix) = expr.kind {
let lhs = rewrite::sub_expr(self, self.shape(), infix.lhs);
let rhs = rewrite::sub_expr(self, self.shape(), infix.rhs);
let lhs = rewrite::sub_expr(self, nested_shape, infix.lhs);
let rhs = rewrite::sub_expr(self, nested_shape, infix.rhs);

format!("assert_eq({lhs}, {rhs}{message});")
let args = format!("{lhs}, {rhs}{message}");

("assert_eq", args)
} else {
unreachable!()
}
}
ConstrainKind::Constrain => {
let expr = rewrite::sub_expr(self, self.shape(), expr);
format!("constrain {expr};")
let constrain = format!("constrain {expr};");
self.push_rewrite(constrain, span);
return;
}
};

let args = wrap_exprs("(", ")", args, nested_shape, shape, true);
let constrain = format!("{callee}{args};");

self.push_rewrite(constrain, span);
}
StatementKind::For(for_stmt) => {
Expand Down
12 changes: 7 additions & 5 deletions tooling/nargo_fmt/tests/expected/call.nr
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ fn foo() {

assert(x == y);

assert(p4_affine.eq(
Gaffine::new(
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889
assert(
p4_affine.eq(
Gaffine::new(
6890855772600357754907169075114257697580319025794532037257385534741338397365,
4338620300185947561074059802482547481416142213883829469920100239455078257889
)
)
));
);
}
6 changes: 4 additions & 2 deletions tooling/nargo_fmt/tests/expected/infix.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ fn foo() {
}

fn big() {
assert(bjj_affine.contains(bjj_affine.gen)
assert(
bjj_affine.contains(bjj_affine.gen)
& bjj_affine.contains(p1_affine)
& bjj_affine.contains(p2_affine)
& bjj_affine.contains(p3_affine)
& bjj_affine.contains(p4_affine)
& bjj_affine.contains(p5_affine));
& bjj_affine.contains(p5_affine)
This conversation was marked as resolved.
Show resolved Hide resolved
);
}
Loading