remove optional fields destruct in mono pattern

This commit is contained in:
Folkert 2020-12-29 02:10:27 +01:00
parent eb501f90a2
commit b9f92851a4
3 changed files with 32 additions and 50 deletions

View file

@ -411,9 +411,6 @@ fn test_at_path<'a>(selected_path: &Path, branch: &Branch<'a>, all_tests: &mut V
DestructType::Required => { DestructType::Required => {
arguments.push((Pattern::Underscore, destruct.layout.clone())); arguments.push((Pattern::Underscore, destruct.layout.clone()));
} }
DestructType::Optional(_expr) => {
// do nothing
}
} }
} }
@ -540,15 +537,10 @@ fn to_relevant_branch_help<'a>(
.. ..
} => { } => {
debug_assert!(test_name == &TagName::Global(RECORD_TAG_NAME.into())); debug_assert!(test_name == &TagName::Global(RECORD_TAG_NAME.into()));
let sub_positions = destructs let sub_positions = destructs.into_iter().enumerate().map(|(index, destruct)| {
.into_iter()
.filter(|destruct| !matches!(destruct.typ, DestructType::Optional(_)))
.enumerate()
.map(|(index, destruct)| {
let pattern = match destruct.typ { let pattern = match destruct.typ {
DestructType::Guard(guard) => guard.clone(), DestructType::Guard(guard) => guard.clone(),
DestructType::Required => Pattern::Underscore, DestructType::Required => Pattern::Underscore,
DestructType::Optional(_expr) => unreachable!("because of the filter"),
}; };
( (

View file

@ -67,7 +67,7 @@ fn simplify<'a>(pattern: &crate::ir::Pattern<'a>) -> Pattern {
field_names.push(destruct.label.clone()); field_names.push(destruct.label.clone());
match &destruct.typ { match &destruct.typ {
DestructType::Required | DestructType::Optional(_) => patterns.push(Anything), DestructType::Required => patterns.push(Anything),
DestructType::Guard(guard) => patterns.push(simplify(guard)), DestructType::Guard(guard) => patterns.push(simplify(guard)),
} }
} }

View file

@ -4802,17 +4802,6 @@ fn store_record_destruct<'a>(
env.arena.alloc(stmt), env.arena.alloc(stmt),
); );
} }
DestructType::Optional(expr) => {
stmt = with_hole(
env,
expr.clone(),
destruct.variable,
procs,
layout_cache,
destruct.symbol,
env.arena.alloc(stmt),
);
}
DestructType::Guard(guard_pattern) => match &guard_pattern { DestructType::Guard(guard_pattern) => match &guard_pattern {
Identifier(symbol) => { Identifier(symbol) => {
stmt = Stmt::Let( stmt = Stmt::Let(
@ -5535,14 +5524,13 @@ pub struct RecordDestruct<'a> {
pub label: Lowercase, pub label: Lowercase,
pub variable: Variable, pub variable: Variable,
pub layout: Layout<'a>, pub layout: Layout<'a>,
pub symbol: Symbol,
pub typ: DestructType<'a>, pub typ: DestructType<'a>,
pub symbol: Symbol,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum DestructType<'a> { pub enum DestructType<'a> {
Required, Required,
Optional(roc_can::expr::Expr),
Guard(Pattern<'a>), Guard(Pattern<'a>),
} }
@ -5883,19 +5871,21 @@ fn from_can_pattern_help<'a>(
// it must be an optional field, and we will use the default // it must be an optional field, and we will use the default
match &destruct.value.typ { match &destruct.value.typ {
roc_can::pattern::DestructType::Optional(field_var, loc_expr) => { roc_can::pattern::DestructType::Optional(field_var, loc_expr) => {
let field_layout = layout_cache // TODO these don't match up in the uniqueness inference; when we remove
.from_var(env.arena, *field_var, env.subs) // that, reinstate this assert!
.unwrap_or_else(|err| { //
panic!("TODO turn fn_var into a RuntimeError {:?}", err) // dbg!(&env.subs.get_without_compacting(*field_var).content);
}); // dbg!(&env.subs.get_without_compacting(destruct.value.var).content);
// debug_assert_eq!(
mono_destructs.push(RecordDestruct { // env.subs.get_root_key_without_compacting(*field_var),
label: destruct.value.label.clone(), // env.subs.get_root_key_without_compacting(destruct.value.var)
symbol: destruct.value.symbol, // );
variable: destruct.value.var, assignments.push((
layout: field_layout, destruct.value.symbol,
typ: DestructType::Optional(loc_expr.value.clone()), // destruct.value.var,
}) *field_var,
loc_expr.value.clone(),
));
} }
_ => unreachable!("only optional destructs can be optional fields"), _ => unreachable!("only optional destructs can be optional fields"),
} }