I’m working to update a module compatible with JuMP v1.1.1 to the newest version of JuMP. Within the module is code similar to:
module HoldBug
using JuMP
export @mytest
macro mytest(m,F)
m = esc(m)
x = esc(F)
:(@NLconstraint( $(m), $(x) <= 4) )
end
end # module HoldBug
using .HoldBug
using JuMP
m = Model()
@variable(m,x)
@macroexpand @mytest(m,x)
@mytest(m,x)
In the newest version of JuMP I get
UndefVarError: x not defined.
Expanding the macro reveals the issue, in JuMP1.4 the variable x is being held by HoldBug
I’m updating the Complementarity package, this piece of code is from the MPEC portion of the module.
Right now the package recursively escapes the expression F, so for example if F = 3*x-y then inside of the macro you get something like
3*esc(x) - esc(y)
rather than
esc(3*x-y)
I’ll experiment with add_nonlinear_constraint, but I’ve tried this in the past and it seems to be a similar issue, where the module hangs onto the variable.
Ah. This is pretty non-trivial. We changed quite a lot. The complementarity package is also really old. It’s almost worth using it as inspiration for a rewrite, rather than trying to minimally modify the code.