Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix escaping of set kwarg in variable macro #3647

Merged
merged 2 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading