Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove unnecessary true && part from at-view macro #53064

Merged
merged 2 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 12 additions & 11 deletions base/views.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,20 +123,21 @@ julia> A
```
"""
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 = replace_ref_begin_end!(ex)
if Meta.isexpr(ex, :ref)
ex = Expr(:call, view, ex.args...)
else # ex replaced by let ...; foo[...]; end
if !(Meta.isexpr(ex, :let) && Meta.isexpr(ex.args[2], :ref))
error("invalid expression")
end
ex.args[2] = Expr(:call, view, ex.args[2].args...)
end
Expr(:&&, true, esc(ex))
ex = Expr(:call, view, ex.args...)
stevengj marked this conversation as resolved.
Show resolved Hide resolved
elseif Meta.isexpr(ex, :let) && (arg2 = ex.args[2]; Meta.isexpr(arg2, :ref))
# ex replaced by let ...; foo[...]; end
ex.args[2] = Expr(:call, view, arg2.args...)
else
throw(ArgumentError("Invalid use of @view macro: argument must be a reference expression A[...]."))
error("invalid expression")
end
return esc(ex)
end

############################################################################
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