jump-dev / JuMP.jl

Modeling language for Mathematical Optimization (linear, mixed-integer, conic, semidefinite, nonlinear)
http://jump.dev/JuMP.jl/
Other
2.22k stars 393 forks source link

@variable macro fails to resolve variables in 1.18.0 #3646

Closed ianfiske closed 8 months ago

ianfiske commented 9 months ago

Here's a MWE:

using JuMP

m = Model()
x = (lower_bound=5.0,)
@variable(m, set = MOI.GreaterThan(x.lower_bound))

With JuMP 1.18.0, this fails with the error

ERROR: UndefVarError: `x` not defined
Stacktrace:
 [1] macro expansion
   @ ~/.julia/packages/JuMP/9tYtW/src/macros/@variable.jl:313 [inlined]
 [2] macro expansion
   @ ~/.julia/packages/JuMP/9tYtW/src/macros.jl:375 [inlined]
 [3] top-level scope
   @ ~/julia-envs/jump-bug/minrepro.jl:6

In JuMP 1.17 and below, there is no error.

odow commented 9 months ago

I'll take a look. I recently refactored our macro handling, so this is my fault.

I guess I was right https://github.com/jump-dev/JuMP.jl/pull/3629#issuecomment-1858912840

The main risk is probably esc scoping bugs

odow commented 9 months ago

See #3647

I guess the lesson is that when writing tests, we should prefer to use variables rather than literals so that we can check for scoping bugs like this.