Skip to content

Commit

Permalink
Piracy fix for kwcall
Browse files Browse the repository at this point in the history
  • Loading branch information
lgoettgens committed Sep 4, 2023
1 parent 8dc359d commit afc5558
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
19 changes: 15 additions & 4 deletions src/piracy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,14 +162,25 @@ function is_pirate(meth::Method; treat_as_own = Union{Function,Type}[])

signature = Base.unwrap_unionall(meth.sig)

function_type_index = 1
if signature.parameters[1] === typeof(Core.kwcall)
# kwcall is a special case, since it is not a real function
# but a wrapper around a function, the third parameter is the original
# function, its positional arguments follow.
function_type_index += 2
end

# the first parameter in the signature is the function type, and it
# follows slightly other rules if it happens to be a Union type
is_foreign_method(signature.parameters[1], method_pkg; treat_as_own = treat_as_own) ||
return false
is_foreign_method(
signature.parameters[function_type_index],
method_pkg;
treat_as_own = treat_as_own,
) || return false

all(
return all(
param -> is_foreign(param, method_pkg; treat_as_own = treat_as_own),
signature.parameters[2:end],
signature.parameters[function_type_index+1:end],
)
end

Expand Down
3 changes: 2 additions & 1 deletion test/test_piracy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Base.findlast(::ForeignParameterizedType{Foo}, x::Int) = x + 1
# Not piracy
const MyUnion = Union{Int,Foo}
MyUnion(x::Int) = x
MyUnion(; x::Int) = x

export MyUnion

Expand Down Expand Up @@ -71,7 +72,7 @@ end
2 + # Foo constructors
1 + # Bar constructor
2 + # f
1 + # MyUnion
4 + # MyUnion (incl. kwcall)
6 + # findlast
3 + # findfirst
1 + # ForeignType callable
Expand Down

0 comments on commit afc5558

Please sign in to comment.