Revert "Bindgen single-field record and unions as unwrapped"

This reverts commit 4c2e6471a6.
This commit is contained in:
Richard Feldman 2022-05-13 23:09:21 -04:00
parent 4c2e6471a6
commit 3fa137d19f
No known key found for this signature in database
GPG Key ID: 7E4127D1E4241798
2 changed files with 25 additions and 30 deletions

View File

@ -79,12 +79,20 @@ pub fn add_type_help<'a>(
add_tag_union(env, opt_name, tag_vars, var, Some(*rec_var), types) add_tag_union(env, opt_name, tag_vars, var, Some(*rec_var), types)
} }
Content::Structure(FlatType::Apply(_symbol, _)) => match layout { Content::Structure(FlatType::Apply(symbol, _)) => {
Layout::Builtin(builtin) => add_builtin_type(env, builtin, var, opt_name, types), if symbol.is_builtin() {
_ => { match layout {
Layout::Builtin(builtin) => {
add_builtin_type(env, builtin, var, opt_name, types)
}
_ => {
unreachable!()
}
}
} else {
todo!("Handle non-builtin Apply") todo!("Handle non-builtin Apply")
} }
}, }
Content::Structure(FlatType::Func(_, _, _)) => { Content::Structure(FlatType::Func(_, _, _)) => {
todo!() todo!()
} }
@ -187,33 +195,13 @@ fn add_struct<I: IntoIterator<Item = (Lowercase, Variable)>>(
types: &mut Types, types: &mut Types,
) -> TypeId { ) -> TypeId {
let subs = env.subs; let subs = env.subs;
let fields_iter = &mut fields.into_iter(); let fields_iter = fields.into_iter();
let first_field = match fields_iter.next() {
Some(field) => field,
None => {
// This is an empty record; there's no more work to do!
return types.add(RocType::Struct {
name,
fields: Vec::new(),
});
}
};
let second_field = match fields_iter.next() {
Some(field) => field,
None => {
// This is a single-field record; unwrap that field.
return add_type(env, first_field.1, types);
}
};
let mut sortables = bumpalo::collections::Vec::with_capacity_in( let mut sortables = bumpalo::collections::Vec::with_capacity_in(
2 + fields_iter.size_hint().1.unwrap_or_default(), fields_iter.size_hint().1.unwrap_or_default(),
env.arena, env.arena,
); );
for (label, field_var) in std::iter::once(first_field) for (label, field_var) in fields_iter {
.chain(std::iter::once(second_field))
.chain(fields_iter)
{
sortables.push(( sortables.push((
label, label,
field_var, field_var,
@ -295,10 +283,11 @@ fn add_tag_union(
} }
1 => { 1 => {
// This is a single-tag union with 1 payload field, e.g.`[ Foo Str ]`. // This is a single-tag union with 1 payload field, e.g.`[ Foo Str ]`.
// We'll just unwrap that. // We'll just wrap that.
let var = *payload_vars.get(0).unwrap(); let var = *payload_vars.get(0).unwrap();
let content = add_type(env, var, types);
add_type(env, var, types) types.add(RocType::TransparentWrapper { name, content })
} }
_ => { _ => {
// This is a single-tag union with multiple payload field, e.g.`[ Foo Str U32 ]`. // This is a single-tag union with multiple payload field, e.g.`[ Foo Str U32 ]`.

View File

@ -497,7 +497,13 @@ fn single_tag_union_with_one_payload_field() {
generate_bindings(module) generate_bindings(module)
.strip_prefix('\n') .strip_prefix('\n')
.unwrap_or_default(), .unwrap_or_default(),
"" // This shoud get completely unwrapped into nothing indoc!(
r#"
#[derive(Clone, Debug, Default, Eq, Ord, Hash, PartialEq, PartialOrd)]
#[repr(transparent)]
pub struct UserId(roc_std::RocStr);
"#
)
); );
} }