Skip to content

Commit

Permalink
fix hygiene and side-effects problems in at-view
Browse files Browse the repository at this point in the history
(cherry picked from commit f225d82)
ref #20247
  • Loading branch information
stevengj authored and tkelman committed Mar 1, 2017
1 parent 1512ae0 commit bd26938
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 14 deletions.
42 changes: 31 additions & 11 deletions base/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,25 +320,32 @@ should transform to
A[B[endof(B)]]
"""
function replace_ref_end!(ex,withex=nothing)
replace_ref_end!(ex) = replace_ref_end_!(ex, nothing)[1]
# replace_ref_end_!(ex,withex) returns (new ex, whether withex was used)
function replace_ref_end_!(ex, withex)
used_withex = false
if isa(ex,Symbol) && ex == :end
withex === nothing && error("Invalid use of end")
return withex
return withex, true
elseif isa(ex,Expr)
if ex.head == :ref
S = ex.args[1] = replace_ref_end!(ex.args[1],withex)
ex.args[1], used_withex = replace_ref_end_!(ex.args[1],withex)
S = isa(ex.args[1],Symbol) ? ex.args[1]::Symbol : gensym(:S) # temp var to cache ex.args[1] if needed
used_S = false # whether we actually need S
# new :ref, so redefine withex
nargs = length(ex.args)-1
if nargs == 0
return ex
return ex, used_withex
elseif nargs == 1
# replace with endof(S)
ex.args[2] = replace_ref_end!(ex.args[2],:(Base.endof($S)))
ex.args[2], used_S = replace_ref_end_!(ex.args[2],:($endof($S)))
else
n = 1
J = endof(ex.args)
for j = 2:J-1
exj = ex.args[j] = replace_ref_end!(ex.args[j],:(Base.size($S,$n)))
exj, used = replace_ref_end_!(ex.args[j],:($size($S,$n)))
used_S |= used
ex.args[j] = exj
if isa(exj,Expr) && exj.head == :...
# splatted object
exjs = exj.args[1]
Expand All @@ -351,16 +358,23 @@ function replace_ref_end!(ex,withex=nothing)
n += 1
end
end
ex.args[J] = replace_ref_end!(ex.args[J],:(Base.trailingsize($S,$n)))
ex.args[J], used = replace_ref_end_!(ex.args[J],:($trailingsize($S,$n)))
used_S |= used
end
if used_S && S !== ex.args[1]
S0 = ex.args[1]
ex.args[1] = S
ex = Expr(:let, ex, :($S = $S0))
end
else
# recursive search
for i = eachindex(ex.args)
ex.args[i] = replace_ref_end!(ex.args[i],withex)
ex.args[i], used = replace_ref_end_!(ex.args[i],withex)
used_withex |= used
end
end
end
ex
ex, used_withex
end

"""
Expand All @@ -371,9 +385,15 @@ reference expression (e.g. `@view A[1,2:end]`), and should *not* be used as the
an assignment (e.g. `@view(A[1,2:end]) = ...`).
"""
macro view(ex)
if isa(ex, Expr) && ex.head == :ref
if Meta.isexpr(ex, :ref)
ex = replace_ref_end!(ex)
Expr(:&&, true, esc(Expr(:call,:(Base.view),ex.args...)))
if Meta.isexpr(ex, :ref)
ex = Expr(:call, view, ex.args...)
else # ex replaced by let ...; foo[...]; end
assert(Meta.isexpr(ex, :let) && Meta.isexpr(ex.args[1], :ref))
ex.args[1] = Expr(:call, view, ex.args[1].args...)
end
Expr(:&&, true, esc(ex))
else
throw(ArgumentError("Invalid use of @view macro: argument must be a reference expression A[...]."))
end
Expand Down
10 changes: 7 additions & 3 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,6 @@ end
@test collect(view(view(reshape(1:13^3, 13, 13, 13), 3:7, 6:6, :), 1:2:5, :, 1:2:5)) ==
cat(3,[68,70,72],[406,408,410],[744,746,748])



# tests @view (and replace_ref_end!)
X = reshape(1:24,2,3,4)
Y = 4:-1:1
Expand All @@ -481,10 +479,16 @@ u = (1,2:3)
@test X[(1,)...,(2,)...,2:end] == @view X[(1,)...,(2,)...,2:end]

# test macro hygiene
let size=(x,y)-> error("should not happen")
let size=(x,y)-> error("should not happen"), Base=nothing
@test X[1:end,2,2] == @view X[1:end,2,2]
end

# test that side effects occur only once
let foo = [X]
@test X[2:end-1] == @view (push!(foo,X)[1])[2:end-1]
@test foo == [X, X]
end

# issue #18034
# ensure that it is possible to create an isbits, LinearFast view of an immutable Array
let
Expand Down

0 comments on commit bd26938

Please sign in to comment.