Skip to content

eachslice is type unstable #39639

@ariroffe

Description

@ariroffe
julia> a = [1 2; 3 4]

julia> function f1(arr)
           for x in eachcol(arr) end
       end

julia> function f2(arr)
           for x in eachslice(arr; dims=2) end
       end

Now, if I do:

julia> @code_warntype f1(a)
Variables
  #self#::Core.Compiler.Const(f1, false)
  arr::Array{Int64,2}
  @_3::Union{Nothing, Tuple{SubArray{Int64,1,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true},Int64}}
  x::SubArray{Int64,1,Array{Int64,2},Tuple{Base.Slice{Base.OneTo{Int64}},Int64},true}
(...)

However:

julia> @code_warntype f2(a)
Variables
  #self#::Core.Compiler.Const(f2, false)
  arr::Array{Int64,2}
  @_3::Union{Nothing, Tuple{Any,Int64}}
  x::Any
(...)

Shouldn't these two functions be equivalent in terms of type stability, and the compiler know that x is a SubArray in the second case?
It isn't just this case though, eachslice seems to have the same behavior for other array eltypes and dimensions.
Sorry if this isn't an issue, I'm somewhat new to Julia.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions