diff --git a/compiler/noirc_evaluator/src/acir/mod.rs b/compiler/noirc_evaluator/src/acir/mod.rs index f83a2095f1..769d0d80cc 100644 --- a/compiler/noirc_evaluator/src/acir/mod.rs +++ b/compiler/noirc_evaluator/src/acir/mod.rs @@ -1880,7 +1880,7 @@ impl<'a> Context<'a> { // This conversion is for debugging support only, to allow the // debugging instrumentation code to work. Taking the reference // of a function in ACIR is useless. - let id = self.acir_context.add_constant(function_id.to_usize()); + let id = self.acir_context.add_constant(function_id.to_u32()); AcirValue::Var(id, AcirType::field()) } Value::ForeignFunction(_) => unimplemented!( diff --git a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs index 56511127da..d2bf7e5bdc 100644 --- a/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs +++ b/compiler/noirc_evaluator/src/brillig/brillig_gen/brillig_block.rs @@ -1614,7 +1614,7 @@ impl<'block> BrilligBlock<'block> { self.brillig_context.const_instruction( new_variable.extract_single_addr(), - value_id.to_usize().into(), + value_id.to_u32().into(), ); new_variable } diff --git a/compiler/noirc_evaluator/src/ssa/ir/map.rs b/compiler/noirc_evaluator/src/ssa/ir/map.rs index 23f5380f03..0fb02f19b1 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/map.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/map.rs @@ -4,7 +4,7 @@ use std::{ collections::BTreeMap, hash::Hash, str::FromStr, - sync::atomic::{AtomicUsize, Ordering}, + sync::atomic::{AtomicU32, Ordering}, }; use thiserror::Error; @@ -18,7 +18,7 @@ use thiserror::Error; /// another map where it will likely be invalid. #[derive(Serialize, Deserialize)] pub(crate) struct Id { - index: usize, + index: u32, // If we do not skip this field it will simply serialize as `"_marker":null` which is useless extra data #[serde(skip)] _marker: std::marker::PhantomData, @@ -26,14 +26,15 @@ pub(crate) struct Id { impl Id { /// Constructs a new Id for the given index. - /// This constructor is deliberately private to prevent - /// constructing invalid IDs. - pub(crate) fn new(index: usize) -> Self { + /// + /// This is private so that we can guarantee ids created from this function + /// point to valid T values in their external maps. + fn new(index: u32) -> Self { Self { index, _marker: std::marker::PhantomData } } /// Returns the underlying index of this Id. - pub(crate) fn to_usize(self) -> usize { + pub(crate) fn to_u32(self) -> u32 { self.index } @@ -43,7 +44,7 @@ impl Id { /// as unlike DenseMap::push and SparseMap::push, the Ids created /// here are likely invalid for any particularly map. #[cfg(test)] - pub(crate) fn test_new(index: usize) -> Self { + pub(crate) fn test_new(index: u32) -> Self { Self::new(index) } } @@ -187,7 +188,7 @@ impl DenseMap { /// Adds an element to the map. /// Returns the identifier/reference to that element. pub(crate) fn insert(&mut self, element: T) -> Id { - let id = Id::new(self.storage.len()); + let id = Id::new(self.storage.len().try_into().unwrap()); self.storage.push(element); id } @@ -195,7 +196,7 @@ impl DenseMap { /// Given the Id of the element being created, adds the element /// returned by the given function to the map pub(crate) fn insert_with_id(&mut self, f: impl FnOnce(Id) -> T) -> Id { - let id = Id::new(self.storage.len()); + let id = Id::new(self.storage.len().try_into().unwrap()); self.storage.push(f(id)); id } @@ -204,7 +205,7 @@ impl DenseMap { /// /// The id-element pairs are ordered by the numeric values of the ids. pub(crate) fn iter(&self) -> impl ExactSizeIterator, &T)> { - let ids_iter = (0..self.storage.len()).map(|idx| Id::new(idx)); + let ids_iter = (0..self.storage.len() as u32).map(|idx| Id::new(idx)); ids_iter.zip(self.storage.iter()) } } @@ -219,13 +220,13 @@ impl std::ops::Index> for DenseMap { type Output = T; fn index(&self, id: Id) -> &Self::Output { - &self.storage[id.index] + &self.storage[id.index as usize] } } impl std::ops::IndexMut> for DenseMap { fn index_mut(&mut self, id: Id) -> &mut Self::Output { - &mut self.storage[id.index] + &mut self.storage[id.index as usize] } } @@ -253,7 +254,7 @@ impl SparseMap { /// Adds an element to the map. /// Returns the identifier/reference to that element. pub(crate) fn insert(&mut self, element: T) -> Id { - let id = Id::new(self.storage.len()); + let id = Id::new(self.storage.len().try_into().unwrap()); self.storage.insert(id, element); id } @@ -261,7 +262,7 @@ impl SparseMap { /// Given the Id of the element being created, adds the element /// returned by the given function to the map pub(crate) fn insert_with_id(&mut self, f: impl FnOnce(Id) -> T) -> Id { - let id = Id::new(self.storage.len()); + let id = Id::new(self.storage.len().try_into().unwrap()); self.storage.insert(id, f(id)); id } @@ -365,7 +366,7 @@ impl std::ops::Index<&K> for TwoWayMap { /// This type wraps an AtomicUsize so it can safely be used across threads. #[derive(Debug, Serialize, Deserialize)] pub(crate) struct AtomicCounter { - next: AtomicUsize, + next: AtomicU32, _marker: std::marker::PhantomData, } @@ -373,7 +374,7 @@ impl AtomicCounter { /// Create a new counter starting after the given Id. /// Use AtomicCounter::default() to start at zero. pub(crate) fn starting_after(id: Id) -> Self { - Self { next: AtomicUsize::new(id.index + 1), _marker: Default::default() } + Self { next: AtomicU32::new(id.index + 1), _marker: Default::default() } } /// Return the next fresh id diff --git a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs index ded1f52d52..7d7798fd30 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/defunctionalize.rs @@ -268,7 +268,7 @@ fn create_apply_functions( } fn function_id_to_field(function_id: FunctionId) -> FieldElement { - (function_id.to_usize() as u128).into() + (function_id.to_u32() as u128).into() } /// Creates an apply function for the given signature and variants diff --git a/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs b/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs index 142447c83a..7ef793a350 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/unrolling.rs @@ -1146,7 +1146,7 @@ mod tests { let refs = loop0.find_pre_header_reference_values(function, &loops.cfg).unwrap(); assert_eq!(refs.len(), 1); - assert!(refs.contains(&ValueId::new(2))); + assert!(refs.contains(&ValueId::test_new(2))); let (loads, stores) = loop0.count_loads_and_stores(function, &refs); assert_eq!(loads, 1); diff --git a/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs b/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs index b0003aa5f0..7c7e977c6c 100644 --- a/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs +++ b/compiler/noirc_evaluator/src/ssa/parser/into_ssa.rs @@ -57,7 +57,7 @@ impl Translator { // A FunctionBuilder must be created with a main Function, so here wer remove it // from the parsed SSA to avoid adding it twice later on. let main_function = parsed_ssa.functions.remove(0); - let main_id = FunctionId::new(0); + let main_id = FunctionId::test_new(0); let mut builder = FunctionBuilder::new(main_function.external_name.clone(), main_id); builder.set_runtime(main_function.runtime_type); @@ -65,7 +65,7 @@ impl Translator { let mut function_id_counter = 1; let mut functions = HashMap::new(); for function in &parsed_ssa.functions { - let function_id = FunctionId::new(function_id_counter); + let function_id = FunctionId::test_new(function_id_counter); function_id_counter += 1; functions.insert(function.internal_name.clone(), function_id);