From 655dc32098c36a7e44f830e3f20ec780e04f57d0 Mon Sep 17 00:00:00 2001 From: Folkert Date: Fri, 20 Mar 2020 22:14:38 +0100 Subject: [PATCH] support nested pattern matches on tag unions --- compiler/gen/src/crane/build.rs | 19 +-------- compiler/gen/tests/test_gen.rs | 75 ++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 36 deletions(-) diff --git a/compiler/gen/src/crane/build.rs b/compiler/gen/src/crane/build.rs index 6f7dde44b4..ab17c4b4f0 100644 --- a/compiler/gen/src/crane/build.rs +++ b/compiler/gen/src/crane/build.rs @@ -330,7 +330,7 @@ pub fn build_expr<'a, B: Backend>( let mem_flags = MemFlags::new(); let expr = build_expr(env, scope, module, builder, expr, procs); - let ret_type = layout_to_type(&field_layout, cfg.pointer_type()); + let ret_type = type_from_layout(cfg, field_layout); return builder .ins() @@ -426,21 +426,6 @@ pub fn build_expr<'a, B: Backend>( } } -fn layout_to_type<'a>(layout: &Layout<'a>, _pointer_type: Type) -> Type { - use roc_mono::layout::Builtin::*; - - match layout { - Layout::Builtin(builtin) => match builtin { - Int64 => cranelift::prelude::types::I64, - Byte => cranelift::prelude::types::I8, - Bool => cranelift::prelude::types::B1, - Float64 => cranelift::prelude::types::F64, - other => panic!("I don't yet know how to make a type from {:?}", other), - }, - other => panic!("I don't yet know how to make a type from {:?}", other), - } -} - struct Branch2<'a> { cond: &'a Expr<'a>, cond_layout: &'a Layout<'a>, @@ -570,7 +555,7 @@ fn build_switch<'a, B: Backend>( let mem_flags = MemFlags::new(); - let ret_type = layout_to_type(&new_cond_layout, env.cfg.pointer_type()); + let ret_type = type_from_layout(env.cfg, &new_cond_layout); builder .ins() diff --git a/compiler/gen/tests/test_gen.rs b/compiler/gen/tests/test_gen.rs index 8083e12429..adae9d07fc 100644 --- a/compiler/gen/tests/test_gen.rs +++ b/compiler/gen/tests/test_gen.rs @@ -1438,25 +1438,62 @@ mod test_gen { ); } - // #[test] - // fn when_on_just_just() { - // assert_evals_to!( - // indoc!( - // r#" - // Maybe a : [ Nothing, Just a ] - // - // x : Maybe (Maybe a) - // x = Just (Just 41) - // - // when x is - // Just (Just v) -> v + 0x1 - // _ -> 0x1 - // "# - // ), - // 42, - // i64 - // ); - // } + #[test] + fn nested_tag_union() { + assert_evals_to!( + indoc!( + r#" + Maybe a : [ Nothing, Just a ] + + x : Maybe (Maybe a) + x = Just (Just 41) + + 5 + "# + ), + 5, + i64 + ); + } + + #[test] + fn nested_record_load() { + assert_evals_to!( + indoc!( + r#" + Maybe a : [ Nothing, Just a ] + + x = { a : { b : 0x5 } } + + y = x.a + + y.b + "# + ), + 5, + i64 + ); + } + + #[test] + fn nested_pattern_match() { + assert_evals_to!( + indoc!( + r#" + Maybe a : [ Nothing, Just a ] + + x : Maybe (Maybe a) + x = Just (Just 41) + + when x is + Just (Just v) -> v + 0x1 + _ -> 0x1 + "# + ), + 42, + i64 + ); + } // #[test] // fn linked_list_empty() {