Skip to content

Commit

Permalink
Throw error for add_to_expression with GenericNonlinearExpr
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 14, 2023
1 parent ca73f21 commit 6edbbf9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1144,3 +1144,12 @@ end
function LinearAlgebra.qr(::AbstractMatrix{<:AbstractJuMPScalar})
return throw(MOI.UnsupportedNonlinearOperator(:qr))
end

function add_to_expression!(f::GenericNonlinearExpr, args...)
return error(
"`add_to_expression!` is not supported for expressions of type " *
"`$(typeof(f))` because they cannot be modified in-place. " *
"Instead of `add_to_expression!(expr, args..)`, use " *
"`expr += *(args...)`.",
)
end
16 changes: 16 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -926,4 +926,20 @@ function test_generic_nonlinear_expr_infer_variable_type()
return
end

function test_add_to_expression!()
model = Model()
@variable(model, x)
y = zero(NonlinearExpr)
@test_throws(
ErrorException(
"`add_to_expression!` is not supported for expressions of type " *
"`$(typeof(y))` because they cannot be modified in-place. " *
"Instead of `add_to_expression!(expr, args..)`, use " *
"`expr += *(args...)`.",
),
test_add_to_expression!(y, 2.0, sin(x)),
)
return
end

end # module

0 comments on commit 6edbbf9

Please sign in to comment.