Type stability when a branch can throw an exception

Is a function considered type stable if it has a branch that can throw an exception? Here’s an example:

function foo(x::Int)
    if x < 0
        error("Oops.")
    else
        2x
    end
end

@code_warntype doesn’t seem to have a problem with it:

julia> @code_warntype foo(-3)
Variables
  #self#::Core.Compiler.Const(foo, false)
  x::Int64

Body::Int64
1 ─ %1 = (x < 0)::Bool
└──      goto #3 if not %1
2 ─      Main.error("Oops.")
└──      Core.Compiler.Const(:(return %3), false)
3 ┄ %5 = (2 * x)::Int64
└──      return %5

Considering that any function called inside a branch could conceivably throw an exception, I suppose it would be bad if the possibility of an exception interfered with type inference…

Yes, this is type stable. Type instabilities are only when the return type can’t be inferred properly. In this case, throw doesn’t return, it just halts the execution.

julia> Base.return_types(foo, tuple(Int))
1-element Array{Any,1}:
 Int64
5 Likes