Unwrap the struct inside a single-tag union

This commit is contained in:
Richard Feldman 2020-07-07 22:07:34 -04:00
parent 2337d6638e
commit cdce98d14f
2 changed files with 23 additions and 11 deletions

View file

@ -525,18 +525,24 @@ pub fn build_expr<'a, 'ctx, 'env>(
field_vals.push(val); field_vals.push(val);
} }
// Create the struct_type // If the struct has only one field that isn't zero-sized,
let struct_type = ctx.struct_type(field_types.into_bump_slice(), false); // unwrap it. This is what the layout expects us to do.
let mut struct_val = struct_type.const_zero().into(); if field_vals.len() == 1 {
field_vals.pop().unwrap()
} else {
// Create the struct_type
let struct_type = ctx.struct_type(field_types.into_bump_slice(), false);
let mut struct_val = struct_type.const_zero().into();
// Insert field exprs into struct_val // Insert field exprs into struct_val
for (index, field_val) in field_vals.into_iter().enumerate() { for (index, field_val) in field_vals.into_iter().enumerate() {
struct_val = builder struct_val = builder
.build_insert_value(struct_val, field_val, index as u32, "insert_field") .build_insert_value(struct_val, field_val, index as u32, "insert_field")
.unwrap(); .unwrap();
}
BasicValueEnum::StructValue(struct_val.into_struct_value())
} }
BasicValueEnum::StructValue(struct_val.into_struct_value())
} }
Tag { Tag {
arguments, arguments,

View file

@ -547,7 +547,13 @@ pub fn layout_from_tag_union<'a>(
Unit => Layout::Struct(&[]), Unit => Layout::Struct(&[]),
BoolUnion { .. } => Layout::Builtin(Builtin::Int1), BoolUnion { .. } => Layout::Builtin(Builtin::Int1),
ByteUnion(_) => Layout::Builtin(Builtin::Int8), ByteUnion(_) => Layout::Builtin(Builtin::Int8),
Unwrapped(field_layouts) => Layout::Struct(field_layouts.into_bump_slice()), Unwrapped(mut field_layouts) => {
if field_layouts.len() == 1 {
field_layouts.pop().unwrap()
} else {
Layout::Struct(field_layouts.into_bump_slice())
}
}
Wrapped(tags) => { Wrapped(tags) => {
let mut tag_layouts = Vec::with_capacity_in(tags.len(), arena); let mut tag_layouts = Vec::with_capacity_in(tags.len(), arena);