Skip to content

Bug in @view: begin indexing vs. replace_ref_begin_end! #41630

@phipsgabler

Description

@phipsgabler

I thought Base.replace_ref_begin_end! is used to implement begin indexing, but appearently they are inconsistent?

Reading the source of it, I stumbled upon this line in views.jl:

exj, used = replace_ref_begin_end_!(ex.args[j], (:($firstindex($S)),:($lastindex($S,$n))))

Shouldn't that be $firstindex($S, $n)? I tried producing a failing example using OffsetArrays, but that only showed some sort of discrepancy, which I can't explain:

julia> a
2×2 OffsetArray(::Array{Int64,2}, -10:-9, 9:10) with eltype Int64 with indices -10:-9×9:10:
 1  2
 3  4

julia> a[end, begin]
3

julia> Meta.@lower a[end, begin]
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope'
1 ─ %1 = Base.lastindex(a, 1)
│   %2 = Base.axes(a, 2)
│   %3 = Base.first(%2)
│   %4 = Base.getindex(a, %1, %3)
└──      return %4
))))

julia> Base.replace_ref_begin_end!(:(a[end, begin]))
:(a[(lastindex)(a, 1), (firstindex)(a)])

julia> a[(lastindex)(a, 1), (firstindex)(a)]
ERROR: BoundsError: attempt to access 2×2 OffsetArray(::Array{Int64,2}, -10:-9, 9:10) with eltype Int64 with indices -10:-9×9:10 at index [-9, 1]
Stacktrace:
 [1] throw_boundserror(::OffsetArray{Int64,2,Array{Int64,2}}, ::Tuple{Int64,Int64}) at ./abstractarray.jl:541
 [2] checkbounds at ./abstractarray.jl:506 [inlined]
 [3] getindex(::OffsetArray{Int64,2,Array{Int64,2}}, ::Int64, ::Int64) at /home/philipp/.julia/packages/OffsetArrays/Osysv/src/OffsetArrays.jl:407
 [4] top-level scope at REPL[22]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions