Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.Sign up
Compiler: use freeze_type as node type if node doesn't have a type #7161
When the compiler sees a method return type it will try to resolve it and then set it as the def's
This PR changes that: if there's no inferred type for a node but it has a
One example is the issue this PR solves:
class Tree def initialize(@value : Int32, @children : Array(Tree)) end def sum : Int32 @value + @children.each.map(&.sum).sum end end Tree.new(1,  of Tree).sum
Here the compiler will try to type
Don't worry: the compiler will later check that the type is effectively what we told it.
An interesting side effect of this change is that in these recursive scenarios the return type isn't a hint anymore, it's a type that's used for inference. For example, this works as well:
class Tree def initialize(@value : Int32, @children : Array(Tree)) end def sum : Float64 @value + @children.each.map(&.sum).sum end end Tree.new(1,  of Tree).sum
@bcardiff Would it be possible to run the script that checks the compiler works fine for many of the major crystal projects? This is a simple change, all specs pass, but I wonder if it could break something that I'm not seeing.