diff --git a/compiler/types/src/pretty_print.rs b/compiler/types/src/pretty_print.rs index 4e4a11fc3c..903ff33ea9 100644 --- a/compiler/types/src/pretty_print.rs +++ b/compiler/types/src/pretty_print.rs @@ -172,7 +172,7 @@ fn find_names_needed( find_names_needed(*ext_var, subs, roots, root_appearances, names_taken); } Structure(TagUnion(tags, ext_var)) => { - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index]; @@ -186,7 +186,7 @@ fn find_names_needed( find_names_needed(*ext_var, subs, roots, root_appearances, names_taken); } Structure(RecursiveTagUnion(rec_var, tags, ext_var)) => { - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index]; diff --git a/compiler/types/src/subs.rs b/compiler/types/src/subs.rs index 278b1010ee..a000926ceb 100644 --- a/compiler/types/src/subs.rs +++ b/compiler/types/src/subs.rs @@ -11,7 +11,7 @@ use ven_ena::unify::{InPlace, Snapshot, UnificationTable, UnifyKey}; // if it went up, maybe check that the change is really required static_assertions::assert_eq_size!([u8; 56], Descriptor); static_assertions::assert_eq_size!([u8; 40], Content); -static_assertions::assert_eq_size!([u8; 32], FlatType); +static_assertions::assert_eq_size!([u8; 24], FlatType); static_assertions::assert_eq_size!([u8; 48], Problem); #[derive(Clone, Copy, Hash, PartialEq, Eq)] @@ -230,7 +230,7 @@ impl SubsSlice { &mut slice[self.start as usize..][..self.length as usize] } - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.length as usize } @@ -238,7 +238,7 @@ impl SubsSlice { self.len() == 0 } - pub fn new(start: u32, length: u16) -> Self { + pub const fn new(start: u32, length: u16) -> Self { Self { start, length, @@ -948,19 +948,39 @@ impl IntoIterator for VariableSubsSlice { #[derive(Clone, Copy, Debug, Default)] pub struct UnionTags { - pub tag_names: SubsSlice, - pub variables: SubsSlice, + length: u16, + tag_names_start: u32, + variables_start: u32, } impl UnionTags { pub fn from_tag_name_index(index: SubsIndex) -> Self { + Self::from_slices( + SubsSlice::new(index.start, 1), + SubsSlice::new(0, 1), // the first variablesubsslice is the empty slice + ) + } + + fn from_slices(tag_names: SubsSlice, variables: SubsSlice) -> Self { + debug_assert_eq!(tag_names.len(), variables.len()); + Self { - tag_names: SubsSlice::new(index.start, 1), - variables: SubsSlice::new(0, 1), // the first variablesubsslice is the empty slice + length: tag_names.len() as u16, + tag_names_start: tag_names.start, + variables_start: variables.start, } } - pub fn len(&self) -> usize { - self.tag_names.len() + + const fn tag_names(&self) -> SubsSlice { + SubsSlice::new(self.tag_names_start, self.length) + } + + pub const fn variables(&self) -> SubsSlice { + SubsSlice::new(self.variables_start, self.length) + } + + pub const fn len(&self) -> usize { + self.length as usize } pub fn is_empty(&self) -> bool { @@ -994,10 +1014,10 @@ impl UnionTags { length += 1; } - UnionTags { - variables: SubsSlice::new(variables_start, length), - tag_names: SubsSlice::new(tag_names_start, length), - } + Self::from_slices( + SubsSlice::new(tag_names_start, length), + SubsSlice::new(variables_start, length), + ) } pub fn insert_slices_into_subs(subs: &mut Subs, input: I) -> Self @@ -1021,16 +1041,19 @@ impl UnionTags { length += 1; } - UnionTags { - variables: SubsSlice::new(variables_start, length), - tag_names: SubsSlice::new(tag_names_start, length), + Self { + length, + tag_names_start, + variables_start, } } pub fn iter_all( &self, ) -> impl Iterator, SubsIndex)> { - self.tag_names.into_iter().zip(self.variables.into_iter()) + self.tag_names() + .into_iter() + .zip(self.variables().into_iter()) } #[inline(always)] @@ -1162,7 +1185,7 @@ fn first(x: &(K, V), y: &(K, V)) -> std::cmp::Ordering { pub type SortedIterator<'a> = Box)> + 'a>; impl RecordFields { - pub fn len(&self) -> usize { + pub const fn len(&self) -> usize { self.length as usize } @@ -1371,7 +1394,7 @@ fn occurs( short_circuit(subs, root_var, &new_seen, it) } TagUnion(tags, ext_var) => { - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index]; @@ -1387,7 +1410,7 @@ fn occurs( } RecursiveTagUnion(_rec_var, tags, ext_var) => { // TODO rec_var is excluded here, verify that this is correct - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index]; @@ -1492,7 +1515,7 @@ fn explicit_substitute( let new_ext_var = explicit_substitute(subs, from, to, ext_var, seen); let mut new_slices = Vec::new(); - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; let mut new_variables = Vec::new(); @@ -1511,12 +1534,13 @@ fn explicit_substitute( } let start = subs.variable_slices.len() as u32; - let length = new_slices.len() as u16; + let length = new_slices.len(); subs.variable_slices.extend(new_slices); let mut union_tags = tags; - union_tags.variables = SubsSlice::new(start, length); + debug_assert_eq!(length, union_tags.len()); + union_tags.variables_start = start; subs.set_content(in_var, Structure(TagUnion(union_tags, new_ext_var))); } @@ -1532,7 +1556,7 @@ fn explicit_substitute( let new_ext_var = explicit_substitute(subs, from, to, ext_var, seen); let mut new_slices = Vec::new(); - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; let mut new_variables = Vec::new(); @@ -1551,12 +1575,13 @@ fn explicit_substitute( } let start = subs.variable_slices.len() as u32; - let length = new_slices.len() as u16; + let length = new_slices.len(); subs.variable_slices.extend(new_slices); let mut union_tags = tags; - union_tags.variables = SubsSlice::new(start, length); + debug_assert_eq!(length, union_tags.len()); + union_tags.variables_start = start; subs.set_content( in_var, @@ -1679,7 +1704,7 @@ fn get_var_names( FlatType::TagUnion(tags, ext_var) => { let mut taken_names = get_var_names(subs, ext_var, taken_names); - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index]; @@ -1698,7 +1723,7 @@ fn get_var_names( let taken_names = get_var_names(subs, ext_var, taken_names); let mut taken_names = get_var_names(subs, rec_var, taken_names); - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let arg_var = subs[var_index]; @@ -2084,7 +2109,7 @@ fn restore_content(subs: &mut Subs, content: &Content) { subs.restore(*ext_var); } TagUnion(tags, ext_var) => { - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index]; @@ -2099,7 +2124,7 @@ fn restore_content(subs: &mut Subs, content: &Content) { } RecursiveTagUnion(rec_var, tags, ext_var) => { - for slice_index in tags.variables { + for slice_index in tags.variables() { let slice = subs[slice_index]; for var_index in slice { let var = subs[var_index];