Skip to content

Commit

Permalink
Throw method error instead of throwing a generic error upon keyword m…
Browse files Browse the repository at this point in the history
…ismatch
  • Loading branch information
dhoegh committed May 31, 2016
1 parent 5ab6cd4 commit 0f6cba6
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/error.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ backtrace() = ccall(:jl_backtrace_from_here, Array{Ptr{Void},1}, (Int32,), false
catch_backtrace() = ccall(:jl_get_backtrace, Array{Ptr{Void},1}, ())

## keyword arg lowering generates calls to this ##
kwerr(kw) = error("unrecognized keyword argument \"", kw, "\"")
kwerr(kw, args...) = throw(MethodError(typeof(args[1]).name.mt.kwsorter, (kw,args...)))

## system error handling ##

Expand Down
3 changes: 2 additions & 1 deletion src/julia-syntax.scm
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@
,else)))
(if (null? restkw)
;; if no rest kw, give error for unrecognized
`(call (top kwerr) ,elt)
`(call (top kwerr) ,kw ,@(map arg-name pargl),@(if (null? vararg) '()
(list `(... ,(arg-name (car vararg))))))
;; otherwise add to rest keywords
`(ccall 'jl_cell_1d_push Void (tuple Any Any)
,rkw (tuple ,elt
Expand Down
6 changes: 3 additions & 3 deletions test/keywordargs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ kwf1(ones; tens=0, hundreds=0) = ones + 10*tens + 100*hundreds
@test kwf1(3, tens=7, hundreds=2) == 273

@test_throws MethodError kwf1() # no method, too few args
@test_throws ErrorException kwf1(1, z=0) # unsupported keyword
@test_throws MethodError kwf1(1, z=0) # unsupported keyword
@test_throws MethodError kwf1(1, 2) # no method, too many positional args

# keyword args plus varargs
Expand All @@ -18,7 +18,7 @@ kwf2(x, rest...; y=1) = (x, y, rest)
@test isequal(kwf2(0,1,2), (0, 1, (1,2)))
@test isequal(kwf2(0,1,2,y=88), (0, 88, (1,2)))
@test isequal(kwf2(0,y=88,1,2), (0, 88, (1,2)))
@test_throws ErrorException kwf2(0, z=1)
@test_throws MethodError kwf2(0, z=1)
@test_throws MethodError kwf2(y=1)

# keyword arg with declared type
Expand Down Expand Up @@ -191,7 +191,7 @@ let f = (x;a=1,b=2)->(x, a, b)
@test f(0) === (0, 1, 2)
@test f(1,a=10,b=20) === (1,10,20)
@test f(0,b=88) === (0, 1, 88)
@test_throws ErrorException f(0,z=1)
@test_throws MethodError f(0,z=1)
end
@test ((a=2)->10a)(3) == 30
@test ((a=2)->10a)() == 20
Expand Down

0 comments on commit 0f6cba6

Please sign in to comment.