Hi, the following code is not correctly inferred in julia 1.7.0.
function test()
k = 1
if k == 1
k = 2
end
return findall(x -> x >= k, [1])
end
julia> @code_warntype(test())
MethodInstance for test()
from test() in Main at none:1
Arguments
#self#::Core.Const(test)
Locals
#17::var"#17#18"
k@_3::Core.Box
k@_4::Union{}
Body::Any
1 ─ Core.NewvarNode(:(#17))
│ (k@_3 = Core.Box())
│ Core.setfield!(k@_3, :contents, 1)
│ %4 = Core.isdefined(k@_3, :contents)::Bool
└── goto #3 if not %4
2 ─ goto #4
3 ─ Core.NewvarNode(:(k@_4))
└── k@_4
4 ┄ %9 = Core.getfield(k@_3, :contents)::Any
│ %10 = (%9 == 1)::Any
└── goto #6 if not %10
5 ─ Core.setfield!(k@_3, :contents, 2)
6 ┄ (#17 = %new(Main.:(var"#17#18"), k@_3))
│ %14 = #17::var"#17#18"
│ %15 = Base.vect(1)::Vector{Int64}
│ %16 = Main.findall(%14, %15)::Any
└── return %16
It is correctly inferred in julia 1.6.1 to an Int64[]
.
Is it expected or should I file an issue ?
Hi @etienne_dg , I am getting correct type inferencing with this function on my Mac (macOS Catalina 10.15.7).
Here is the output and details of my Julia installation:
➜ julia
_
_ _ _(_)_ | Documentation: https://docs.julialang.org
(_) | (_) (_) |
_ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
| | | | | | |/ _` | |
| | |_| | | | (_| | | Version 1.7.0 (2021-11-30)
_/ |\__'_|_|_|\__'_| | Official https://julialang.org/ release
|__/ |
julia> function test()
k = 1
if k == 1
k = 2
end
return findall(x -> x >= k, [1])
end
test (generic function with 1 method)
julia> test() |> typeof
Vector{Int64} (alias for Array{Int64, 1})
Also, I get this as the output of the function
julia> test()
Int64[]
1 Like
Strange, I built this as a MWE of failing tests in Graphs.jl, and it fails for every OS.
I tested my example on Ubuntu 20.04
What do you get if you try @code_warntype(test())
or @inferred test()
?
I get the same as you with :
julia> test() |> typeof
Vector{Int64} (alias for Array{Int64, 1})
This is not about findall
but captured values in closures.
1 Like
I thought about it but it was working on julia 1.6, what is going on 1.7 ?