Skip to content

Commit

Permalink
Merge branch 'master' into aztec-packages
Browse files Browse the repository at this point in the history
* master:
  fix: better formatting of leading/trailing line/block comments in expression lists (#6338)
  feat: let the formatter remove lambda block braces for single-statement blocks (#6335)
  chore: run tests in metaprogramming.rs (#6339)
  fix: formatter didn't format `>>=` well (#6337)
  chore: Update title from feedback (#6334)
  feat: Reject programs with unconditional recursion (#6292)
  fix: (formatter) indent after infix lhs (#6331)
  feat: merge and sort imports (#6322)
  fix: mutable global pattern didn't have a span (#6328)
  feat(ssa): Various mem2reg reverts to reduce memory and compilation time (#6307)
  chore: update `noir-edwards` repo to point at `noir-lang` org (#6323)
  feat: Sha256 refactoring and benchmark with longer input (#6318)
  chore: Release Noir(0.36.0) (#6213)
  chore: remove usage of slices in pedersen hash (#6295)
  chore: remove dead function (#6308)
  feat: new formatter (#6300)
  • Loading branch information
TomAFrench committed Oct 24, 2024
2 parents 13fba37 + 3299c25 commit dc39d9f
Show file tree
Hide file tree
Showing 18 changed files with 240 additions and 60 deletions.
1 change: 1 addition & 0 deletions compiler/noirc_frontend/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

mod bound_checks;
mod imports;
mod metaprogramming;
mod name_shadowing;
mod references;
mod traits;
Expand Down
26 changes: 24 additions & 2 deletions compiler/noirc_frontend/src/tests/metaprogramming.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::hir::def_collector::dc_crate::CompilationError;
use crate::hir::{
def_collector::dc_crate::CompilationError, resolution::errors::ResolverError,
type_check::TypeCheckError,
};

use super::get_program_errors;
use super::{assert_no_errors, get_program_errors};

// Regression for #5388
#[test]
Expand Down Expand Up @@ -74,3 +77,22 @@ fn unquoted_integer_as_integer_token() {

assert_no_errors(src);
}

#[test]
fn allows_references_to_structs_generated_by_macros() {
let src = r#"
comptime fn make_new_struct(_s: StructDefinition) -> Quoted {
quote { struct Bar {} }
}
#[make_new_struct]
struct Foo {}
fn main() {
let _ = Foo {};
let _ = Bar {};
}
"#;

assert_no_errors(src);
}
3 changes: 1 addition & 2 deletions docs/versioned_docs/version-v0.36.0/how_to/_category_.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"label": "Modules, Packages and Crates",
"position": 2,
"position": 1,
"collapsible": true,
"collapsed": true
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"position": 4,
"position": 0,
"collapsible": true,
"collapsed": true
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"label": "Concepts",
"position": 0,
"collapsible": true,
"collapsed": true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ fn equiv_trans<T, TU, U, UT, UV, V, VU>(
x: Equiv<T, TU, U, UT>,
y: Equiv<U, UV, V, VU>,
) -> Equiv<T, (Equiv<U, UV, V, VU>, Equiv<T, TU, U, UT>), V, (Equiv<T, TU, U, UT>, Equiv<U, UV, V, VU>)> {
Equiv { to_: |z| { y.to(x.to(z)) }, fro_: |z| { x.fro(y.fro(z)) } }
Equiv { to_: |z| y.to(x.to(z)), fro_: |z| x.fro(y.fro(z)) }
}

fn mul_one_r<let N: u32>() -> Equiv<W<N>, (), W<N>, ()> {
Equiv { to_: |_x| { W {} }, fro_: |_x| { W {} } }
Equiv { to_: |_x| W {}, fro_: |_x| W {} }
}

fn add_equiv_r<let C: u32, let N: u32, let M: u32, EN, EM>(
_: Equiv<W<N>, EN, W<M>, EM>,
) -> Equiv<W<C + N>, (), W<C + M>, ()> {
Equiv { to_: |_x| { W {} }, fro_: |_x| { W {} } }
Equiv { to_: |_x| W {}, fro_: |_x| W {} }
}

fn mul_comm<let N: u32, let M: u32>() -> Equiv<W<N * M>, (), W<M * N>, ()> {
Equiv { to_: |_x| { W {} }, fro_: |_x| { W {} } }
Equiv { to_: |_x| W {}, fro_: |_x| W {} }
}

fn mul_add<let N: u32, let M: u32, let P: u32>() -> Equiv<W<N * (M + P)>, (), W<N * M + N * P>, ()> {
Equiv { to_: |_x| { W {} }, fro_: |_x| { W {} } }
Equiv { to_: |_x| W {}, fro_: |_x| W {} }
}

// (N + 1) * N == N * N + N
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fn main() {

fn closure_test(mut x: Field) {
let one = 1;
let add1 = |z| { (|| { *z += one; })() };
let add1 = |z| (|| { *z += one; })();

let two = 2;
let add2 = |z| { *z = *z + two; };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ fn main() {
let z1 = 0;
let z2 = 1;
let cl_outer = |x| {
let cl_inner = |y| { x + y + z2 };
let cl_inner = |y| x + y + z2;
cl_inner(1) + z1
};
let result = cl_outer(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ mod bar {

// Ensure closures can still access Bar even
// though `map` separates them from `fn_attr`.
let _ = &[1, 2, 3].map(|_| { quote { Bar }.as_type() });
let _ = &[1, 2, 3].map(|_| quote { Bar }.as_type());
}

// use_callers_scope should also work nested
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ fn main(w: Field) -> pub Field {
assert(twice(|x| x * 2, 5) == 20);
assert((|x, y| x + y + 1)(2, 3) == 6);
// nested lambdas
assert((|a, b| { a + (|c| c + 2)(b) })(0, 1) == 3);
assert((|a, b| a + (|c| c + 2)(b))(0, 1) == 3);
// Closures:
let a = 42;
let g = || a;
Expand Down
70 changes: 62 additions & 8 deletions tooling/nargo_fmt/src/chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,11 @@ pub(crate) enum GroupKind {
/// fit in the current line and it's not a block, instead of splitting that expression
/// somewhere that's probably undesired, we'll "turn it" into a block
/// (write the "{" and "}" delimiters) and write the lambda body in the next line.
LambdaBody { is_block: bool },
LambdaBody {
block_statement_count: Option<usize>,
has_comments: bool,
lambda_has_return_type: bool,
},
/// A method call.
/// We track all this information to see, if we end up needing to format this call
/// in multiple lines, if we can write everything up to the left parentheses (inclusive)
Expand Down Expand Up @@ -762,14 +766,14 @@ impl<'a> Formatter<'a> {

// If a lambda body doesn't fit in the current line and it's not a block,
// we can turn it into a block and write it in the next line, so its contents fit.
if let GroupKind::LambdaBody { is_block: false } = group.kind {
if let GroupKind::LambdaBody { block_statement_count: None, .. } = group.kind {
// Try to format it again in the next line, but we don't want to recurse
// infinitely so we change the group kind.
group.kind = GroupKind::Regular;
self.write("{");
self.trim_spaces();
self.increase_indentation();
self.write_line();
self.write_line_without_skipping_whitespace_and_comments();
self.write_indentation();
self.format_chunk_group_impl(group);

Expand Down Expand Up @@ -803,7 +807,7 @@ impl<'a> Formatter<'a> {
// )
let comma_trimmed = self.trim_comma();
self.decrease_indentation();
self.write_line();
self.write_line_without_skipping_whitespace_and_comments();
self.write_indentation();
self.write("}");
if comma_trimmed {
Expand All @@ -816,6 +820,24 @@ impl<'a> Formatter<'a> {
return;
}

// At this point we determined we are going to write this group in a single line.
// If the current group is a lambda body that is a block with a single statement, like this:
//
// |x| { 1 + 2 }
//
// given that we determined the block fits the current line, if we remove the surrounding
// `{ .. }` it will still fit the current line, and reduce some noise from the code
// (this is what rustfmt seems to do too).
if let GroupKind::LambdaBody {
block_statement_count: Some(1),
has_comments: false,
lambda_has_return_type: false,
} = group.kind
{
self.format_lambda_body_removing_braces(group);
return;
}

self.format_chunk_group_in_one_line(group);
}

Expand All @@ -827,10 +849,10 @@ impl<'a> Formatter<'a> {
}
Chunk::TrailingComment(text_chunk) | Chunk::LeadingComment(text_chunk) => {
self.write(&text_chunk.string);
self.write(" ");
self.write_space_without_skipping_whitespace_and_comments();
}
Chunk::Group(chunks) => self.format_chunk_group_impl(chunks),
Chunk::SpaceOrLine => self.write(" "),
Chunk::SpaceOrLine => self.write_space_without_skipping_whitespace_and_comments(),
Chunk::IncreaseIndentation => self.increase_indentation(),
Chunk::DecreaseIndentation => self.decrease_indentation(),
Chunk::PushIndentation => self.push_indentation(),
Expand Down Expand Up @@ -893,9 +915,16 @@ impl<'a> Formatter<'a> {
self.write_indentation();
}
Chunk::LeadingComment(text_chunk) => {
let ends_with_newline = text_chunk.string.ends_with('\n');
self.write_chunk_lines(text_chunk.string.trim());
self.write_line_without_skipping_whitespace_and_comments();
self.write_indentation();

// Respect whether the leading comment had a newline before what comes next or not
if ends_with_newline {
self.write_line_without_skipping_whitespace_and_comments();
self.write_indentation();
} else {
self.write_space_without_skipping_whitespace_and_comments();
}
}
Chunk::Group(mut group) => {
if chunks.force_multiline_on_children_with_same_tag_if_multiline
Expand Down Expand Up @@ -939,6 +968,31 @@ impl<'a> Formatter<'a> {
}
}

fn format_lambda_body_removing_braces(&mut self, group: ChunkGroup) {
// Write to an intermediate string so we can remove the braces if needed.
let text_chunk = self.chunk_formatter().chunk(|formatter| {
formatter.format_chunk_group_in_one_line(group);
});
let string = text_chunk.string;

// Don't remove the braces if the lambda's body is a Semi expression.
if string.ends_with("; }") || string.ends_with("; },") {
self.write(&string);
return;
}

let string = string.strip_prefix("{ ").unwrap();

// The lambda might have a trailing comma if it's inside an arguments list
if let Some(string) = string.strip_suffix(" },") {
self.write(string);
self.write(",");
} else {
let string = string.strip_suffix(" }").unwrap();
self.write(string);
}
}

/// Appends the string to the current buffer line by line, with some pre-checks.
fn write_chunk_lines(&mut self, string: &str) {
for (index, line) in string.lines().enumerate() {
Expand Down
4 changes: 2 additions & 2 deletions tooling/nargo_fmt/src/formatter/comments_and_whitespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,8 @@ mod tests {
let src = "global x = [ /* hello */
1 , 2 ] ;";
let expected = "global x = [
/* hello */
1, 2,
/* hello */ 1,
2,
];
";
assert_format_with_max_width(src, expected, 20);
Expand Down
Loading

0 comments on commit dc39d9f

Please sign in to comment.