Skip to content

Commit

Permalink
Add to other macros
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Sep 13, 2023
1 parent 34952ab commit be6beb3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
15 changes: 14 additions & 1 deletion src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1334,7 +1334,13 @@ function _constraint_macro(

creation_code =
Containers.container_code(idxvars, indices, code, requested_container)

# Wrap the entire code block in a let statement to make the model act as
# a type stable local variable.
creation_code = quote
let $model = $model
$creation_code
end
end
if anonvar
# Anonymous constraint, no need to register it in the model-level
# dictionary nor to assign it to a variable in the user scope.
Expand Down Expand Up @@ -1939,6 +1945,13 @@ macro expression(args...)
end
code =
Containers.container_code(idxvars, indices, code, requested_container)
# Wrap the entire code block in a let statement to make the model act as
# a type stable local variable.
creation_code = quote
let $m = $m
$creation_code
end
end
# don't do anything with the model, but check that it's valid anyway
if anonvar
macro_code = code
Expand Down
4 changes: 2 additions & 2 deletions test/test_hygiene.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ Test.@test j == 10

# Test that `model` is inferred correctly inside macros, despite being a
# non-const global.
model = JuMP.Model()
JuMP.@variable(model, x[1:0])
m = JuMP.Model()
JuMP.@variable(m, x[1:0])
Test.@test x == JuMP.VariableRef[]

end

0 comments on commit be6beb3

Please sign in to comment.