unisonweb / unison

A friendly programming language from the future
https://unison-lang.org
Other
5.81k stars 271 forks source link

Variable capture during pattern matching #5419

Closed dolio closed 1 month ago

dolio commented 1 month ago

Code like the following:

humbug = do
  match (5, 6) with z ->
    trace "before" z
    match 99 with n -> ()
    trace "after" z

produces unexpected output:

> run humbug
trace: before
(5, 6)
trace: after
99

I believe the reason is that the pattern match compiler is capturing variables. To handle the 'whole result' match, it introduces a new name for compound expressions in the scrutinee, and I think it isn't being freshened enough. So, the z get substituted with the variable for the (5,6) match, then captured for the 99 match, because the scope is not limited to the match.