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

make broadcasting into getproperty extensible #39473

Merged
merged 4 commits into from
Mar 12, 2021
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
1 change: 1 addition & 0 deletions base/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ getproperty(x::Tuple, f::Int) = getfield(x, f)
setproperty!(x::Tuple, f::Int, v) = setfield!(x, f, v) # to get a decent error

getproperty(x, f::Symbol) = getfield(x, f)
dotgetproperty(x, f) = getproperty(x, f)
setproperty!(x, f::Symbol, v) = setfield!(x, f, convert(fieldtype(typeof(x), f), v))

include("coreio.jl")
Expand Down
21 changes: 13 additions & 8 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1545,7 +1545,10 @@
(define (expand-update-operator- op op= lhs rhs declT)
(let* ((e (remove-argument-side-effects lhs))
(newlhs (car e))
(temp (and (eq? op= '|.=|) (pair? newlhs) (not (eq? (car newlhs) 'ref))
(temp (and (eq? op= '|.=|)
(pair? newlhs)
(not (or (eq? (car newlhs) 'ref)
(and (eq? (car newlhs) '|.|) (length= newlhs 3))))
(make-ssavalue)))
(e (if temp
(cons temp (append (cdr e) (list `(= ,temp ,newlhs))))
Expand Down Expand Up @@ -1742,13 +1745,15 @@
gen))))

(define (ref-to-view expr)
(if (and (pair? expr) (eq? (car expr) 'ref))
(let* ((ex (partially-expand-ref expr))
(stmts (butlast (cdr ex)))
(refex (last (cdr ex)))
(nuref `(call (top dotview) ,(caddr refex) ,@(cdddr refex))))
`(block ,@stmts ,nuref))
expr))
(cond ((and (pair? expr) (eq? (car expr) 'ref))
(let* ((ex (partially-expand-ref expr))
(stmts (butlast (cdr ex)))
(refex (last (cdr ex)))
(nuref `(call (top dotview) ,(caddr refex) ,@(cdddr refex))))
`(block ,@stmts ,nuref)))
((and (length= expr 3) (eq? (car expr) '|.|))
`(call (top dotgetproperty) ,(cadr expr) ,(caddr expr)))
(else expr)))

; lazily fuse nested calls to expr == f.(args...) into a single broadcast call,
; or a broadcast! call if lhs is non-null.
Expand Down
17 changes: 16 additions & 1 deletion test/syntax.jl
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,7 @@ let ε=1, μ=2, x=3, î=4
@test µ == μ == 2
# NFC normalization of identifiers:
@test Meta.parse("\u0069\u0302") === Meta.parse("\u00ee")
@test == 4
@test î == 4
# latin vs greek ε (#14751)
@test Meta.parse("\u025B") === Meta.parse("\u03B5")
@test ɛ == ε == 1
Expand Down Expand Up @@ -2727,6 +2727,21 @@ end
@eval f39705(x) = $(Expr(:||)) && x
@test f39705(1) === false


struct A x end
Base.dotgetproperty(::A, ::Symbol) = [0, 0, 0]

@testset "dotgetproperty" begin
a = (x = [1, 2, 3],)
@test @inferred((a -> a.x .+= 1)(a)) == [2, 3, 4]

b = [1, 2, 3]
@test A(b).x === b
@test begin A(b).x .= 1 end == [1, 1, 1]
@test begin A(b).x .+= 1 end == [2, 3, 4]
@test b == [1, 2, 3]
end

@test Meta.@lower((::T) = x) == Expr(:error, "invalid assignment location \"::T\"")
@test Meta.@lower((::T,) = x) == Expr(:error, "invalid assignment location \"::T\"")
@test Meta.@lower((; ::T) = x) == Expr(:error, "invalid assignment location \"::T\"")
Expand Down