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

Updating error message on ambiguous variable #2222

Merged
merged 3 commits into from
Jul 1, 2020
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
11 changes: 9 additions & 2 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,15 @@ macro variable(args...)
anon_singleton = true
else
x = popfirst!(extra)
if x in [:Int,:Bin,:PSD]
_error("Ambiguous variable name $x detected. Use the \"category\" keyword argument to specify a category for an anonymous variable.")
if x == :Int
_error("Ambiguous variable name $x detected. To specify an anonymous integer " *
"variable, use `@variable(model, integer = true)` instead.")
elseif x == :Bin
_error("Ambiguous variable name $x detected. To specify an anonymous binary " *
"variable, use `@variable(model, binary = true)` instead.")
elseif x == :PSD
_error("Size of anonymous square matrix of positive semidefinite anonymous variables is not specified. To specify size of square matrix " *
"use `@variable(model, [1:n, 1:n], PSD)` instead.")
end
anon_singleton = false
end
Expand Down
20 changes: 20 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,26 @@ end
@testset "Macros for JuMP.Model" begin
macros_test(Model, VariableRef)

@testset "Adding anonymous variable and specify required constraint on it" begin
model = Model()
@test_macro_throws ErrorException("In `@variable(m, Int)`: Ambiguous variable name Int detected." *
" To specify an anonymous integer variable, use `@variable(model, integer = true)` instead.") @variable(m, Int)
v = @variable(model, integer = true)
@test name(v) == ""
@test is_integer(v)

@test_macro_throws ErrorException("In `@variable(m, Bin)`: Ambiguous variable name Bin detected." *
" To specify an anonymous binary variable, use `@variable(model, binary = true)` instead.") @variable(m, Bin)
v = @variable(model, binary = true)
@test name(v) == ""
@test is_binary(v)

@test_macro_throws ErrorException("In `@variable(m, PSD)`: Size of anonymous square matrix of positive semidefinite anonymous variables is not specified." *
" To specify size of square matrix use `@variable(model, [1:n, 1:n], PSD)` instead.") @variable(m, PSD)
v = @variable(model, [1:1, 1:1], PSD)
@test name(v[1]) == ""
end

@testset "Nested tuple destructuring" begin
m = Model()
d = Dict((1,2) => 3)
Expand Down