-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Closed
Description
julia> function f(r)
a, b, c = r..., nothing
end
f (generic function with 1 method)
julia> @code_warntype f(1:2)
MethodInstance for f(::UnitRange{Int64})
from f(r) in Main at REPL[1]:1
Arguments
#self#::Core.Const(f)
r::UnitRange{Int64}
Locals
@_3::Int64
c::Any
b::Any
a::Any
Body::Tuple
1 ─ %1 = Core.tuple(Main.nothing)::Core.Const((nothing,))
│ %2 = Core._apply_iterate(Base.iterate, Core.tuple, r, %1)::Tuple
│ %3 = Base.indexed_iterate(%2, 1)::Core.PartialStruct(Tuple{Any, Int64}, Any[Any, Core.Const(2)])
│ (a = Core.getfield(%3, 1))
│ (@_3 = Core.getfield(%3, 2))
│ %6 = Base.indexed_iterate(%2, 2, @_3::Core.Const(2))::Core.PartialStruct(Tuple{Any, Int64}, Any[Any, Core.Const(3)])
│ (b = Core.getfield(%6, 1))
│ (@_3 = Core.getfield(%6, 2))
│ %9 = Base.indexed_iterate(%2, 3, @_3::Core.Const(3))::Core.PartialStruct(Tuple{Any, Int64}, Any[Any, Core.Const(4)])
│ (c = Core.getfield(%9, 1))
└── return %2
The RHS is inferred as a Tuple
, although it's really a Tuple{Vararg{Union{Int, Nothing}}}
In this case the variables may at least be inferred as Union{Int, Nothing}
. Ideally a
and b
may be inferred as Int
s as the function will error if there are less than three elements on the right hand side.
julia> f(1:1)
ERROR: BoundsError: attempt to access Tuple{Int64, Nothing} at index [3]
Edit: A simpler example:
julia> function f(r)
a, b, c = r..., 1
end
f (generic function with 1 method)
julia> @code_warntype f(1:2)
Variables
#self#::Core.Const(f)
r::UnitRange{Int64}
@_3::Int64
c::Any
b::Any
a::Any
Body::Tuple
1 ─ %1 = Core.tuple(1)::Core.Const((1,))
│ %2 = Core._apply_iterate(Base.iterate, Core.tuple, r, %1)::Tuple
│ %3 = Base.indexed_iterate(%2, 1)::Core.PartialStruct(Tuple{Any, Int64}, Any[Any, Core.Const(2)])
│ (a = Core.getfield(%3, 1))
│ (@_3 = Core.getfield(%3, 2))
│ %6 = Base.indexed_iterate(%2, 2, @_3::Core.Const(2))::Core.PartialStruct(Tuple{Any, Int64}, Any[Any, Core.Const(3)])
│ (b = Core.getfield(%6, 1))
│ (@_3 = Core.getfield(%6, 2))
│ %9 = Base.indexed_iterate(%2, 3, @_3::Core.Const(3))::Core.PartialStruct(Tuple{Any, Int64}, Any[Any, Core.Const(4)])
│ (c = Core.getfield(%9, 1))
└── return %2
In this case the variables must all be Int
s
Metadata
Metadata
Assignees
Labels
No labels