Module: Jade::Frontend::TypeChecking::Inference::IfThenElse

Extended by:
Helpers, IfThenElse
Included in:
IfThenElse
Defined in:
lib/jade/frontend/type_checking/inference/if_then_else.rb

Instance Method Summary collapse

Methods included from Helpers

check, generalize, instantiate, type_from_symbol, unify

Instance Method Details

#infer(node, registry, state, expected) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/jade/frontend/type_checking/inference/if_then_else.rb', line 9

def infer(node, registry, state, expected)
  node => AST::IfThenElse(condition:, if_branch:, else_branch:)

  cond_state, cond_result = check(condition, registry, state, Expected.check(Type.bool))
  after_cond_state, _ = cond_state.unify_result(cond_result, Type.bool) do
    Error::IfConditionTypeMismatch.new(
      state.env.entry_name,
      condition.range,
      actual: it.actual,
    )
  end

  if_state, if_result = check(if_branch, registry, after_cond_state, expected)
  else_state, else_result = check(else_branch, registry, if_state, expected)

  constraints = cond_result.constraints +
    if_result.constraints +
    else_result.constraints

  else_state
    .unify_result(
      else_result.with(constraints:),
      if_result.type,
      expected.rigid_vars
    ) do
      Error::IfBranchesTypeMismatch.new(
        state.env.entry_name,
        else_branch.range,
        expected: it.expected,
        actual: it.actual,
      )
    end
end