diff --git a/lib/typeprof/core/ast/pattern.rb b/lib/typeprof/core/ast/pattern.rb index eed2da94..3ecccb48 100644 --- a/lib/typeprof/core/ast/pattern.rb +++ b/lib/typeprof/core/ast/pattern.rb @@ -153,8 +153,9 @@ def initialize(raw_node, lenv) def subnodes = { cond:, body: } def install_pattern0(genv, subject) - @cond.install(genv) + # The guard is evaluated after the pattern binds its variables @body.install_pattern(genv, subject) + @cond.install(genv) subject end end diff --git a/scenario/patterns/guard_binding.rb b/scenario/patterns/guard_binding.rb new file mode 100644 index 00000000..70c1fc89 --- /dev/null +++ b/scenario/patterns/guard_binding.rb @@ -0,0 +1,30 @@ +## update: test.rb +def cond?(x) + x +end + +def check_if(v) + case v + in [x] if cond?(x).even? + x + end +end + +def check_unless(v) + case v + in [_, y] unless cond?(y).even? + y + end +end + +check_if([1]) +check_unless([:a, 1]) + +## diagnostics + +## assert +class Object + def cond?: (Integer) -> Integer + def check_if: ([Integer]) -> Integer + def check_unless: ([:a, Integer]) -> Integer +end