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

Fix variable_ref_type for unsupported types and GenericNonlinearExpr #3556

Merged
merged 3 commits into from
Nov 2, 2023
Merged
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 src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ Alias for `GenericNonlinearExpr{VariableRef}`, the specific
"""
const NonlinearExpr = GenericNonlinearExpr{VariableRef}

variable_ref_type(::GenericNonlinearExpr{V}) where {V} = V
variable_ref_type(::Type{GenericNonlinearExpr{V}}) where {V} = V

const _PREFIX_OPERATORS =
(:+, :-, :*, :/, :^, :||, :&&, :>, :<, :(<=), :(>=), :(==))
Expand Down
8 changes: 8 additions & 0 deletions src/variables.jl
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,14 @@ variable type associated with the model or expression type `F`.
"""
variable_ref_type(::F) where {F} = variable_ref_type(F)

function variable_ref_type(::Type{F}) where {F}
return error(
"Unable to compute the `variable_ref_type` of the type `$F`. If you " *
"are developing a JuMP extension, define a new method for " *
"`JuMP.variable_ref_type(::Type{$F})`",
)
end

variable_ref_type(::Type{V}) where {V<:AbstractVariableRef} = V

value_type(::Type{<:AbstractVariableRef}) = Float64
Expand Down
7 changes: 7 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -971,4 +971,11 @@ function test_operator_max()
return
end

function test_variable_ref_type()
for V in (GenericVariableRef{Int}, VariableRef)
@test variable_ref_type(GenericNonlinearExpr{V}) == V
end
return
end

end # module
14 changes: 14 additions & 0 deletions test/test_variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1555,4 +1555,18 @@ function test_parameter_arrays()
return
end

function test_variable_ref_type_unsupported()
for F in (Vector{VariableRef}, Vector{Int})
@test_throws(
ErrorException(
"Unable to compute the `variable_ref_type` of the type `$F`. If you " *
"are developing a JuMP extension, define a new method for " *
"`JuMP.variable_ref_type(::Type{$F})`",
),
variable_ref_type(F),
)
end
return
end

end # module TestVariable
Loading