Skip to content

Commit

Permalink
lower prologue attributes to the kwarg function definition sans keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Oct 3, 2016
1 parent c7891be commit a72753e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 19 deletions.
3 changes: 0 additions & 3 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ end
@pure function rationalize{T<:Integer}(::Type{T}, x::Irrational; tol::Real=0)
return rationalize(T, big(x), tol=tol)
end
@pure function rationalize{T<:Integer}(::Type{T}, x::Irrational)
return rationalize(T, big(x), tol=0)
end
@pure function lessrational{T<:Integer}(rx::Rational{T}, x::Irrational)
# an @pure version of `<` for determining if the rationalization of
# an irrational number required rounding up or down
Expand Down
31 changes: 15 additions & 16 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -403,13 +403,10 @@
(lambda (x) (eq? x v))
vals))
keynames))
;; 1-element list of function's line number node, or empty if none
(lno (if (and (pair? (cdr body))
(pair? (cadr body)) (eq? (caadr body) 'line))
(list (cadr body))
'()))
;; body statements, minus line number node
(stmts (if (null? lno) (cdr body) (cddr body)))
;; list of function's initial line number and meta nodes (empty if none)
(prologue (extract-method-prologue body))
;; body statements
(stmts (cdr body))
(positional-sparams
(filter (lambda (s)
(let ((name (if (symbol? s) s (cadr s))))
Expand All @@ -434,12 +431,12 @@
,(method-def-expr-
name positional-sparams (append pargl vararg)
`(block
,@lno
,@prologue
,@(if (not ordered-defaults)
'()
(append! (map (lambda (kwname) `(local ,kwname)) keynames)
(map make-assignment keynames vals)))
;; call mangled(vals..., [rest_kw ,]pargs..., [vararg]...)
;; call mangled(vals..., [rest_kw,] pargs..., [vararg]...)
(return (call ,mangled
,@(if ordered-defaults keynames vals)
,@(if (null? restkw) '() (list empty-vector-any))
Expand All @@ -463,7 +460,6 @@
(car not-optional))
,@(cdr not-optional) ,@vararg)
`(block
,@lno
,@stmts) isstaged rett)

;; call with unsorted keyword args. this sorts and re-dispatches.
Expand Down Expand Up @@ -550,13 +546,16 @@
,(if (or (not (symbol? name)) (is-call-name? name))
'(null) name)))))

;; prologue includes line number node and eventual meta nodes
(define (extract-method-prologue body)
(if (pair? body)
(take-while (lambda (e)
(and (pair? e) (or (eq? (car e) 'line) (eq? (car e) 'meta))))
(cdr body))
'()))

(define (optional-positional-defs name sparams req opt dfl body isstaged overall-argl rett)
;; prologue includes line number node and eventual meta nodes
(let ((prologue (if (pair? body)
(take-while (lambda (e)
(and (pair? e) (or (eq? (car e) 'line) (eq? (car e) 'meta))))
(cdr body))
'())))
(let ((prologue (extract-method-prologue body)))
`(block
,@(map (lambda (n)
(let* ((passed (append req (list-head opt n)))
Expand Down
17 changes: 17 additions & 0 deletions test/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,3 +371,20 @@ f18450() = ifelse(true, Tuple{Vararg{Int}}, Tuple{Vararg})

# issue #18569
@test Core.Inference.isconstType(Type{Tuple},true)

# ensure pure attribute applies correctly to all signatures of fpure
Base.@pure function fpure(a=rand(); b=rand())
# use the `rand` function since it is known to be `@inline`
# but would be too big to inline
return a + b + rand()
end
gpure() = fpure()
gpure(x::Irrational) = fpure(x)
@test which(fpure, ()).source.pure
@test which(fpure, (typeof(pi),)).source.pure
@test !which(gpure, ()).source.pure
@test !which(gpure, (typeof(pi),)).source.pure
@test @code_typed(gpure())[1].pure
@test @code_typed(gpure(π))[1].pure
@test gpure() == gpure() == gpure()
@test gpure(π) == gpure(π) == gpure(π)

0 comments on commit a72753e

Please sign in to comment.