From 0a6f66c57fbb3b158c43521b8f26995f35792d37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20H=C3=B8egh?= Date: Sun, 29 May 2016 12:44:43 +0200 Subject: [PATCH] Throw method error instead of throwing a generic error upon keyword mismatch --- base/error.jl | 2 +- src/julia-syntax.scm | 3 ++- test/keywordargs.jl | 6 +++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/base/error.jl b/base/error.jl index 7e2578719dbbc4..2bfee47b0a237c 100644 --- a/base/error.jl +++ b/base/error.jl @@ -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 ## diff --git a/src/julia-syntax.scm b/src/julia-syntax.scm index 0e74e5924d98f0..c6c689b841f0fb 100644 --- a/src/julia-syntax.scm +++ b/src/julia-syntax.scm @@ -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 diff --git a/test/keywordargs.jl b/test/keywordargs.jl index ce2c979f051af2..eff8844ec7fa41 100644 --- a/test/keywordargs.jl +++ b/test/keywordargs.jl @@ -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 @@ -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 @@ -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