From 508e0302391839546dfc14065be5c89ff0bd1849 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 17:15:37 +0100 Subject: [PATCH 1/8] Add constructor --- tooling/nargo_fmt/src/visitor/expr.rs | 12 ++++++++++++ tooling/nargo_fmt/tests/expected/constructor.nr | 8 ++++++++ tooling/nargo_fmt/tests/input/constructor.nr | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 tooling/nargo_fmt/tests/expected/constructor.nr create mode 100644 tooling/nargo_fmt/tests/input/constructor.nr diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index fa55ac71f78..5b3c8bb1b23 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -39,6 +39,18 @@ impl FmtVisitor<'_> { self.format_expr(infix.rhs) ) } + ExpressionKind::Constructor(constructor_expr) => { + let type_str = constructor_expr.type_name.to_string(); + let formatted_fields = constructor_expr + .fields + .iter() + .map(|(field_ident, field_value)| { + format!("{}: {}", field_ident, self.format_expr(field_value.clone())) + }) + .collect::>() + .join(", "); + format!("{} {{ {} }}", type_str, formatted_fields) + } ExpressionKind::Literal(literal) => match literal { Literal::Integer(_) => slice!(self, span.start(), span.end()).to_string(), Literal::Array(ArrayLiteral::Repeated { repeated_element, length }) => { diff --git a/tooling/nargo_fmt/tests/expected/constructor.nr b/tooling/nargo_fmt/tests/expected/constructor.nr new file mode 100644 index 00000000000..4d711cca549 --- /dev/null +++ b/tooling/nargo_fmt/tests/expected/constructor.nr @@ -0,0 +1,8 @@ +struct Point { + x: i32, + y: i32, +} + +fn foo() { + Point { x: 5, y: 10 }; +} diff --git a/tooling/nargo_fmt/tests/input/constructor.nr b/tooling/nargo_fmt/tests/input/constructor.nr new file mode 100644 index 00000000000..3a7bf1defa3 --- /dev/null +++ b/tooling/nargo_fmt/tests/input/constructor.nr @@ -0,0 +1,8 @@ +struct Point { + x: u8, + y: u8, +} + +fn foo() { + Point{x :5,y: 10 }; +} From c163f295a53161110b2f07bb6ff9324a8b6614cb Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 18:17:29 +0100 Subject: [PATCH 2/8] Use vecmap --- tooling/nargo_fmt/src/visitor/expr.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 5b3c8bb1b23..41543a15c9f 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -4,6 +4,7 @@ use noirc_frontend::{ }; use super::FmtVisitor; +use iter_extended::vecmap; impl FmtVisitor<'_> { pub(crate) fn visit_expr(&mut self, expr: Expression) { @@ -41,13 +42,11 @@ impl FmtVisitor<'_> { } ExpressionKind::Constructor(constructor_expr) => { let type_str = constructor_expr.type_name.to_string(); - let formatted_fields = constructor_expr - .fields - .iter() - .map(|(field_ident, field_value)| { + + let formatted_fields = constructor_expr.fields + .vecmap(|(field_ident, field_value)| { format!("{}: {}", field_ident, self.format_expr(field_value.clone())) }) - .collect::>() .join(", "); format!("{} {{ {} }}", type_str, formatted_fields) } From 6a45f15e601b28812980a964fadf0def34437608 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 18:18:19 +0100 Subject: [PATCH 3/8] Use multiline --- tooling/nargo_fmt/tests/input/constructor.nr | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tooling/nargo_fmt/tests/input/constructor.nr b/tooling/nargo_fmt/tests/input/constructor.nr index 3a7bf1defa3..22909350520 100644 --- a/tooling/nargo_fmt/tests/input/constructor.nr +++ b/tooling/nargo_fmt/tests/input/constructor.nr @@ -4,5 +4,6 @@ struct Point { } fn foo() { - Point{x :5,y: 10 }; + Point{x :5, + y: 10 }; } From 3dac5c673e2c1395c2f04da33c524205257b2db3 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 18:52:09 +0100 Subject: [PATCH 4/8] Fix vecmap --- tooling/nargo_fmt/src/visitor/expr.rs | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index a0ab4812202..02060dbd04e 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -42,12 +42,10 @@ impl FmtVisitor<'_> { } ExpressionKind::Constructor(constructor_expr) => { let type_str = constructor_expr.type_name.to_string(); - - let formatted_fields = constructor_expr.fields - .vecmap(|(field_ident, field_value)| { - format!("{}: {}", field_ident, self.format_expr(field_value.clone())) - }) - .join(", "); + let formatted_fields = vecmap(&constructor_expr.fields, |(field_ident, field_value)| { + format!("{}: {}", field_ident, self.format_expr(field_value.clone())) + }) + .join(", "); format!("{} {{ {} }}", type_str, formatted_fields) } ExpressionKind::Index(index_expr) => { From a1360f843f19af5a76ce8b1d58fc42a265569ca0 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 18:52:25 +0100 Subject: [PATCH 5/8] cargo fmt --- tooling/nargo_fmt/src/visitor/expr.rs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 02060dbd04e..757994d2f4b 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -42,10 +42,11 @@ impl FmtVisitor<'_> { } ExpressionKind::Constructor(constructor_expr) => { let type_str = constructor_expr.type_name.to_string(); - let formatted_fields = vecmap(&constructor_expr.fields, |(field_ident, field_value)| { - format!("{}: {}", field_ident, self.format_expr(field_value.clone())) - }) - .join(", "); + let formatted_fields = + vecmap(&constructor_expr.fields, |(field_ident, field_value)| { + format!("{}: {}", field_ident, self.format_expr(field_value.clone())) + }) + .join(", "); format!("{} {{ {} }}", type_str, formatted_fields) } ExpressionKind::Index(index_expr) => { From 3e9771df61c4c759b12c735adf1b8cd02b7fdec5 Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 20:03:48 +0100 Subject: [PATCH 6/8] Fix import --- Cargo.lock | 1 + tooling/nargo_fmt/Cargo.toml | 1 + tooling/nargo_fmt/src/visitor/expr.rs | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index 6051d356fca..e31cee13364 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2388,6 +2388,7 @@ name = "nargo_fmt" version = "0.16.0" dependencies = [ "bytecount", + "iter-extended", "noirc_frontend", "serde", "similar-asserts", diff --git a/tooling/nargo_fmt/Cargo.toml b/tooling/nargo_fmt/Cargo.toml index 921c9893ab5..b33092bf868 100644 --- a/tooling/nargo_fmt/Cargo.toml +++ b/tooling/nargo_fmt/Cargo.toml @@ -10,6 +10,7 @@ bytecount = "0.6.3" noirc_frontend.workspace = true serde.workspace = true toml.workspace = true +iter-extended.workspace = true thiserror.workspace = true [dev-dependencies] diff --git a/tooling/nargo_fmt/src/visitor/expr.rs b/tooling/nargo_fmt/src/visitor/expr.rs index 757994d2f4b..706a4c2de45 100644 --- a/tooling/nargo_fmt/src/visitor/expr.rs +++ b/tooling/nargo_fmt/src/visitor/expr.rs @@ -1,10 +1,10 @@ +use iter_extended::vecmap; use noirc_frontend::{ hir::resolution::errors::Span, ArrayLiteral, BlockExpression, Expression, ExpressionKind, Literal, Statement, }; use super::FmtVisitor; -use iter_extended::vecmap; impl FmtVisitor<'_> { pub(crate) fn visit_expr(&mut self, expr: Expression) { From d2a99f24190995cbea9b30f8dcca4a791e96cd1c Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Wed, 11 Oct 2023 20:25:28 +0100 Subject: [PATCH 7/8] Fix test --- tooling/nargo_fmt/tests/expected/constructor.nr | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tooling/nargo_fmt/tests/expected/constructor.nr b/tooling/nargo_fmt/tests/expected/constructor.nr index 4d711cca549..40bafa9ed8b 100644 --- a/tooling/nargo_fmt/tests/expected/constructor.nr +++ b/tooling/nargo_fmt/tests/expected/constructor.nr @@ -1,8 +1,8 @@ struct Point { - x: i32, - y: i32, + x: u8, + y: u8, } fn foo() { - Point { x: 5, y: 10 }; + plain::Point { x: 5, y: 10 }; } From 3965beebd63a499a35a646113195765614a2b14b Mon Sep 17 00:00:00 2001 From: Jonathan Bursztyn Date: Thu, 12 Oct 2023 10:08:44 +0100 Subject: [PATCH 8/8] Remove plain:: --- compiler/noirc_frontend/src/ast/statement.rs | 8 +++++++- tooling/nargo_fmt/tests/expected/constructor.nr | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/compiler/noirc_frontend/src/ast/statement.rs b/compiler/noirc_frontend/src/ast/statement.rs index 639d4d8f763..81b84984542 100644 --- a/compiler/noirc_frontend/src/ast/statement.rs +++ b/compiler/noirc_frontend/src/ast/statement.rs @@ -161,7 +161,13 @@ impl std::hash::Hash for Ident { impl Display for Ident { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.contents.fmt(f) + let content = &self.0.contents; + let formatted_content = if content.starts_with("plain::") { + &content["plain::".len()..] + } else { + content + }; + f.write_str(formatted_content) } } diff --git a/tooling/nargo_fmt/tests/expected/constructor.nr b/tooling/nargo_fmt/tests/expected/constructor.nr index 40bafa9ed8b..38f77069a7f 100644 --- a/tooling/nargo_fmt/tests/expected/constructor.nr +++ b/tooling/nargo_fmt/tests/expected/constructor.nr @@ -4,5 +4,5 @@ struct Point { } fn foo() { - plain::Point { x: 5, y: 10 }; + Point { x: 5, y: 10 }; }