diff --git a/CHANGELOG.md b/CHANGELOG.md index 067da397..79c89b7b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project does not currently adhere to a particular versioning scheme. - Fix readback of numeric operations. ([#467][gh-467]) - Propagate the "builtin" attribute of definitions when extracting functions from `bend` and `fold` syntax. +- Panic while using unscoped variables on definition patterns. ([#468][gh-468]) ### Added @@ -339,6 +340,7 @@ and this project does not currently adhere to a particular versioning scheme. [gh-465]: https://github.com/HigherOrderCO/Bend/issues/465 [gh-466]: https://github.com/HigherOrderCO/Bend/issues/466 [gh-467]: https://github.com/HigherOrderCO/Bend/issues/467 +[gh-468]: https://github.com/HigherOrderCO/Bend/issues/468 [gh-470]: https://github.com/HigherOrderCO/Bend/issues/470 [gh-475]: https://github.com/HigherOrderCO/Bend/issues/475 [gh-478]: https://github.com/HigherOrderCO/Bend/issues/478 diff --git a/src/fun/transform/desugar_match_defs.rs b/src/fun/transform/desugar_match_defs.rs index b1b7bb77..36abcf69 100644 --- a/src/fun/transform/desugar_match_defs.rs +++ b/src/fun/transform/desugar_match_defs.rs @@ -127,9 +127,19 @@ fn simplify_rule_match( fn irrefutable_fst_row_rule(args: Vec, rule: Rule) -> Term { let mut term = rule.body; for (arg, pat) in args.into_iter().zip(rule.pats.into_iter()) { - let Pattern::Var(var) = pat else { unreachable!() }; - if let Some(var) = var { - term = Term::Use { nam: Some(var), val: Box::new(Term::Var { nam: arg }), nxt: Box::new(term) }; + match pat { + Pattern::Var(None) => {} + Pattern::Var(Some(var)) => { + term = Term::Use { nam: Some(var), val: Box::new(Term::Var { nam: arg }), nxt: Box::new(term) }; + } + Pattern::Chn(var) => { + term = Term::Let { + pat: Box::new(Pattern::Chn(var)), + val: Box::new(Term::Var { nam: arg }), + nxt: Box::new(term), + }; + } + _ => unreachable!(), } } term diff --git a/tests/golden_tests/compile_file/def_pat_unscoped.bend b/tests/golden_tests/compile_file/def_pat_unscoped.bend new file mode 100644 index 00000000..37476696 --- /dev/null +++ b/tests/golden_tests/compile_file/def_pat_unscoped.bend @@ -0,0 +1,3 @@ +add $a b = (+ $a b) + +main = (add 9 4) diff --git a/tests/snapshots/compile_file__def_pat_unscoped.bend.snap b/tests/snapshots/compile_file__def_pat_unscoped.bend.snap new file mode 100644 index 00000000..ad2abaad --- /dev/null +++ b/tests/snapshots/compile_file__def_pat_unscoped.bend.snap @@ -0,0 +1,8 @@ +--- +source: tests/golden_tests.rs +input_file: tests/golden_tests/compile_file/def_pat_unscoped.bend +--- +@add = ($([+] $(a b)) (a b)) + +@main = a + & @add ~ (9 (4 a))