I Built A Crystal Compiler With Parallel/Incremental Compilation

This assumption doesn’t necessarily hold. Parameter and return types are only restrictions, not actual types. The values are not cast to them.
Changing the method body can easily affect the actual return type or cause implications for parameter types, without changing any of the type restrictions.

For example, uncommenting the line in this example would not alter the signature of ::foo, but it breaks code semantics outside of it.

def foo(x : Int32) : Int32?
  # return nil if x.zero?
  1 // x
end

foo(3) + 1 # Error: undefined method '+' for Nil (compile-time type is (Int32 | Nil)) (if line 2 is uncommented)

This kind of dynamic is a tough nut to crack for compiling Crystal incrementally.

1 Like