Skip to content

Commit

Permalink
Merge pull request #21195 from JuliaLang/jb/fix21172
Browse files Browse the repository at this point in the history
fix #21172, `a = f(x) = 1`
  • Loading branch information
JeffBezanson authored Mar 28, 2017
2 parents 4809620 + fb67034 commit 85f37bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
31 changes: 18 additions & 13 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -1830,32 +1830,37 @@
'=
(lambda (e)
(define lhs (cadr e))
(cond
((and (pair? lhs)
(or (eq? (car lhs) 'call)
(define (function-lhs? lhs)
(and (pair? lhs)
(or (and (eq? (car lhs) 'comparison) (length= lhs 4))
(eq? (car lhs) 'call)
(eq? (car lhs) 'where)
(and (eq? (car lhs) '|::|)
(pair? (cadr lhs))
(eq? (car (cadr lhs)) 'call))))
(expand-forms (cons 'function (cdr e))))
((and (pair? lhs)
(eq? (car lhs) 'comparison)
(length= lhs 4))
;; allow defining functions that use comparison syntax
(expand-forms (list* 'function
`(call ,(caddr lhs) ,(cadr lhs) ,(cadddr lhs)) (cddr e))))
(eq? (car (cadr lhs)) 'call)))))
(define (assignment-to-function lhs e) ;; convert '= expr to 'function expr
(if (eq? (car lhs) 'comparison)
;; allow defining functions that use comparison syntax
(list* 'function
`(call ,(caddr lhs) ,(cadr lhs) ,(cadddr lhs)) (cddr e))
(cons 'function (cdr e))))
(cond
((function-lhs? lhs)
(expand-forms (assignment-to-function lhs e)))
((and (pair? lhs)
(eq? (car lhs) 'curly))
(expand-typealias (cadr e) (caddr e)))
((assignment? (caddr e))
;; chain of assignments - convert a=b=c to `b=c; a=c`
(let loop ((lhss (list lhs))
(rhs (caddr e)))
(if (assignment? rhs)
(if (and (assignment? rhs) (not (function-lhs? (cadr rhs))))
(loop (cons (cadr rhs) lhss) (caddr rhs))
(let ((rr (if (symbol-like? rhs) rhs (make-ssavalue))))
(expand-forms
`(block ,.(if (eq? rr rhs) '() `((= ,rr ,rhs)))
`(block ,.(if (eq? rr rhs) '() `((= ,rr ,(if (assignment? rhs)
(assignment-to-function (cadr rhs) rhs)
rhs))))
,@(map (lambda (l) `(= ,l ,rr))
lhss)
(unnecessary ,rr)))))))
Expand Down
5 changes: 5 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4787,3 +4787,8 @@ end
struct F21178{A,B} end
b21178(::F1,::F2) where {B1,B2,F1<:F21178{B1,<:Any},F2<:F21178{B2}} = F1,F2,B1,B2
@test b21178(F21178{1,2}(),F21178{1,2}()) == (F21178{1,2}, F21178{1,2}, 1, 1)

# issue #21172
a21172 = f21172(x) = 2x
@test f21172(8) == 16
@test a21172 === f21172

0 comments on commit 85f37bc

Please sign in to comment.