Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve Vararg checking #457

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SciMLBase"
uuid = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
authors = ["Chris Rackauckas <[email protected]> and contributors"]
version = "1.92.5"
version = "1.92.6"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
19 changes: 15 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@
f::Any
end

function sig_has_vararg(sig)
isvarargtype(x) = x isa typeof(Vararg)
# unwrap unionalls
while sig isa UnionAll
sig = sig.body
end
if sig <: Tuple
return any(isvarargtype, sig.parameters)
else
error("SciMLBase is messing around with types that it doesn't understand. Please file an issue")

Check warning on line 217 in src/utils.jl

View check run for this annotation

Codecov / codecov/patch

src/utils.jl#L217

Added line #L217 was not covered by tests
end
end

function Base.showerror(io::IO, e::FunctionArgumentsError)
println(io, ARGUMENTS_ERROR_MESSAGE)
print(io, "Offending function: ")
Expand Down Expand Up @@ -255,8 +268,7 @@
# Find if there's a `f(args...)` dispatch
# If so, no error
for i in 1:length(nargs)
if nargs[i] < inplace_param_number &&
any(isequal(Vararg{Any}), methods(f).ms[1].sig.parameters)
if nargs[i] < inplace_param_number && sig_has_vararg(methods(f).ms[1].sig)
# If varargs, assume iip
return iip_preferred
end
Expand All @@ -274,8 +286,7 @@
# Find if there's a `f(args...)` dispatch
# If so, no error
for i in 1:length(nargs)
if nargs[i] < inplace_param_number &&
any(isequal(Vararg{Any}), methods(f).ms[1].sig.parameters)
if nargs[i] < inplace_param_number && sig_has_vararg(methods(f).ms[1].sig)
# If varargs, assume iip
return iip_preferred
end
Expand Down
10 changes: 10 additions & 0 deletions test/function_building_error_messages.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,3 +463,13 @@ optf(u) = 1.0
optf(u, p) = 1.0
OptimizationFunction(optf)
OptimizationProblem(optf, 1.0)

# Varargs
var1(u...) = 1.0
var2(u::Vararg{Any, N}) where N = 1.0
var3(u::Vararg{Int, N}) where N = 1.0
var4(u::Vararg{Vector{T}, N}) where {T, N} = 1.0
OptimizationFunction(var1)
OptimizationFunction(var2)
OptimizationFunction(var3)
OptimizationFunction(var4)
Loading