Give non-recursive single tag unions a payload

This commit is contained in:
Richard Feldman 2022-08-07 16:51:52 -04:00
parent 3adae16cff
commit 23b8ff896a
No known key found for this signature in database
GPG key ID: 7E4127D1E4241798
2 changed files with 18 additions and 15 deletions

View file

@ -248,7 +248,11 @@ fn add_type(target_info: TargetInfo, id: TypeId, types: &Types, impls: &mut Impl
RocTagUnion::NonNullableUnwrapped { .. } => {
todo!();
}
RocTagUnion::SingleTagUnion { name, tag_name } => {
RocTagUnion::NonRecursiveSingleTag {
name,
tag_name,
payload: None,
} => {
// An enumeration with one tag is a zero-sized unit type, so
// represent it as a zero-sized struct (e.g. "struct Foo()").
let derive = derive_str(
@ -1407,7 +1411,7 @@ fn type_name(id: TypeId, types: &Types) -> String {
RocType::Struct { name, .. }
| RocType::TagUnionPayload { name, .. }
| RocType::TagUnion(RocTagUnion::NonRecursive { name, .. })
| RocType::TagUnion(RocTagUnion::SingleTagUnion { name, .. })
| RocType::TagUnion(RocTagUnion::NonRecursiveSingleTag { name, .. })
| RocType::TagUnion(RocTagUnion::Recursive { name, .. })
| RocType::TagUnion(RocTagUnion::Enumeration { name, .. })
| RocType::TagUnion(RocTagUnion::NullableWrapped { name, .. })
@ -2030,7 +2034,7 @@ fn cannot_derive_copy(roc_type: &RocType, types: &Types) -> bool {
| RocType::Bool
| RocType::Num(_)
| RocType::TagUnion(RocTagUnion::Enumeration { .. })
| RocType::TagUnion(RocTagUnion::SingleTagUnion { .. })
| RocType::TagUnion(RocTagUnion::NonRecursiveSingleTag { .. })
| RocType::Function { .. } => false,
RocType::RocStr
| RocType::RocList(_)
@ -2082,7 +2086,7 @@ fn has_float_help(roc_type: &RocType, types: &Types, do_not_recurse: &[TypeId])
| RocType::RocStr
| RocType::Bool
| RocType::TagUnion(RocTagUnion::Enumeration { .. })
| RocType::TagUnion(RocTagUnion::SingleTagUnion { .. })
| RocType::TagUnion(RocTagUnion::NonRecursiveSingleTag { .. })
| RocType::Function { .. } => false,
RocType::RocList(id) | RocType::RocSet(id) | RocType::RocBox(id) => {
has_float_help(types.get_type(*id), types, do_not_recurse)

View file

@ -86,10 +86,10 @@ impl Types {
match (union_a, union_b) {
(
SingleTagUnion {
NonRecursiveSingleTag {
tag_name: tag_a, ..
},
SingleTagUnion {
NonRecursiveSingleTag {
tag_name: tag_b, ..
},
) => tag_a == tag_b,
@ -198,8 +198,8 @@ impl Types {
}
// These are all listed explicitly so that if we ever add a new variant,
// we'll get an exhaustiveness error here.
(SingleTagUnion { .. }, _)
| (_, SingleTagUnion { .. })
(NonRecursiveSingleTag { .. }, _)
| (_, NonRecursiveSingleTag { .. })
| (Enumeration { .. }, _)
| (_, Enumeration { .. })
| (NonRecursive { .. }, _)
@ -516,9 +516,10 @@ pub enum RocTagUnion {
tags: Vec<String>,
size: u32,
},
SingleTagUnion {
NonRecursiveSingleTag {
name: String,
tag_name: String,
payload: Option<TypeId>,
},
/// A non-recursive tag union
/// e.g. `Result a e : [Ok a, Err e]`
@ -539,10 +540,7 @@ pub enum RocTagUnion {
/// A recursive tag union with just one constructor
/// Optimization: No need to store a tag ID (the payload is "unwrapped")
/// e.g. `RoseTree a : [Tree a (List (RoseTree a))]`
NonNullableUnwrapped {
name: String,
content: TypeId,
},
NonNullableUnwrapped { name: String, content: TypeId },
/// A recursive tag union that has an empty variant
/// Optimization: Represent the empty variant as null pointer => no memory usage & fast comparison
@ -1137,11 +1135,12 @@ fn add_tag_union<'a>(
// This should be a single-tag union.
debug_assert_eq!(tags.len(), 1);
let (tag_name, _) = tags.pop().unwrap();
let (tag_name, payload) = tags.pop().unwrap();
RocTagUnion::SingleTagUnion {
RocTagUnion::NonRecursiveSingleTag {
name: name.clone(),
tag_name,
payload,
}
}
Layout::Builtin(_)