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 an illegal simplification in MA.operate!! for NonlinearExpr #3826

Merged
merged 2 commits into from
Sep 13, 2024
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
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
Loading