Skip to content

Commit

Permalink
Fix an illegal simplification in MA.operate!! for NonlinearExpr (#3826)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 13, 2024
1 parent dc4f2a8 commit 0fbe07e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -408,10 +408,15 @@ function _MA.operate!!(
_throw_if_not_real(x)
if any(isequal(_MA.Zero()), args)
return x
elseif x.head == :+
push!(x.args, *(args...))
return x
end
# It may seem like we should do this performance optimization, but it is NOT
# safe. See JuMP#3825. The issue is that even though we're calling operate!!
# `x` is not mutable, because it may, amoungst other things, be aliased in
# one of the args
# elseif x.head == :+
# push!(x.args, *(args...))
# return x
# end
return +(x, *(args...))
end

Expand Down
10 changes: 10 additions & 0 deletions test/test_nlp_expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,16 @@ function test_ma_zero_in_operate!!()
return
end

function test_ma_operate!!_nested_sum()
model = Model()
@variable(model, x)
y = NonlinearExpr(:+, Any[x])
z = MA.operate!!(MA.add_mul, y, y)
@test isequal_canonical(y, @force_nonlinear(+x))
@test isequal_canonical(z, @force_nonlinear(+(+x, +x)))
return
end

function test_nonlinear_operator_inferred()
model = Model()
@variable(model, x)
Expand Down

0 comments on commit 0fbe07e

Please sign in to comment.