Skip to content

Commit

Permalink
fix JuliaLang#34516, several issues with nospecialize on keyword argu…
Browse files Browse the repository at this point in the history
…ments (JuliaLang#36019)

- `at-nospecialize(a)` syntax was not allowed as a kwarg
- include nospecialize annotations in the keyword sorter method
- we were dropping nospecialize applied to keyword arguments
  • Loading branch information
JeffBezanson authored and simeonschaub committed Aug 11, 2020
1 parent 599593d commit 6f4f078
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
22 changes: 14 additions & 8 deletions src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@
(let loop ((stmts body))
(if (eq? functionloc (cadr stmts))
(set-cdr! stmts (cddr stmts))
(loop (cdr body)))))
(loop (cdr stmts)))))
functionloc))

;; construct the (method ...) expression for one primitive method definition,
Expand Down Expand Up @@ -418,6 +418,8 @@

(define (keywords-method-def-expr name sparams argl body rett)
(let* ((kargl (cdar argl)) ;; keyword expressions (= k v)
(annotations (map (lambda (a) `(meta ,(cadr a) ,(arg-name (cadr (caddr a)))))
(filter nospecialize-meta? kargl)))
(kargl (map (lambda (a)
(if (nospecialize-meta? a) (caddr a) a))
kargl))
Expand Down Expand Up @@ -457,8 +459,6 @@
keynames))
;; list of function's initial line number and meta nodes (empty if none)
(prologue (extract-method-prologue body))
(annotations (map (lambda (a) `(meta ,(cadr a) ,(arg-name (cadr (caddr a)))))
(filter nospecialize-meta? kargl)))
;; body statements
(stmts (cdr body))
(positional-sparams (filter-sparams (cons 'list pargl-all) sparams))
Expand Down Expand Up @@ -519,6 +519,11 @@
(call (core kwftype) ,ftype)) ,kw ,@pargl ,@vararg)
`(block
,@(filter linenum? prologue)
;; nospecialize meta for just positional args
,@(map (lambda (m)
`(meta ,(cadr m) ,@(filter (lambda (v) (not (memq v keynames)))
(cddr m))))
(filter nospecialize-meta? prologue))
,(scopenest
keynames
(map (lambda (v dflt)
Expand Down Expand Up @@ -653,13 +658,14 @@
(define (throw-unassigned-kw-args argl)
(define (throw-unassigned argname)
`(call (core throw) (call (core UndefKeywordError) (inert ,argname))))
(define (to-kw x)
(cond ((symbol? x) `(kw ,x ,(throw-unassigned x)))
((decl? x) `(kw ,x ,(throw-unassigned (cadr x))))
((nospecialize-meta? x) `(meta ,(cadr x) ,(to-kw (caddr x))))
(else x)))
(if (has-parameters? argl)
(cons (cons 'parameters
(map (lambda (x)
(cond ((symbol? x) `(kw ,x ,(throw-unassigned x)))
((decl? x) `(kw ,x ,(throw-unassigned (cadr x))))
(else x)))
(cdar argl)))
(map to-kw (cdar argl)))
(cdr argl))
argl))

Expand Down
10 changes: 9 additions & 1 deletion test/keywordargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,18 @@ end
@test g21147(((1,),), 2) === Int
end
@testset "issue #21510" begin
f21510(; Base.@nospecialize a = 2) = a
f21510(; @nospecialize a = 2) = a
@test f21510(a=:b) == :b
@test f21510() == 2
end
@testset "issue #34516" begin
f34516(; @nospecialize(x)) = 0
f34516(y; @nospecialize(x::Any)) = 1
@test_throws UndefKeywordError f34516()
@test_throws UndefKeywordError f34516(1)
g34516(@nospecialize(x); k=0) = 0
@test first(methods(Core.kwfunc(g34516))).nospecialize != 0
end
@testset "issue #21518" begin
a = 0
f21518(;kw=nothing) = kw
Expand Down

0 comments on commit 6f4f078

Please sign in to comment.