diff --git a/base/subarray.jl b/base/subarray.jl index e9e184648028b..c4c3ae0c503a8 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -408,7 +408,7 @@ function replace_ref_end_!(ex, withex) n = 1 J = lastindex(ex.args) for j = 2:J - exj, used = replace_ref_end_!(ex.args[j],:($size($S,$n))) + exj, used = replace_ref_end_!(ex.args[j],:($lastindex($S,$n))) used_S |= used ex.args[j] = exj if isa(exj,Expr) && exj.head == :... @@ -499,7 +499,7 @@ end # _views implements the transformation for the @views macro. # @views calls esc(_views(...)) to work around #20241, # so any function calls we insert (to maybeview, or to -# size and lastindex in replace_ref_end!) must be interpolated +# lastindex in replace_ref_end!) must be interpolated # as values rather than as symbols to ensure that they are called # from Base rather than from the caller's scope. _views(x) = x diff --git a/test/arrayops.jl b/test/arrayops.jl index df224959f76c6..9db8053080a90 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -2324,3 +2324,20 @@ end inds_b = Base.Indices{1}([1:3]) @test Base.promote_shape(inds_a, inds_b) == Base.promote_shape(inds_b, inds_a) end + +struct T25958 +end +Base.lastindex(::T25958, args...) = (:lastindex, args...) +Base.getindex(::T25958, args...) = args +Base.view(::T25958, args...) = args +@testset "ensure @view and @views matches lowering" begin + t = T25958() + @test t[end] == @view(t[end]) == @views t[end] + @test t[1,end] == @view(t[1,end]) == @views t[1,end] + @test t[end,1] == @view(t[end,1]) == @views t[end,1] + @test t[end,end] == @view(t[end,end]) == @views t[end,end] + @test t[1,end,end] == @view(t[1,end,end]) == @views t[1,end,end] + @test t[end,1,end] == @view(t[end,1,end]) == @views t[end,1,end] + @test t[end,end,1] == @view(t[end,end,1]) == @views t[end,end,1] + @test t[end,end,end] == @view(t[end,end,end]) == @views t[end,end,end] +end