The type inference starts at type_inference.cr. Here, the program's AST is visited with a TypeVisitor. This visitor's does many things:
- It declares classes and defs (when the visitor visits a ClassDef node it declares a class, etc.). Note that the visitor doesn't visits a def's contents.
- It types literals. For example the number 1, represented by a NumberLiteral node, is assigned the type
Int32. Every ASTNode has a type, which initially is nil. - It keeps track of variables defined a method (or at the top level) and their types. For example, when the visitor finds an assignment like
a = 1it first visits1and assigns the typeInt32to it. Then it binds the nodeato the