Skip to content

Commit

Permalink
Fix firstindex in replace_ref_begin_end (fixes #41630) (#41695)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2ebbb2b)
  • Loading branch information
phipsgabler authored and KristofferC committed Aug 31, 2021
1 parent 5ea7af3 commit 06e232b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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
21 changes: 21 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,24 @@ end
strY = String(take!(io))
@test strX == strY
end

@testset "issue #41630: replace_ref_begin_end!/@view on offset-like arrays" begin
x = OffsetArray([1 2; 3 4], -10:-9, 9:10) # 2×2 OffsetArray{...} with indices -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

0 comments on commit 06e232b

Please sign in to comment.