Skip to content

Commit

Permalink
Fix error for unsupported objective sense (#3601)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Dec 7, 2023
1 parent cecc5ba commit 59cc031
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
22 changes: 8 additions & 14 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1771,30 +1771,24 @@ In the last case, the expression throws an error using the `_error`
function in case the value is not an `MOI.OptimizationSense`.
"""
function _moi_sense(_error::Function, sense)
if sense == :Min
expr = MIN_SENSE
expr = if sense == :Min
MIN_SENSE
elseif sense == :Max
expr = MAX_SENSE
MAX_SENSE
else
# Refers to a variable that holds the sense.
# TODO: Better document this behavior
expr = esc(sense)
esc(sense)
end
return :(_throw_error_for_invalid_sense($_error, $expr))
end

function _throw_error_for_invalid_sense(_error::Function, sense)
return _error(
"Unexpected sense `$value`. The sense must be an",
" `MOI.OptimizatonSense`, `Min` or `Max`.",
"unexpected sense `$sense`. The sense must be an " *
"`::MOI.OptimizatonSense`, or the symbol `:Min` or `:Max`.",
)
end
function _throw_error_for_invalid_sense(
_error::Function,
sense::MOI.OptimizationSense,
)
return sense
end

_throw_error_for_invalid_sense(::Function, sense::MOI.OptimizationSense) = sense

"""
_replace_zero(model::M, x) where {M<:AbstractModel}
Expand Down
14 changes: 14 additions & 0 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2131,4 +2131,18 @@ function test_issue_3514()
return
end

function test_bad_objective_sense()
model = Model()
@variable(model, x)
@test_throws_strip(
ErrorException(
"In `@objective(model, :MinMax, x)`: unexpected sense `MinMax`. " *
"The sense must be an `::MOI.OptimizatonSense`, or the symbol " *
"`:Min` or `:Max`.",
),
@objective(model, :MinMax, x),
)
return
end

end # module

0 comments on commit 59cc031

Please sign in to comment.