Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix firstindex in replace_ref_begin_end (fixes #41630) #41695

Merged
merged 5 commits into from
Aug 11, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion base/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function replace_ref_begin_end_!(ex, withex)
n = 1
J = lastindex(ex.args)
for j = 2:J
exj, used = replace_ref_begin_end_!(ex.args[j], (:($firstindex($S)),:($lastindex($S,$n))))
exj, used = replace_ref_begin_end_!(ex.args[j], (:($firstindex($S,$n)),:($lastindex($S,$n))))
used_S |= used
ex.args[j] = exj
if isa(exj,Expr) && exj.head === :...
Expand Down
38 changes: 38 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,44 @@ end
end
end

@testset "issue #41630: replace_ref_begin_end!/@view on offset-like arrays" begin
# recreate minimal version of OffsetArrays.jl,
# see https://github.com/JuliaArrays/OffsetArrays.jl/blob/master/src/OffsetArrays.jl
struct OffsetArray{T,N,AA<:AbstractArray{T,N}} <: AbstractArray{T,N}
parent::AA
offsets::NTuple{N,Int}
end
Base.IndexStyle(::Type{<:OffsetArray{<:Any,<:Any,AA}}) where {AA} = IndexStyle(AA)
Base.parent(A::OffsetArray) = A.parent
Base.size(A::OffsetArray) = size(parent(A))
Base.axes(A::OffsetArray) = map((ax, o) -> ax .+ o, axes(parent(A)), A.offsets)
Base.getindex(A::OffsetArray, i::Int...) = parent(A)[CartesianIndex(i .- A.offsets)]

x = OffsetArray([1 2; 3 4], (-11, 8)) # 2×2 OffsetArray{...} with indices -10:-9×9:10

# implementation correct?
@test collect(x) == [1 2; 3 4]
@test size(x) == (2, 2)
@test axes(x) == (-10:-9, 9:10)

# begin/end with offset indices
@test (@view x[begin, 9])[] == 1
@test (@view x[-10, end])[] == 2
@test (@view x[-9, begin])[] == 3
@test (@view x[end, 10])[] == 4
@test (@view x[begin, begin])[] == 1
@test (@view x[begin, end])[] == 2
@test (@view x[end, begin])[] == 3
@test (@view x[end, end])[] == 4

# nested usages of begin/end
y = OffsetArray([-10, -9], (5,))
@test (@view x[begin, -y[end]])[] == 1
@test (@view x[y[begin], end])[] == 2
@test (@view x[end, -y[end]])[] == 3
@test (@view x[y[end], end])[] == 4
end

@testset "issue #18034: an isbits, IndexLinear view of an immutable Array" begin
struct ImmutableTestArray{T, N} <: Base.DenseArray{T, N}
end
Expand Down