hello, I’ve tried to ensure type stability by using @code_warntype. However, something very strange happened: In the following, closureB() only differs from closureA() with a leading if-end block, which is irrelevant actually. But, a lot of types in gg() becomes ANY, is it a bug??? I’m using JuliaPro-0.6.3.1.
function closureA(x::Int64)::Function
oneto3 = Base.OneTo(3)
function f()
for i in oneto3
println(i)
end
end
return f
end
function closureB(x::Int64)::Function
### the only difference with closureA()
if x < 0
error("abc")
end
### the only difference with closureB()
oneto3 = Base.OneTo(3)
function f()
for i in oneto3
println(i)
end
end
return f
end
ff = closureA(1)
gg = closureB(1)
julia> @code_warntype ff()
Variables:
#self#::#f#5{Base.OneTo{Int64}}
i::Int64
#temp#::Int64
Body:
begin
SSAValue(0) = (Core.getfield)(#self#::#f#5{Base.OneTo{Int64}}, :oneto3)::Base.OneTo{Int64}
#temp#::Int64 = 1
3:
unless (Base.not_int)((#temp#::Int64 === (Base.add_int)((Core.getfield)(SSAValue(0), :stop)::Int64, 1)::Int64)::Bool)::Bool goto 18
SSAValue(3) = #temp#::Int64
SSAValue(4) = (Base.add_int)(#temp#::Int64, 1)::Int64
i::Int64 = SSAValue(3)
#temp#::Int64 = SSAValue(4) # line 6:
$(Expr(:inbounds, false))
# meta: location coreio.jl println 5
SSAValue(2) = (Core.typeassert)(Base.STDOUT, Base.IO)::IO
# meta: pop location
$(Expr(:inbounds, :pop))
(Base.print)(SSAValue(2), i::Int64, $(QuoteNode('\n')))::Void
16:
goto 3
18:
return
end::Void
julia> @code_warntype gg()
Variables:
#self#::#f#8
i::Any
#temp#::Any
Body:
begin
SSAValue(0) = (Core.getfield)((Core.getfield)(#self#::#f#8, :oneto3)::Any, :contents)::Any
#temp#::Any = (Base.start)(SSAValue(0))::Any
3:
unless !((Base.done)(SSAValue(0), #temp#::Any)::Any)::Any goto 12
SSAValue(1) = (Base.next)(SSAValue(0), #temp#::Any)::Any
i::Any = (Core.getfield)(SSAValue(1), 1)::Any
#temp#::Any = (Core.getfield)(SSAValue(1), 2)::Any # line 12:
(Main.println)(i::Any)::Void
10:
goto 3
12:
return
end::Void
could I have more than one version of Julia installed? (wanna know before crashing everything)
I am not the perfect person to answer this question. I only know, in Linux, you can download the binaries for various versions, but I’m not sure whether the packages of various versions are by default installed at the same place (hopefully not).
julia> function closureB(x::Int64)::Function
### the only difference with closureA()
if x < 0
error("abc")
end
### the only difference with closureB()
oneto3 = Base.OneTo(3)
local f
let oneto3 = oneto3
function f()
for i in oneto3
println(i)
end
end
end
return f
end
closureB (generic function with 1 method)
julia> gg = closureB(1)
(::f) (generic function with 1 method)
julia> @code_warntype gg()
Variables:
#self#::#f#6{Base.OneTo{Int64}}
i::Int64
#temp#::Int64
Body:
begin
SSAValue(0) = (Core.getfield)(#self#::#f#6{Base.OneTo{Int64}}, Symbol("#4#oneto3"))::Base.OneTo{Int64}
#temp#::Int64 = 1
3:
unless (Base.not_int)((#temp#::Int64 === (Base.add_int)((Core.getfield)(SSAValue(0), :stop)::Int64, 1)::Int64)::Bool)::Bool goto 18
SSAValue(3) = #temp#::Int64
SSAValue(4) = (Base.add_int)(#temp#::Int64, 1)::Int64
i::Int64 = SSAValue(3)
#temp#::Int64 = SSAValue(4) # line 13:
$(Expr(:inbounds, false))
# meta: location coreio.jl println 5
SSAValue(2) = (Core.typeassert)(Base.STDOUT, Base.IO)::IO
# meta: pop location
$(Expr(:inbounds, :pop))
(Base.print)(SSAValue(2), i::Int64, $(QuoteNode('\n')))::Void
16:
goto 3
18:
return
end::Void