From fb670345366611421b2e04212289d44f69b75096 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Tue, 28 Mar 2017 11:15:28 -0400 Subject: [PATCH] fix #21172, `a = f(x) = 1` --- src/julia-syntax.scm | 31 ++++++++++++++++++------------- test/core.jl | 5 +++++ 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 612310f9d8951..f70368039b002 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -1830,20 +1830,23 @@ '= (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))) @@ -1851,11 +1854,13 @@ ;; 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))))))) diff --git a/test/core.jl b/test/core.jl index 445a1fec4f5df..f3c42b0875cfb 100644 --- a/test/core.jl +++ b/test/core.jl @@ -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