Skip to content

Commit 0b7ecf6

Browse files
authored
throw MethodError when passing kwargs to a function that doesn't accept any (#33151)
1 parent 8b4232b commit 0b7ecf6

File tree

3 files changed

+8
-6
lines changed

3 files changed

+8
-6
lines changed

src/rtutils.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -194,10 +194,7 @@ JL_DLLEXPORT void JL_NORETURN jl_eof_error(void)
194194
// get kwsorter field, with appropriate error check and message
195195
JL_DLLEXPORT jl_value_t *jl_get_keyword_sorter(jl_value_t *f)
196196
{
197-
jl_methtable_t *mt = jl_gf_mtable(f);
198-
if (mt->kwsorter == NULL)
199-
jl_errorf("function %s does not accept keyword arguments", jl_symbol_name(mt->name));
200-
return mt->kwsorter;
197+
return jl_get_kwsorter(jl_typeof(f));
201198
}
202199

203200
JL_DLLEXPORT void jl_typeassert(jl_value_t *x, jl_value_t *t)

test/keywordargs.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,7 @@ end
357357
# issue #33026
358358
using InteractiveUtils
359359
@test (@which kwf1(1, tens=2)).line > 0
360+
361+
no_kw_args(x::Int) = 0
362+
@test_throws MethodError no_kw_args(1, k=1)
363+
@test_throws MethodError no_kw_args("", k=1)

test/precompile.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ try
8787
(::Task)(::UInt8, ::UInt16, ::UInt32) = 2
8888
8989
# issue 16471 (capturing references to a kwfunc)
90-
Test.@test_throws ErrorException Core.kwfunc(Base.nothing)
90+
Test.@test !isdefined(Base.Nothing.name.mt, :kwsorter)
9191
Base.nothing(::UInt8, ::UInt16, ::UInt32; x = 52) = x
9292
const nothingkw = Core.kwfunc(Base.nothing)
9393
@@ -165,7 +165,8 @@ try
165165
const a31488 = fill(String(_v31488), 100)
166166
end
167167
""")
168-
@test_throws ErrorException Core.kwfunc(Base.nothing) # make sure `nothing` didn't have a kwfunc (which would invalidate the attempted test)
168+
# make sure `nothing` didn't have a kwfunc (which would invalidate the attempted test)
169+
@test !isdefined(Base.Nothing.name.mt, :kwsorter)
169170

170171
# Issue #12623
171172
@test __precompile__(false) === nothing

0 commit comments

Comments
 (0)