From 6feb73455192442b11afa7d3930741056a34cb90 Mon Sep 17 00:00:00 2001 From: Philipp Gabler Date: Wed, 11 Aug 2021 22:01:06 +0200 Subject: [PATCH] Fix firstindex in replace_ref_begin_end (fixes #41630) (#41695) (cherry picked from commit 2ebbb2b3a0b54eae66549bd058ae334cb3642e50) --- base/views.jl | 2 +- test/offsetarray.jl | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/base/views.jl b/base/views.jl index ccf24d4cdea3a7..153b5d57004d84 100644 --- a/base/views.jl +++ b/base/views.jl @@ -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 === :... diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 5deb442f36222f..362aada64fe701 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -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