From be6beb38326e2fc7c22e13aca148c5be4bfdae52 Mon Sep 17 00:00:00 2001 From: odow Date: Thu, 14 Sep 2023 10:11:49 +1200 Subject: [PATCH] Add to other macros --- src/macros.jl | 15 ++++++++++++++- test/test_hygiene.jl | 4 ++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/macros.jl b/src/macros.jl index 19bb34e427a..a74c7eb70e1 100644 --- a/src/macros.jl +++ b/src/macros.jl @@ -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. @@ -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 diff --git a/test/test_hygiene.jl b/test/test_hygiene.jl index 09b82cb11ff..9ff08bb6edc 100644 --- a/test/test_hygiene.jl +++ b/test/test_hygiene.jl @@ -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