support nested pattern matches on tag unions

This commit is contained in:
Folkert 2020-03-20 22:14:38 +01:00
parent 0f22cbbf7d
commit 655dc32098
2 changed files with 58 additions and 36 deletions

View file

@ -330,7 +330,7 @@ pub fn build_expr<'a, B: Backend>(
let mem_flags = MemFlags::new(); let mem_flags = MemFlags::new();
let expr = build_expr(env, scope, module, builder, expr, procs); 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 return builder
.ins() .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> { struct Branch2<'a> {
cond: &'a Expr<'a>, cond: &'a Expr<'a>,
cond_layout: &'a Layout<'a>, cond_layout: &'a Layout<'a>,
@ -570,7 +555,7 @@ fn build_switch<'a, B: Backend>(
let mem_flags = MemFlags::new(); 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 builder
.ins() .ins()

View file

@ -1438,25 +1438,62 @@ mod test_gen {
); );
} }
// #[test] #[test]
// fn when_on_just_just() { fn nested_tag_union() {
// assert_evals_to!( assert_evals_to!(
// indoc!( indoc!(
// r#" r#"
// Maybe a : [ Nothing, Just a ] Maybe a : [ Nothing, Just a ]
//
// x : Maybe (Maybe a) x : Maybe (Maybe a)
// x = Just (Just 41) x = Just (Just 41)
//
// when x is 5
// Just (Just v) -> v + 0x1 "#
// _ -> 0x1 ),
// "# 5,
// ), i64
// 42, );
// 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] // #[test]
// fn linked_list_empty() { // fn linked_list_empty() {