From cddc75f05fca6aff60cc0e8739c1c2cc24a5e5ac Mon Sep 17 00:00:00 2001 From: odow Date: Sat, 6 Jan 2024 11:39:15 +1300 Subject: [PATCH 1/2] Fix escaping of set kwarg in variable macro --- src/macros/@variable.jl | 2 +- test/test_macros.jl | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/macros/@variable.jl b/src/macros/@variable.jl index 72d9ba0cccb..befbd97daa3 100644 --- a/src/macros/@variable.jl +++ b/src/macros/@variable.jl @@ -206,7 +206,7 @@ macro variable(input_args...) "constrained to `$set`.", ) end - set = set_kw + set = esc(set_kw) end # ; set_string_name name_expr = Containers.build_name_expr(name, index_vars, kwargs) diff --git a/test/test_macros.jl b/test/test_macros.jl index fbb2baf69d5..68f60cae59c 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -898,15 +898,16 @@ end function test_unrecognized_variable_type() model = Model() + a = 1 err = ErrorException( - "In `@variable(model, x, 2, variable_type = 1)`: " * + "In `@variable(model, x, 2, variable_type = a)`: " * "Unrecognized positional arguments: (2, 1). (You may have " * "passed it as a positional argument, or as a keyword value to " * "`variable_type`.)\n\nIf you're trying to create a JuMP " * "extension, you need to implement `build_variable`. Read the " * "docstring for more details.", ) - @test_throws_runtime err @variable(model, x, 2, variable_type = 1) + @test_throws_runtime err @variable(model, x, 2, variable_type = a) return end @@ -2234,4 +2235,15 @@ function test_macro_modify_user_data() return end +function test_escaping_of_set_kwarg() + model = Model() + bound = 5.0 + x = @variable(model, set = MOI.GreaterThan(bound)) + @test lower_bound(x) == bound + set = MOI.LessThan(1.0) + y = @variable(model, set = set) + @test upper_bound(y) == 1.0 + return +end + end # module From 76ae8c9845210e9e2388759864cd6883d3c6840a Mon Sep 17 00:00:00 2001 From: odow Date: Sat, 6 Jan 2024 11:51:23 +1300 Subject: [PATCH 2/2] Add a related test --- test/test_macros.jl | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test/test_macros.jl b/test/test_macros.jl index 68f60cae59c..5183deb4bc6 100644 --- a/test/test_macros.jl +++ b/test/test_macros.jl @@ -2211,6 +2211,14 @@ function test_base_name_escape() return end +function test_container_escape() + model = Model() + a = Containers.DenseAxisArray + x = @variable(model, [1:2], container = a) + @test x isa Containers.DenseAxisArray{VariableRef} + return +end + function test_constraint_broadcast_in_set() model = Model() @variable(model, x[1:2])