From 85b97525ddb576d919c73c0f88c02904acf4ac3e Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Thu, 1 Jul 2021 03:51:12 -0400 Subject: [PATCH] fix #41416, splatted default argument lost with keyword argument (#41427) (cherry picked from commit ebc8f3c61701f271f5fac33e3dac56eed786679c) --- src/ast.scm | 6 +++++- src/julia-syntax.scm | 17 ++++++++--------- test/keywordargs.jl | 7 +++++++ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/ast.scm b/src/ast.scm index 6ed530718e3db..2fc4d90fe0e75 100644 --- a/src/ast.scm +++ b/src/ast.scm @@ -306,7 +306,7 @@ (bad-formal-argument v)) (else (case (car v) - ((... kw) + ((...) (arg-name (cadr v)) ;; to check for errors (decl-var (cadr v))) ((|::|) @@ -317,6 +317,8 @@ (if (nospecialize-meta? v #t) (arg-name (caddr v)) (bad-formal-argument v))) + ((kw) + (arg-name (cadr v))) (else (bad-formal-argument v)))))) (define (arg-type v) @@ -336,6 +338,8 @@ (if (nospecialize-meta? v #t) (arg-type (caddr v)) (bad-formal-argument v))) + ((kw) + (arg-type (cadr v))) (else (bad-formal-argument v)))))) ;; convert a lambda list into a list of just symbols diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index e90b99bd4cced..9b4b6abc27f71 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -433,9 +433,12 @@ ;; 1-element list of vararg argument, or empty if none (vararg (let* ((l (if (null? pargl) '() (last pargl))) ;; handle vararg with default value - (l (if (kwarg? l) (cadr l) l))) - (if (or (vararg? l) (varargexpr? l)) + (l- (if (kwarg? l) (cadr l) l))) + (if (or (vararg? l-) (varargexpr? l-)) (list l) '()))) + ;; expression to forward varargs to another call + (splatted-vararg (if (null? vararg) '() + (list `(... ,(arg-name (car vararg)))))) ;; positional args with vararg (pargl-all pargl) ;; positional args without vararg @@ -510,8 +513,7 @@ ,@(if ordered-defaults keynames vals) ,@(if (null? restkw) '() `((call (top pairs) (call (core NamedTuple))))) ,@(map arg-name pargl) - ,@(if (null? vararg) '() - (list `(... ,(arg-name (car vararg))))))))) + ,@splatted-vararg)))) (if ordered-defaults (scopenest keynames vals ret) ret)))) @@ -569,16 +571,13 @@ ,@(if (null? restkw) `((if (call (top isempty) ,rkw) (null) - (call (top kwerr) ,kw ,@(map arg-name pargl) - ,@(if (null? vararg) '() - (list `(... ,(arg-name (car vararg)))))))) + (call (top kwerr) ,kw ,@(map arg-name pargl) ,@splatted-vararg))) '()) (return (call ,mangled ;; finally, call the core function ,@keynames ,@(if (null? restkw) '() (list rkw)) ,@(map arg-name pargl) - ,@(if (null? vararg) '() - (list `(... ,(arg-name (car vararg))))))))))) + ,@splatted-vararg)))))) ;; return primary function ,(if (not (symbol? name)) '(null) name)))))) diff --git a/test/keywordargs.jl b/test/keywordargs.jl index 348eaea2ee4c3..9cbae2b1a0b19 100644 --- a/test/keywordargs.jl +++ b/test/keywordargs.jl @@ -380,3 +380,10 @@ f40964(xs::Int...=1; k = 2) = (xs, k) @test f40964() === ((1,), 2) @test f40964(7, 8) === ((7,8), 2) @test f40964(7, 8, k=0) === ((7,8), 0) +# issue #41416 +@test f40964(; k = 1) === ((1,), 1) +f41416(a...="a"; b=true) = (b, a) +@test f41416() === (true, ("a",)) +@test f41416(;b=false) === (false, ("a",)) +@test f41416(33) === (true, (33,)) +@test f41416(3; b=false) === (false, (3,))