fix incorrect layout for Switch

This commit is contained in:
Folkert 2020-03-20 19:56:50 +01:00
parent d0da300042
commit 0f22cbbf7d
3 changed files with 11 additions and 10 deletions

View file

@ -1329,15 +1329,14 @@ mod test_gen {
); );
} }
/*
#[test] #[test]
fn pair_with_guard_pattern() { fn pair_with_guard_pattern() {
assert_evals_to!( assert_evals_to!(
indoc!( indoc!(
r#" r#"
when Pair 2 3 is when Pair 2 3 is
Pair 4 _ -> 9 Pair 4 _ -> 1
Pair 3 _ -> 9 Pair 3 _ -> 2
Pair a b -> a + b Pair a b -> a + b
"# "#
), ),
@ -1345,7 +1344,6 @@ mod test_gen {
i64 i64
); );
} }
*/
#[test] #[test]
fn result_with_guard_pattern() { fn result_with_guard_pattern() {

View file

@ -969,9 +969,9 @@ fn decide_to_branching<'a>(
tests, tests,
fallback, fallback,
} => { } => {
let cond = env // the cond_layout can change in the process. E.g. if the cond is a Tag, we actually
.arena // switch on the tag discriminant (currently an i64 value)
.alloc(path_to_expr(env, cond_symbol, &path, &cond_layout)); let (cond, cond_layout) = path_to_expr_help(env, cond_symbol, &path, cond_layout);
let default_branch = env.arena.alloc(decide_to_branching( let default_branch = env.arena.alloc(decide_to_branching(
env, env,
@ -1008,7 +1008,7 @@ fn decide_to_branching<'a>(
// make a jump table based on the tests // make a jump table based on the tests
Expr::Switch { Expr::Switch {
cond, cond: env.arena.alloc(cond),
cond_layout, cond_layout,
branches: branches.into_bump_slice(), branches: branches.into_bump_slice(),
default_branch, default_branch,

View file

@ -890,6 +890,7 @@ fn store_pattern<'a>(
Underscore => { Underscore => {
// ignore // ignore
} }
IntLiteral(_) | FloatLiteral(_) | EnumLiteral { .. } | BitLiteral(_) => {}
_ => { _ => {
// store the field in a symbol, and continue matching on it // store the field in a symbol, and continue matching on it
let symbol = env.fresh_symbol(); let symbol = env.fresh_symbol();
@ -927,6 +928,7 @@ fn store_record_destruct<'a>(
struct_layout: Layout<'a>, struct_layout: Layout<'a>,
stored: &mut Vec<'a, (Symbol, Layout<'a>, Expr<'a>)>, stored: &mut Vec<'a, (Symbol, Layout<'a>, Expr<'a>)>,
) -> Result<(), String> { ) -> Result<(), String> {
use Pattern::*;
let record = env.arena.alloc(Expr::Load(outer_symbol)); let record = env.arena.alloc(Expr::Load(outer_symbol));
let load = Expr::Access { let load = Expr::Access {
label: destruct.label.clone(), label: destruct.label.clone(),
@ -939,10 +941,10 @@ fn store_record_destruct<'a>(
stored.push((destruct.symbol, destruct.layout.clone(), load)); stored.push((destruct.symbol, destruct.layout.clone(), load));
} }
Some(guard_pattern) => match &guard_pattern { Some(guard_pattern) => match &guard_pattern {
Pattern::Identifier(symbol) => { Identifier(symbol) => {
stored.push((*symbol, destruct.layout.clone(), load)); stored.push((*symbol, destruct.layout.clone(), load));
} }
Pattern::Underscore => { Underscore => {
// important that this is special-cased to do nothing: mono record patterns will extract all the // important that this is special-cased to do nothing: mono record patterns will extract all the
// fields, but those not bound in the source code are guarded with the underscore // fields, but those not bound in the source code are guarded with the underscore
// pattern. So given some record `{ x : a, y : b }`, a match // pattern. So given some record `{ x : a, y : b }`, a match
@ -955,6 +957,7 @@ fn store_record_destruct<'a>(
// //
// internally. But `y` is never used, so we must make sure it't not stored/loaded. // internally. But `y` is never used, so we must make sure it't not stored/loaded.
} }
IntLiteral(_) | FloatLiteral(_) | EnumLiteral { .. } | BitLiteral(_) => {}
_ => { _ => {
let symbol = env.fresh_symbol(); let symbol = env.fresh_symbol();
stored.push((symbol, destruct.layout.clone(), load)); stored.push((symbol, destruct.layout.clone(), load));