Skip to content

Commit

Permalink
fix function syntax using both where and return type
Browse files Browse the repository at this point in the history
also fix #20172
  • Loading branch information
JeffBezanson committed Feb 1, 2017
1 parent 567b7c2 commit c8d870d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
19 changes: 14 additions & 5 deletions src/ast.scm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@
(define (deparse-arglist l (sep ",")) (string.join (map deparse l) sep))

(define (deparse e)
(define (block-stmts e)
(if (and (pair? e) (eq? (car e) 'block))
(cdr e)
(list e)))
(define (deparse-block head lst)
(string head "\n"
(string.join (map (lambda (ex) (string " " (deparse ex)))
lst)
"\n")
"\nend"))
(cond ((or (symbol? e) (number? e)) (string e))
((string? e) (print-to-string e))
((eq? e #t) "true")
Expand Down Expand Up @@ -67,17 +77,16 @@
(string "# line " (cadr e))
(string "# " (caddr e) ", line " (cadr e))))
((block)
(string "begin\n"
(string.join (map (lambda (ex) (string " " (deparse ex)))
(cdr e))
"\n")
"\nend"))
(deparse-block "begin" (cdr e)))
((comprehension)
(string "[ " (deparse (cadr e)) " for " (deparse-arglist (cddr e) ", ") " ]"))
((generator)
(string "(" (deparse (cadr e)) " for " (deparse-arglist (cddr e) ", ") ")"))
((where)
(string (deparse (cadr e)) " where " (deparse (caddr e))))
((function for while)
(deparse-block (string (car e) " " (deparse (cadr e)))
(block-stmts (caddr e))))
(else
(string e))))))

Expand Down
7 changes: 4 additions & 3 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -954,7 +954,7 @@
(where (if (and (pair? name) (eq? (car name) 'where))
(let ((w (flatten-where-expr name)))
(begin0 (cddr w)
(if (not (and (pair? (cadr w)) (eq? (caadr w) 'call)))
(if (not (and (pair? (cadr w)) (memq (caadr w) '(call |::|))))
(error (string "invalid assignment location \"" (deparse name) "\"")))
(set! name (cadr w))))
#f))
Expand Down Expand Up @@ -1000,7 +1000,8 @@
#f name)))
(expand-forms
(method-def-expr name sparams argl (caddr e) isstaged rett))))
(else e))))
(else
(error (string "invalid assignment location \"" (deparse name) "\""))))))

;; handle ( )->( ) function expressions. blocks `(a;b=1)` on the left need to be
;; converted to argument lists with kwargs.
Expand Down Expand Up @@ -3553,7 +3554,7 @@ f(x) = yt(x)
((...)
(error "\"...\" expression outside call"))
(else
(error (string "unhandled expr " e))))))
(error (string "invalid syntax " (deparse e)))))))
;; introduce new slots for assigned arguments
(for-each (lambda (v)
(if (vinfo:asgn v)
Expand Down
7 changes: 7 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4179,6 +4179,13 @@ function f17613_2(x)::Float64
end
@test isa(f17613_2(1), Float64)

# return type decl with `where`
function where1090(x::Array{T})::T where T<:Real
return x[1] + 2.0
end
@test where1090([4]) === 6
@test_throws MethodError where1090(String[])

type A1090 end
Base.convert(::Type{Int}, ::A1090) = "hey"
f1090()::Int = A1090()
Expand Down

0 comments on commit c8d870d

Please sign in to comment.