Skip to content

Commit

Permalink
add comment and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Jan 27, 2024
1 parent f6a5f87 commit 63ee520
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ macro view(ex)
Meta.isexpr(ex, :ref) || throw(ArgumentError(
"Invalid use of @view macro: argument must be a reference expression A[...]."))
ex = replace_ref_begin_end!(ex)
# NOTE We embed `view` as a function object itself directly into the AST.
# By doing this, we prevent the creation of function definitions like
# `view(A, idx) = xxx` in cases such as `@view(A[idx]) = xxx.`
if Meta.isexpr(ex, :ref)
ex = Expr(:call, view, ex.args...)
elseif Meta.isexpr(ex, :let) && (arg2 = ex.args[2]; Meta.isexpr(arg2, :ref))
Expand Down
18 changes: 18 additions & 0 deletions test/subarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -937,3 +937,21 @@ end
v = view(1:2, r)
@test v == view(1:2, collect(r))
end

# https://github.com/JuliaLang/julia/pull/53064
# `@view(A[idx]) = xxx` should raise syntax error always
@test try
Core.eval(@__MODULE__, :(@view(A[idx]) = 2))
false
catch err
err isa ErrorException && startswith(err.msg, "syntax:")
end
module Issue53064
import Base: view
end
@test try
Core.eval(Issue53064, :(@view(A[idx]) = 2))
false
catch err
err isa ErrorException && startswith(err.msg, "syntax:")
end

0 comments on commit 63ee520

Please sign in to comment.