Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #34516, several issues with nospecialize on keyword arguments #36019

Merged
merged 1 commit into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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