diff --git a/compiler/mono/src/decision_tree.rs b/compiler/mono/src/decision_tree.rs index ae2de26840..ef6a508c54 100644 --- a/compiler/mono/src/decision_tree.rs +++ b/compiler/mono/src/decision_tree.rs @@ -1078,11 +1078,15 @@ pub fn optimize_when<'a>( .into_iter() .enumerate() .map(|(index, (pattern, guard, branch))| { - ((guard, pattern, index as u64), (index as u64, branch)) + let has_guard = !guard.is_none(); + ( + (guard, pattern.clone(), index as u64), + (index as u64, branch, pattern, has_guard), + ) }) .unzip(); - let indexed_branches: Vec<(u64, Stmt<'a>)> = _indexed_branches; + let indexed_branches: Vec<_> = _indexed_branches; let decision_tree = compile(patterns); let decider = tree_to_decider(decision_tree); @@ -1094,7 +1098,14 @@ pub fn optimize_when<'a>( let mut choices = MutMap::default(); let mut jumps = Vec::new(); - for (index, branch) in indexed_branches.into_iter() { + for (index, mut branch, pattern, has_guard) in indexed_branches.into_iter() { + // bind the fields referenced in the pattern. For guards this happens separately, so + // the pattern variables are defined when evaluating the guard. + if !has_guard { + branch = + crate::ir::store_pattern(env, procs, layout_cache, &pattern, cond_symbol, branch); + } + let ((branch_index, choice), opt_jump) = create_choices(&target_counts, index, branch); if let Some((index, body)) = opt_jump { diff --git a/compiler/mono/src/ir.rs b/compiler/mono/src/ir.rs index 55d1bc585e..a693052ab9 100644 --- a/compiler/mono/src/ir.rs +++ b/compiler/mono/src/ir.rs @@ -5096,8 +5096,6 @@ fn from_can_when<'a>( jump, ); - // let new_guard_stmt = store_pattern(env, procs, layout_cache, &pattern, cond_symbol, guard_stmt); - ( pattern.clone(), Guard::Guard { @@ -5108,9 +5106,7 @@ fn from_can_when<'a>( branch_stmt, ) } else { - let new_branch_stmt = - store_pattern(env, procs, layout_cache, &pattern, cond_symbol, branch_stmt); - (pattern, Guard::NoGuard, new_branch_stmt) + (pattern, Guard::NoGuard, branch_stmt) } }); let mono_branches = Vec::from_iter_in(it, arena); diff --git a/compiler/test_mono/generated/nested_pattern_match.txt b/compiler/test_mono/generated/nested_pattern_match.txt index 38b4165ff0..24279438b5 100644 --- a/compiler/test_mono/generated/nested_pattern_match.txt +++ b/compiler/test_mono/generated/nested_pattern_match.txt @@ -7,8 +7,8 @@ procedure Test.0 (): let Test.20 = Just Test.21; let Test.2 = Just Test.20; joinpoint Test.17: - let Test.11 = 1i64; - ret Test.11; + let Test.10 = 1i64; + ret Test.10; in let Test.15 = 0i64; let Test.16 = GetTagId Test.2; @@ -19,8 +19,8 @@ procedure Test.0 (): let Test.14 = GetTagId Test.12; let Test.18 = lowlevel Eq Test.13 Test.14; if Test.18 then - let Test.10 = UnionAtIndex (Id 0) (Index 0) Test.2; - let Test.5 = UnionAtIndex (Id 0) (Index 0) Test.10; + let Test.11 = UnionAtIndex (Id 0) (Index 0) Test.2; + let Test.5 = UnionAtIndex (Id 0) (Index 0) Test.11; let Test.7 = 1i64; let Test.6 = CallByName Num.24 Test.5 Test.7; ret Test.6; diff --git a/compiler/test_mono/generated/peano2.txt b/compiler/test_mono/generated/peano2.txt index 673b61ffe4..4a1a5a234f 100644 --- a/compiler/test_mono/generated/peano2.txt +++ b/compiler/test_mono/generated/peano2.txt @@ -22,9 +22,9 @@ procedure Test.0 (): let Test.7 = 1i64; ret Test.7; else - let Test.9 = 0i64; - ret Test.9; + let Test.8 = 0i64; + ret Test.8; else dec Test.2; - let Test.10 = 0i64; - ret Test.10; + let Test.9 = 0i64; + ret Test.9; diff --git a/compiler/test_mono/generated/quicksort_swap.txt b/compiler/test_mono/generated/quicksort_swap.txt index fd927efcce..4845bb7c58 100644 --- a/compiler/test_mono/generated/quicksort_swap.txt +++ b/compiler/test_mono/generated/quicksort_swap.txt @@ -26,8 +26,8 @@ procedure Test.1 (Test.2): let Test.29 = CallByName List.3 Test.2 Test.31; let Test.8 = Struct {Test.29, Test.30}; joinpoint Test.26: - let Test.19 = Array []; - ret Test.19; + let Test.17 = Array []; + ret Test.17; in let Test.23 = StructAtIndex 1 Test.8; let Test.24 = 1i64; @@ -39,10 +39,10 @@ procedure Test.1 (Test.2): let Test.22 = GetTagId Test.20; let Test.27 = lowlevel Eq Test.21 Test.22; if Test.27 then - let Test.18 = StructAtIndex 0 Test.8; - let Test.4 = UnionAtIndex (Id 1) (Index 0) Test.18; - let Test.17 = StructAtIndex 1 Test.8; - let Test.5 = UnionAtIndex (Id 1) (Index 0) Test.17; + let Test.19 = StructAtIndex 0 Test.8; + let Test.4 = UnionAtIndex (Id 1) (Index 0) Test.19; + let Test.18 = StructAtIndex 1 Test.8; + let Test.5 = UnionAtIndex (Id 1) (Index 0) Test.18; let Test.16 = 0i64; let Test.10 = CallByName List.4 Test.2 Test.16 Test.5; let Test.11 = 0i64; diff --git a/compiler/test_mono/generated/rigids.txt b/compiler/test_mono/generated/rigids.txt index f8ccc8bde6..d6a146ba1b 100644 --- a/compiler/test_mono/generated/rigids.txt +++ b/compiler/test_mono/generated/rigids.txt @@ -24,8 +24,8 @@ procedure Test.1 (Test.2, Test.3, Test.4): let Test.32 = CallByName List.3 Test.4 Test.2; let Test.13 = Struct {Test.32, Test.33}; joinpoint Test.29: - let Test.22 = Array []; - ret Test.22; + let Test.20 = Array []; + ret Test.20; in let Test.26 = StructAtIndex 1 Test.13; let Test.27 = 1i64; @@ -37,10 +37,10 @@ procedure Test.1 (Test.2, Test.3, Test.4): let Test.25 = GetTagId Test.23; let Test.30 = lowlevel Eq Test.24 Test.25; if Test.30 then - let Test.21 = StructAtIndex 0 Test.13; - let Test.6 = UnionAtIndex (Id 1) (Index 0) Test.21; - let Test.20 = StructAtIndex 1 Test.13; - let Test.7 = UnionAtIndex (Id 1) (Index 0) Test.20; + let Test.22 = StructAtIndex 0 Test.13; + let Test.6 = UnionAtIndex (Id 1) (Index 0) Test.22; + let Test.21 = StructAtIndex 1 Test.13; + let Test.7 = UnionAtIndex (Id 1) (Index 0) Test.21; let Test.15 = CallByName List.4 Test.4 Test.2 Test.7; let Test.14 = CallByName List.4 Test.15 Test.3 Test.6; ret Test.14; diff --git a/compiler/test_mono/generated/when_nested_maybe.txt b/compiler/test_mono/generated/when_nested_maybe.txt index 38b4165ff0..24279438b5 100644 --- a/compiler/test_mono/generated/when_nested_maybe.txt +++ b/compiler/test_mono/generated/when_nested_maybe.txt @@ -7,8 +7,8 @@ procedure Test.0 (): let Test.20 = Just Test.21; let Test.2 = Just Test.20; joinpoint Test.17: - let Test.11 = 1i64; - ret Test.11; + let Test.10 = 1i64; + ret Test.10; in let Test.15 = 0i64; let Test.16 = GetTagId Test.2; @@ -19,8 +19,8 @@ procedure Test.0 (): let Test.14 = GetTagId Test.12; let Test.18 = lowlevel Eq Test.13 Test.14; if Test.18 then - let Test.10 = UnionAtIndex (Id 0) (Index 0) Test.2; - let Test.5 = UnionAtIndex (Id 0) (Index 0) Test.10; + let Test.11 = UnionAtIndex (Id 0) (Index 0) Test.2; + let Test.5 = UnionAtIndex (Id 0) (Index 0) Test.11; let Test.7 = 1i64; let Test.6 = CallByName Num.24 Test.5 Test.7; ret Test.6;