Skip to content

Commit

Permalink
disallow colons in variable index sets
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Jun 3, 2016
1 parent 6121efb commit ae01e41
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/JuMPContainer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ end

Base.isempty(d::JuMPContainer) = isempty(_innercontainer(d))

coloncheck(args::Number...) = nothing

function coloncheck(args...)
if any(t -> t == Colon(), args)
error("Colons not allowed as keys in JuMP containers.")
end
end

# generate and instantiate a type which is indexed by the given index sets
# the following types of index sets are allowed:
# 0:K -- range with compile-time starting index
Expand Down
2 changes: 2 additions & 0 deletions src/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -985,6 +985,8 @@ macro variable(args...)
$(anonvar ? variable : :($escvarname = $variable))
end)
else
coloncheckcode = Expr(:call,:coloncheck,refcall.args[2:end]...)
code = :($coloncheckcode; $code)
return assert_validmodel(m, quote
$(getloopedcode(variable, code, condition, idxvars, idxsets, idxpairs, :Variable))
isa($variable, JuMPContainer) && pushmeta!($variable, :model, $m)
Expand Down
6 changes: 6 additions & 0 deletions test/macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,9 @@ facts("[macros] Anonymous versions of macros") do
@fact e[2] --> e[2]
@fact f[:red] --> f[:red]
end

facts("[macros] Colons in index sets") do
m = Model()
S = [:]
@fact_throws ErrorException @variable(m, x[S])
end

0 comments on commit ae01e41

Please sign in to comment.