Skip to content

Commit d74d0fc

Browse files
AztecBotTomAFrench
andauthored
feat: Sync from noir (#10710)
Automated pull of development from the [noir](https://github.com/noir-lang/noir) programming language, a dependency of Aztec. BEGIN_COMMIT_OVERRIDE feat: add `nargo test --format json` (noir-lang/noir#6796) chore: Change Id to use a u32 (noir-lang/noir#6807) END_COMMIT_OVERRIDE --------- Co-authored-by: TomAFrench <tom@tomfren.ch>
1 parent 9c9a2dc commit d74d0fc

File tree

11 files changed

+287
-71
lines changed

11 files changed

+287
-71
lines changed

.noir-sync-commit

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
b88db67a4fa92f861329105fb732a7b1309620fe
1+
eb975ab28fb056cf92859377c02f2bb1a608eda3

noir/noir-repo/compiler/fm/src/file_map.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,19 @@ impl FileMap {
8080
pub fn all_file_ids(&self) -> impl Iterator<Item = &FileId> {
8181
self.name_to_id.values()
8282
}
83+
84+
pub fn get_name(&self, file_id: FileId) -> Result<PathString, Error> {
85+
let name = self.files.get(file_id.as_usize())?.name().clone();
86+
87+
// See if we can make the file name a bit shorter/easier to read if it starts with the current directory
88+
if let Some(current_dir) = &self.current_dir {
89+
if let Ok(name_without_prefix) = name.0.strip_prefix(current_dir) {
90+
return Ok(PathString::from_path(name_without_prefix.to_path_buf()));
91+
}
92+
}
93+
94+
Ok(name)
95+
}
8396
}
8497
impl Default for FileMap {
8598
fn default() -> Self {
@@ -97,16 +110,7 @@ impl<'a> Files<'a> for FileMap {
97110
type Source = &'a str;
98111

99112
fn name(&self, file_id: Self::FileId) -> Result<Self::Name, Error> {
100-
let name = self.files.get(file_id.as_usize())?.name().clone();
101-
102-
// See if we can make the file name a bit shorter/easier to read if it starts with the current directory
103-
if let Some(current_dir) = &self.current_dir {
104-
if let Ok(name_without_prefix) = name.0.strip_prefix(current_dir) {
105-
return Ok(PathString::from_path(name_without_prefix.to_path_buf()));
106-
}
107-
}
108-
109-
Ok(name)
113+
self.get_name(file_id)
110114
}
111115

112116
fn source(&'a self, file_id: Self::FileId) -> Result<Self::Source, Error> {

noir/noir-repo/compiler/noirc_errors/src/reporter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ fn convert_diagnostic(
272272
diagnostic.with_message(&cd.message).with_labels(secondary_labels).with_notes(notes)
273273
}
274274

275-
fn stack_trace<'files>(
275+
pub fn stack_trace<'files>(
276276
files: &'files impl Files<'files, FileId = fm::FileId>,
277277
call_stack: &[Location],
278278
) -> String {

noir/noir-repo/compiler/noirc_evaluator/src/acir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1880,7 +1880,7 @@ impl<'a> Context<'a> {
18801880
// This conversion is for debugging support only, to allow the
18811881
// debugging instrumentation code to work. Taking the reference
18821882
// of a function in ACIR is useless.
1883-
let id = self.acir_context.add_constant(function_id.to_usize());
1883+
let id = self.acir_context.add_constant(function_id.to_u32());
18841884
AcirValue::Var(id, AcirType::field())
18851885
}
18861886
Value::ForeignFunction(_) => unimplemented!(

noir/noir-repo/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1614,7 +1614,7 @@ impl<'block> BrilligBlock<'block> {
16141614

16151615
self.brillig_context.const_instruction(
16161616
new_variable.extract_single_addr(),
1617-
value_id.to_usize().into(),
1617+
value_id.to_u32().into(),
16181618
);
16191619
new_variable
16201620
}

noir/noir-repo/compiler/noirc_evaluator/src/ssa/ir/map.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::{
44
collections::BTreeMap,
55
hash::Hash,
66
str::FromStr,
7-
sync::atomic::{AtomicUsize, Ordering},
7+
sync::atomic::{AtomicU32, Ordering},
88
};
99
use thiserror::Error;
1010

@@ -18,22 +18,23 @@ use thiserror::Error;
1818
/// another map where it will likely be invalid.
1919
#[derive(Serialize, Deserialize)]
2020
pub(crate) struct Id<T> {
21-
index: usize,
21+
index: u32,
2222
// If we do not skip this field it will simply serialize as `"_marker":null` which is useless extra data
2323
#[serde(skip)]
2424
_marker: std::marker::PhantomData<T>,
2525
}
2626

2727
impl<T> Id<T> {
2828
/// Constructs a new Id for the given index.
29-
/// This constructor is deliberately private to prevent
30-
/// constructing invalid IDs.
31-
pub(crate) fn new(index: usize) -> Self {
29+
///
30+
/// This is private so that we can guarantee ids created from this function
31+
/// point to valid T values in their external maps.
32+
fn new(index: u32) -> Self {
3233
Self { index, _marker: std::marker::PhantomData }
3334
}
3435

3536
/// Returns the underlying index of this Id.
36-
pub(crate) fn to_usize(self) -> usize {
37+
pub(crate) fn to_u32(self) -> u32 {
3738
self.index
3839
}
3940

@@ -43,7 +44,7 @@ impl<T> Id<T> {
4344
/// as unlike DenseMap::push and SparseMap::push, the Ids created
4445
/// here are likely invalid for any particularly map.
4546
#[cfg(test)]
46-
pub(crate) fn test_new(index: usize) -> Self {
47+
pub(crate) fn test_new(index: u32) -> Self {
4748
Self::new(index)
4849
}
4950
}
@@ -187,15 +188,15 @@ impl<T> DenseMap<T> {
187188
/// Adds an element to the map.
188189
/// Returns the identifier/reference to that element.
189190
pub(crate) fn insert(&mut self, element: T) -> Id<T> {
190-
let id = Id::new(self.storage.len());
191+
let id = Id::new(self.storage.len().try_into().unwrap());
191192
self.storage.push(element);
192193
id
193194
}
194195

195196
/// Given the Id of the element being created, adds the element
196197
/// returned by the given function to the map
197198
pub(crate) fn insert_with_id(&mut self, f: impl FnOnce(Id<T>) -> T) -> Id<T> {
198-
let id = Id::new(self.storage.len());
199+
let id = Id::new(self.storage.len().try_into().unwrap());
199200
self.storage.push(f(id));
200201
id
201202
}
@@ -204,7 +205,7 @@ impl<T> DenseMap<T> {
204205
///
205206
/// The id-element pairs are ordered by the numeric values of the ids.
206207
pub(crate) fn iter(&self) -> impl ExactSizeIterator<Item = (Id<T>, &T)> {
207-
let ids_iter = (0..self.storage.len()).map(|idx| Id::new(idx));
208+
let ids_iter = (0..self.storage.len() as u32).map(|idx| Id::new(idx));
208209
ids_iter.zip(self.storage.iter())
209210
}
210211
}
@@ -219,13 +220,13 @@ impl<T> std::ops::Index<Id<T>> for DenseMap<T> {
219220
type Output = T;
220221

221222
fn index(&self, id: Id<T>) -> &Self::Output {
222-
&self.storage[id.index]
223+
&self.storage[id.index as usize]
223224
}
224225
}
225226

226227
impl<T> std::ops::IndexMut<Id<T>> for DenseMap<T> {
227228
fn index_mut(&mut self, id: Id<T>) -> &mut Self::Output {
228-
&mut self.storage[id.index]
229+
&mut self.storage[id.index as usize]
229230
}
230231
}
231232

@@ -253,15 +254,15 @@ impl<T> SparseMap<T> {
253254
/// Adds an element to the map.
254255
/// Returns the identifier/reference to that element.
255256
pub(crate) fn insert(&mut self, element: T) -> Id<T> {
256-
let id = Id::new(self.storage.len());
257+
let id = Id::new(self.storage.len().try_into().unwrap());
257258
self.storage.insert(id, element);
258259
id
259260
}
260261

261262
/// Given the Id of the element being created, adds the element
262263
/// returned by the given function to the map
263264
pub(crate) fn insert_with_id(&mut self, f: impl FnOnce(Id<T>) -> T) -> Id<T> {
264-
let id = Id::new(self.storage.len());
265+
let id = Id::new(self.storage.len().try_into().unwrap());
265266
self.storage.insert(id, f(id));
266267
id
267268
}
@@ -365,15 +366,15 @@ impl<K: Eq + Hash, V> std::ops::Index<&K> for TwoWayMap<K, V> {
365366
/// This type wraps an AtomicUsize so it can safely be used across threads.
366367
#[derive(Debug, Serialize, Deserialize)]
367368
pub(crate) struct AtomicCounter<T> {
368-
next: AtomicUsize,
369+
next: AtomicU32,
369370
_marker: std::marker::PhantomData<T>,
370371
}
371372

372373
impl<T> AtomicCounter<T> {
373374
/// Create a new counter starting after the given Id.
374375
/// Use AtomicCounter::default() to start at zero.
375376
pub(crate) fn starting_after(id: Id<T>) -> Self {
376-
Self { next: AtomicUsize::new(id.index + 1), _marker: Default::default() }
377+
Self { next: AtomicU32::new(id.index + 1), _marker: Default::default() }
377378
}
378379

379380
/// Return the next fresh id

noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ fn create_apply_functions(
268268
}
269269

270270
fn function_id_to_field(function_id: FunctionId) -> FieldElement {
271-
(function_id.to_usize() as u128).into()
271+
(function_id.to_u32() as u128).into()
272272
}
273273

274274
/// Creates an apply function for the given signature and variants

noir/noir-repo/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1146,7 +1146,7 @@ mod tests {
11461146

11471147
let refs = loop0.find_pre_header_reference_values(function, &loops.cfg).unwrap();
11481148
assert_eq!(refs.len(), 1);
1149-
assert!(refs.contains(&ValueId::new(2)));
1149+
assert!(refs.contains(&ValueId::test_new(2)));
11501150

11511151
let (loads, stores) = loop0.count_loads_and_stores(function, &refs);
11521152
assert_eq!(loads, 1);

noir/noir-repo/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ impl Translator {
5757
// A FunctionBuilder must be created with a main Function, so here wer remove it
5858
// from the parsed SSA to avoid adding it twice later on.
5959
let main_function = parsed_ssa.functions.remove(0);
60-
let main_id = FunctionId::new(0);
60+
let main_id = FunctionId::test_new(0);
6161
let mut builder = FunctionBuilder::new(main_function.external_name.clone(), main_id);
6262
builder.set_runtime(main_function.runtime_type);
6363

6464
// Map function names to their IDs so calls can be resolved
6565
let mut function_id_counter = 1;
6666
let mut functions = HashMap::new();
6767
for function in &parsed_ssa.functions {
68-
let function_id = FunctionId::new(function_id_counter);
68+
let function_id = FunctionId::test_new(function_id_counter);
6969
function_id_counter += 1;
7070

7171
functions.insert(function.internal_name.clone(), function_id);

noir/noir-repo/tooling/nargo_cli/src/cli/test_cmd.rs

+27-31
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use acvm::{BlackBoxFunctionSolver, FieldElement};
1212
use bn254_blackbox_solver::Bn254BlackBoxSolver;
1313
use clap::Args;
1414
use fm::FileManager;
15-
use formatters::{Formatter, PrettyFormatter, TerseFormatter};
15+
use formatters::{Formatter, JsonFormatter, PrettyFormatter, TerseFormatter};
1616
use nargo::{
1717
insert_all_files_for_workspace_into_file_manager, ops::TestStatus, package::Package, parse_all,
1818
prepare_package, workspace::Workspace, PrintOutput,
@@ -71,13 +71,16 @@ enum Format {
7171
Pretty,
7272
/// Display one character per test
7373
Terse,
74+
/// Output a JSON Lines document
75+
Json,
7476
}
7577

7678
impl Format {
7779
fn formatter(&self) -> Box<dyn Formatter> {
7880
match self {
7981
Format::Pretty => Box::new(PrettyFormatter),
8082
Format::Terse => Box::new(TerseFormatter),
83+
Format::Json => Box::new(JsonFormatter),
8184
}
8285
}
8386
}
@@ -87,6 +90,7 @@ impl Display for Format {
8790
match self {
8891
Format::Pretty => write!(f, "pretty"),
8992
Format::Terse => write!(f, "terse"),
93+
Format::Json => write!(f, "json"),
9094
}
9195
}
9296
}
@@ -211,6 +215,12 @@ impl<'a> TestRunner<'a> {
211215
) -> bool {
212216
let mut all_passed = true;
213217

218+
for (package_name, total_test_count) in test_count_per_package {
219+
self.formatter
220+
.package_start_async(package_name, *total_test_count)
221+
.expect("Could not display package start");
222+
}
223+
214224
let (sender, receiver) = mpsc::channel();
215225
let iter = &Mutex::new(tests.into_iter());
216226
thread::scope(|scope| {
@@ -228,6 +238,10 @@ impl<'a> TestRunner<'a> {
228238
break;
229239
};
230240

241+
self.formatter
242+
.test_start_async(&test.name, &test.package_name)
243+
.expect("Could not display test start");
244+
231245
let time_before_test = std::time::Instant::now();
232246
let (status, output) = match catch_unwind(test.runner) {
233247
Ok((status, output)) => (status, output),
@@ -255,6 +269,16 @@ impl<'a> TestRunner<'a> {
255269
time_to_run,
256270
};
257271

272+
self.formatter
273+
.test_end_async(
274+
&test_result,
275+
self.file_manager,
276+
self.args.show_output,
277+
self.args.compile_options.deny_warnings,
278+
self.args.compile_options.silence_warnings,
279+
)
280+
.expect("Could not display test start");
281+
258282
if thread_sender.send(test_result).is_err() {
259283
break;
260284
}
@@ -275,7 +299,7 @@ impl<'a> TestRunner<'a> {
275299
let total_test_count = *total_test_count;
276300

277301
self.formatter
278-
.package_start(package_name, total_test_count)
302+
.package_start_sync(package_name, total_test_count)
279303
.expect("Could not display package start");
280304

281305
// Check if we have buffered test results for this package
@@ -485,7 +509,7 @@ impl<'a> TestRunner<'a> {
485509
current_test_count: usize,
486510
total_test_count: usize,
487511
) -> std::io::Result<()> {
488-
self.formatter.test_end(
512+
self.formatter.test_end_sync(
489513
test_result,
490514
current_test_count,
491515
total_test_count,
@@ -496,31 +520,3 @@ impl<'a> TestRunner<'a> {
496520
)
497521
}
498522
}
499-
500-
#[cfg(test)]
501-
mod tests {
502-
use std::io::Write;
503-
use std::{thread, time::Duration};
504-
use termcolor::{ColorChoice, StandardStream};
505-
506-
#[test]
507-
fn test_stderr_lock() {
508-
for i in 0..4 {
509-
thread::spawn(move || {
510-
let mut writer = StandardStream::stderr(ColorChoice::Always);
511-
//let mut writer = writer.lock();
512-
513-
let mut show = |msg| {
514-
thread::sleep(Duration::from_millis(10));
515-
//println!("{i} {msg}");
516-
writeln!(writer, "{i} {msg}").unwrap();
517-
};
518-
519-
show("a");
520-
show("b");
521-
show("c");
522-
});
523-
}
524-
thread::sleep(Duration::from_millis(100));
525-
}
526-
}

0 commit comments

Comments
 (0)