From b9f92851a4f771f67a0c96b21acfc5095090dddc Mon Sep 17 00:00:00 2001 From: Folkert Date: Tue, 29 Dec 2020 02:10:27 +0100 Subject: [PATCH] remove optional fields destruct in mono pattern --- compiler/mono/src/decision_tree.rs | 38 +++++++++++---------------- compiler/mono/src/exhaustive.rs | 2 +- compiler/mono/src/ir.rs | 42 ++++++++++++------------------ 3 files changed, 32 insertions(+), 50 deletions(-) diff --git a/compiler/mono/src/decision_tree.rs b/compiler/mono/src/decision_tree.rs index 15eca105c8..4bb8d08d09 100644 --- a/compiler/mono/src/decision_tree.rs +++ b/compiler/mono/src/decision_tree.rs @@ -411,9 +411,6 @@ fn test_at_path<'a>(selected_path: &Path, branch: &Branch<'a>, all_tests: &mut V DestructType::Required => { arguments.push((Pattern::Underscore, destruct.layout.clone())); } - DestructType::Optional(_expr) => { - // do nothing - } } } @@ -540,27 +537,22 @@ fn to_relevant_branch_help<'a>( .. } => { debug_assert!(test_name == &TagName::Global(RECORD_TAG_NAME.into())); - let sub_positions = destructs - .into_iter() - .filter(|destruct| !matches!(destruct.typ, DestructType::Optional(_))) - .enumerate() - .map(|(index, destruct)| { - let pattern = match destruct.typ { - DestructType::Guard(guard) => guard.clone(), - DestructType::Required => Pattern::Underscore, - DestructType::Optional(_expr) => unreachable!("because of the filter"), - }; + let sub_positions = destructs.into_iter().enumerate().map(|(index, destruct)| { + let pattern = match destruct.typ { + DestructType::Guard(guard) => guard.clone(), + DestructType::Required => Pattern::Underscore, + }; - ( - Path::Index { - index: index as u64, - tag_id: *tag_id, - path: Box::new(path.clone()), - }, - Guard::NoGuard, - pattern, - ) - }); + ( + Path::Index { + index: index as u64, + tag_id: *tag_id, + path: Box::new(path.clone()), + }, + Guard::NoGuard, + pattern, + ) + }); start.extend(sub_positions); start.extend(end); diff --git a/compiler/mono/src/exhaustive.rs b/compiler/mono/src/exhaustive.rs index a27227e646..51b61a4c07 100644 --- a/compiler/mono/src/exhaustive.rs +++ b/compiler/mono/src/exhaustive.rs @@ -67,7 +67,7 @@ fn simplify<'a>(pattern: &crate::ir::Pattern<'a>) -> Pattern { field_names.push(destruct.label.clone()); match &destruct.typ { - DestructType::Required | DestructType::Optional(_) => patterns.push(Anything), + DestructType::Required => patterns.push(Anything), DestructType::Guard(guard) => patterns.push(simplify(guard)), } } diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index 1f45b5fcf0..de8b90a9f2 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -4802,17 +4802,6 @@ fn store_record_destruct<'a>( 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 { Identifier(symbol) => { stmt = Stmt::Let( @@ -5535,14 +5524,13 @@ pub struct RecordDestruct<'a> { pub label: Lowercase, pub variable: Variable, pub layout: Layout<'a>, - pub symbol: Symbol, pub typ: DestructType<'a>, + pub symbol: Symbol, } #[derive(Clone, Debug, PartialEq)] pub enum DestructType<'a> { Required, - Optional(roc_can::expr::Expr), 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 match &destruct.value.typ { roc_can::pattern::DestructType::Optional(field_var, loc_expr) => { - let field_layout = layout_cache - .from_var(env.arena, *field_var, env.subs) - .unwrap_or_else(|err| { - panic!("TODO turn fn_var into a RuntimeError {:?}", err) - }); - - mono_destructs.push(RecordDestruct { - label: destruct.value.label.clone(), - symbol: destruct.value.symbol, - variable: destruct.value.var, - layout: field_layout, - typ: DestructType::Optional(loc_expr.value.clone()), - }) + // TODO these don't match up in the uniqueness inference; when we remove + // that, reinstate this assert! + // + // dbg!(&env.subs.get_without_compacting(*field_var).content); + // dbg!(&env.subs.get_without_compacting(destruct.value.var).content); + // debug_assert_eq!( + // env.subs.get_root_key_without_compacting(*field_var), + // env.subs.get_root_key_without_compacting(destruct.value.var) + // ); + assignments.push(( + destruct.value.symbol, + // destruct.value.var, + *field_var, + loc_expr.value.clone(), + )); } _ => unreachable!("only optional destructs can be optional fields"), }