Merge pull request #4209 from roc-lang/impl-tag-discriminant

Derive `Hash` implementations for tag unions
This commit is contained in:
Ayaz 2022-10-10 22:23:05 -05:00 committed by GitHub
commit 83b64c4fb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 844 additions and 106 deletions

View file

@ -879,15 +879,8 @@ impl<'a> UnionLayout<'a> {
}
}
pub fn tag_id_layout(&self) -> Layout<'a> {
// TODO is it beneficial to return a more specific layout?
// e.g. Layout::bool() and Layout::VOID
match self.discriminant() {
Discriminant::U0 => Layout::u8(),
Discriminant::U1 => Layout::u8(),
Discriminant::U8 => Layout::u8(),
Discriminant::U16 => Layout::u16(),
}
pub fn tag_id_layout(&self) -> Layout<'static> {
self.discriminant().layout()
}
fn stores_tag_id_in_pointer_bits(tags: &[&[Layout<'a>]], target_info: TargetInfo) -> bool {
@ -1138,6 +1131,17 @@ impl Discriminant {
pub const fn alignment_bytes(&self) -> u32 {
self.stack_size()
}
pub const fn layout(&self) -> Layout<'static> {
// TODO is it beneficial to return a more specific layout?
// e.g. Layout::bool() and Layout::VOID
match self {
Discriminant::U0 => Layout::u8(),
Discriminant::U1 => Layout::u8(),
Discriminant::U8 => Layout::u8(),
Discriminant::U16 => Layout::u16(),
}
}
}
/// Custom type so we can get the numeric representation of a symbol in tests (so `#UserApp.3`
@ -2713,7 +2717,7 @@ impl<'a> Layout<'a> {
Layout::Builtin(Builtin::Int(IntWidth::U8))
}
pub fn u16() -> Layout<'a> {
pub const fn u16() -> Layout<'a> {
Layout::Builtin(Builtin::Int(IntWidth::U16))
}