Skip to content

Commit c66a1d9

Browse files
JeffBezansonKristofferC
authored andcommitted
fix hasmethod with kwargs to exclude positional arg names (#58410)
(cherry picked from commit 7df60f4)
1 parent 9a727c6 commit c66a1d9

File tree

2 files changed

+2
-1
lines changed

2 files changed

+2
-1
lines changed

base/reflection.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1851,11 +1851,11 @@ function hasmethod(f, t, kwnames::Tuple{Vararg{Symbol}}; world::UInt=get_world_c
18511851
match = ccall(:jl_gf_invoke_lookup, Any, (Any, Any, UInt), tt, nothing, world)
18521852
match === nothing && return false
18531853
kws = ccall(:jl_uncompress_argnames, Array{Symbol,1}, (Any,), (match::Method).slot_syms)
1854+
kws = kws[((match::Method).nargs + 1):end] # remove positional arguments
18541855
isempty(kws) && return true # some kwfuncs simply forward everything directly
18551856
for kw in kws
18561857
endswith(String(kw), "...") && return true
18571858
end
1858-
kwnames = Symbol[kwnames[i] for i in 1:length(kwnames)]
18591859
return issubset(kwnames, kws)
18601860
end
18611861

test/reflection.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,7 @@ f(x::Int; y=3) = x + y
825825
@test hasmethod(f, Tuple{Int})
826826
@test hasmethod(f, Tuple{Int}, ())
827827
@test hasmethod(f, Tuple{Int}, (:y,))
828+
@test !hasmethod(f, Tuple{Int}, (:x,))
828829
@test !hasmethod(f, Tuple{Int}, (:jeff,))
829830
@test !hasmethod(f, Tuple{Int}, (:y,), world=typemin(UInt))
830831
g(; b, c, a) = a + b + c

0 commit comments

Comments
 (0)