diff --git a/jacobs2021/src/lib.rs b/jacobs2021/src/lib.rs index 54b4166..bf53a08 100644 --- a/jacobs2021/src/lib.rs +++ b/jacobs2021/src/lib.rs @@ -550,16 +550,20 @@ impl Compiler { } tested.insert(key, raw_cases.len()); - raw_cases.push((cons, Vec::new(), vec![row])); + + let mut rows = fallback_rows.clone(); + + rows.push(row); + raw_cases.push((cons, Vec::new(), rows)); } else { + for (_, _, rows) in &mut raw_cases { + rows.push(row.clone()); + } + fallback_rows.push(row); } } - for (_, _, rows) in &mut raw_cases { - rows.append(&mut fallback_rows.clone()); - } - let cases = raw_cases .into_iter() .map(|(cons, vars, rows)| { @@ -1041,6 +1045,35 @@ mod tests { ); } + #[test] + fn test_compile_unreachable_int_pattern() { + let mut compiler = Compiler::new(); + let int_type = new_type(&mut compiler, Type::Int); + let input = compiler.new_variable(int_type); + let result = compile( + compiler, + input, + vec![(int(4), rhs(1)), (bind("a"), rhs(3)), (int(5), rhs(2))], + ); + + assert_eq!( + result.tree, + Decision::Switch( + input, + vec![ + Case::new(Constructor::Int(4), Vec::new(), success(1)), + Case::new( + Constructor::Int(5), + Vec::new(), + success_with_bindings(vec![("a", input)], 3) + ), + ], + Some(Box::new(success_with_bindings(vec![("a", input)], 3))) + ) + ); + assert_eq!(result.diagnostics.reachable, vec![1, 3, 3]); + } + #[test] fn test_compile_nonexhaustive_nested_int_pattern() { let mut compiler = Compiler::new();