mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-29 23:04:49 +00:00
use union layout in mono patterns
This commit is contained in:
parent
08cb889e6e
commit
9ba4885083
2 changed files with 68 additions and 68 deletions
|
@ -220,8 +220,7 @@ fn flatten<'a>(
|
|||
} if union.alternatives.len() == 1
|
||||
&& !matches!(
|
||||
layout,
|
||||
Layout::Union(UnionLayout::NullableWrapped { .. })
|
||||
| Layout::Union(UnionLayout::NullableUnwrapped { .. })
|
||||
UnionLayout::NullableWrapped { .. } | UnionLayout::NullableUnwrapped { .. }
|
||||
) =>
|
||||
{
|
||||
// TODO ^ do we need to check that guard.is_none() here?
|
||||
|
@ -581,12 +580,9 @@ fn to_relevant_branch_help<'a>(
|
|||
debug_assert_eq!(tag_id, *test_id);
|
||||
|
||||
// the test matches the constructor of this pattern
|
||||
|
||||
match Wrapped::opt_from_layout(&layout) {
|
||||
None => todo!(),
|
||||
Some(wrapped) => {
|
||||
match wrapped {
|
||||
Wrapped::SingleElementRecord => {
|
||||
match layout {
|
||||
UnionLayout::NonRecursive([[Layout::Struct([_])]]) => {
|
||||
// a one-element record equivalent
|
||||
// Theory: Unbox doesn't have any value for us
|
||||
debug_assert_eq!(arguments.len(), 1);
|
||||
let arg = arguments[0].clone();
|
||||
|
@ -597,35 +593,41 @@ fn to_relevant_branch_help<'a>(
|
|||
start.extend(end);
|
||||
}
|
||||
}
|
||||
Wrapped::RecordOrSingleTagUnion | Wrapped::LikeARoseTree => {
|
||||
let sub_positions = arguments.into_iter().enumerate().map(
|
||||
|(index, (pattern, _))| {
|
||||
UnionLayout::NonRecursive([_]) | UnionLayout::NonNullableUnwrapped(_) => {
|
||||
let sub_positions =
|
||||
arguments
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, (pattern, _))| {
|
||||
let mut new_path = path.to_vec();
|
||||
new_path.push(PathInstruction {
|
||||
index: index as u64,
|
||||
tag_id,
|
||||
});
|
||||
(new_path, Guard::NoGuard, pattern)
|
||||
},
|
||||
);
|
||||
});
|
||||
start.extend(sub_positions);
|
||||
start.extend(end);
|
||||
}
|
||||
Wrapped::MultiTagUnion => {
|
||||
let sub_positions = arguments.into_iter().enumerate().map(
|
||||
|(index, (pattern, _))| {
|
||||
UnionLayout::NonRecursive(_)
|
||||
| UnionLayout::Recursive(_)
|
||||
| UnionLayout::NullableWrapped { .. }
|
||||
| UnionLayout::NullableUnwrapped { .. } => {
|
||||
let sub_positions =
|
||||
arguments
|
||||
.into_iter()
|
||||
.enumerate()
|
||||
.map(|(index, (pattern, _))| {
|
||||
let mut new_path = path.to_vec();
|
||||
new_path.push(PathInstruction {
|
||||
index: 1 + index as u64,
|
||||
tag_id,
|
||||
});
|
||||
(new_path, Guard::NoGuard, pattern)
|
||||
},
|
||||
);
|
||||
});
|
||||
start.extend(sub_positions);
|
||||
start.extend(end);
|
||||
}
|
||||
Wrapped::EmptyRecord => todo!(),
|
||||
}
|
||||
|
||||
Some(Branch {
|
||||
|
@ -633,8 +635,6 @@ fn to_relevant_branch_help<'a>(
|
|||
patterns: start,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5589,7 +5589,7 @@ fn store_pattern_help<'a>(
|
|||
AppliedTag {
|
||||
arguments, layout, ..
|
||||
} => {
|
||||
let wrapped = Wrapped::from_layout(layout);
|
||||
let wrapped = Wrapped::from_layout(&Layout::Union(*layout));
|
||||
let write_tag = wrapped == Wrapped::MultiTagUnion;
|
||||
|
||||
let mut arg_layouts = Vec::with_capacity_in(arguments.len(), env.arena);
|
||||
|
@ -5607,10 +5607,10 @@ fn store_pattern_help<'a>(
|
|||
for (index, (argument, arg_layout)) in arguments.iter().enumerate().rev() {
|
||||
let index = if write_tag { index + 1 } else { index };
|
||||
|
||||
let mut arg_layout = arg_layout;
|
||||
let mut arg_layout = *arg_layout;
|
||||
|
||||
if let Layout::RecursivePointer = arg_layout {
|
||||
arg_layout = layout;
|
||||
arg_layout = Layout::Union(*layout);
|
||||
}
|
||||
|
||||
let load = Expr::AccessAtIndex {
|
||||
|
@ -5623,7 +5623,7 @@ fn store_pattern_help<'a>(
|
|||
match argument {
|
||||
Identifier(symbol) => {
|
||||
// store immediately in the given symbol
|
||||
stmt = Stmt::Let(*symbol, load, *arg_layout, env.arena.alloc(stmt));
|
||||
stmt = Stmt::Let(*symbol, load, arg_layout, env.arena.alloc(stmt));
|
||||
is_productive = true;
|
||||
}
|
||||
Underscore => {
|
||||
|
@ -5645,7 +5645,7 @@ fn store_pattern_help<'a>(
|
|||
stmt = new;
|
||||
// only if we bind one of its (sub)fields to a used name should we
|
||||
// extract the field
|
||||
stmt = Stmt::Let(symbol, load, *arg_layout, env.arena.alloc(stmt));
|
||||
stmt = Stmt::Let(symbol, load, arg_layout, env.arena.alloc(stmt));
|
||||
}
|
||||
StorePattern::NotProductive(new) => {
|
||||
// do nothing
|
||||
|
@ -6734,7 +6734,7 @@ pub enum Pattern<'a> {
|
|||
tag_name: TagName,
|
||||
tag_id: u8,
|
||||
arguments: Vec<'a, (Pattern<'a>, Layout<'a>)>,
|
||||
layout: Layout<'a>,
|
||||
layout: UnionLayout<'a>,
|
||||
union: crate::exhaustive::Union,
|
||||
},
|
||||
}
|
||||
|
@ -6928,7 +6928,9 @@ fn from_can_pattern_help<'a>(
|
|||
));
|
||||
}
|
||||
|
||||
let layout = Layout::Struct(field_layouts.into_bump_slice());
|
||||
let layout = UnionLayout::NonRecursive(
|
||||
env.arena.alloc([field_layouts.into_bump_slice()]),
|
||||
);
|
||||
|
||||
Pattern::AppliedTag {
|
||||
tag_name: tag_name.clone(),
|
||||
|
@ -7016,8 +7018,7 @@ fn from_can_pattern_help<'a>(
|
|||
temp
|
||||
};
|
||||
|
||||
let layout =
|
||||
Layout::Union(UnionLayout::NonRecursive(layouts.into_bump_slice()));
|
||||
let layout = UnionLayout::NonRecursive(layouts.into_bump_slice());
|
||||
|
||||
Pattern::AppliedTag {
|
||||
tag_name: tag_name.clone(),
|
||||
|
@ -7075,8 +7076,7 @@ fn from_can_pattern_help<'a>(
|
|||
};
|
||||
|
||||
debug_assert!(layouts.len() > 1);
|
||||
let layout =
|
||||
Layout::Union(UnionLayout::Recursive(layouts.into_bump_slice()));
|
||||
let layout = UnionLayout::Recursive(layouts.into_bump_slice());
|
||||
|
||||
Pattern::AppliedTag {
|
||||
tag_name: tag_name.clone(),
|
||||
|
@ -7121,7 +7121,7 @@ fn from_can_pattern_help<'a>(
|
|||
));
|
||||
}
|
||||
|
||||
let layout = Layout::Union(UnionLayout::NonNullableUnwrapped(fields));
|
||||
let layout = UnionLayout::NonNullableUnwrapped(fields);
|
||||
|
||||
Pattern::AppliedTag {
|
||||
tag_name: tag_name.clone(),
|
||||
|
@ -7206,10 +7206,10 @@ fn from_can_pattern_help<'a>(
|
|||
temp
|
||||
};
|
||||
|
||||
let layout = Layout::Union(UnionLayout::NullableWrapped {
|
||||
let layout = UnionLayout::NullableWrapped {
|
||||
nullable_id,
|
||||
other_tags: layouts.into_bump_slice(),
|
||||
});
|
||||
};
|
||||
|
||||
Pattern::AppliedTag {
|
||||
tag_name: tag_name.clone(),
|
||||
|
@ -7267,10 +7267,10 @@ fn from_can_pattern_help<'a>(
|
|||
));
|
||||
}
|
||||
|
||||
let layout = Layout::Union(UnionLayout::NullableUnwrapped {
|
||||
let layout = UnionLayout::NullableUnwrapped {
|
||||
nullable_id,
|
||||
other_fields,
|
||||
});
|
||||
};
|
||||
|
||||
Pattern::AppliedTag {
|
||||
tag_name: tag_name.clone(),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue