Skip to content

Commit

Permalink
Fix escaping of set kwarg in variable macro (#3647)
Browse files Browse the repository at this point in the history
  • Loading branch information
odow authored Jan 6, 2024
1 parent 66184ef commit ef9ad37
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/macros/@variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -2210,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])
Expand All @@ -2234,4 +2243,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

0 comments on commit ef9ad37

Please sign in to comment.