Skip to content

Commit 63ee520

Browse files
committed
add comment and tests
1 parent f6a5f87 commit 63ee520

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

base/views.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,9 @@ macro view(ex)
126126
Meta.isexpr(ex, :ref) || throw(ArgumentError(
127127
"Invalid use of @view macro: argument must be a reference expression A[...]."))
128128
ex = replace_ref_begin_end!(ex)
129+
# NOTE We embed `view` as a function object itself directly into the AST.
130+
# By doing this, we prevent the creation of function definitions like
131+
# `view(A, idx) = xxx` in cases such as `@view(A[idx]) = xxx.`
129132
if Meta.isexpr(ex, :ref)
130133
ex = Expr(:call, view, ex.args...)
131134
elseif Meta.isexpr(ex, :let) && (arg2 = ex.args[2]; Meta.isexpr(arg2, :ref))

test/subarray.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -937,3 +937,21 @@ end
937937
v = view(1:2, r)
938938
@test v == view(1:2, collect(r))
939939
end
940+
941+
# https://github.com/JuliaLang/julia/pull/53064
942+
# `@view(A[idx]) = xxx` should raise syntax error always
943+
@test try
944+
Core.eval(@__MODULE__, :(@view(A[idx]) = 2))
945+
false
946+
catch err
947+
err isa ErrorException && startswith(err.msg, "syntax:")
948+
end
949+
module Issue53064
950+
import Base: view
951+
end
952+
@test try
953+
Core.eval(Issue53064, :(@view(A[idx]) = 2))
954+
false
955+
catch err
956+
err isa ErrorException && startswith(err.msg, "syntax:")
957+
end

0 commit comments

Comments
 (0)