Skip to content

Commit

Permalink
Merge pull request #782 from JuliaOpt/ml/nocolons
Browse files Browse the repository at this point in the history
disallow colons in variable index sets
  • Loading branch information
joehuchette committed Jun 3, 2016
2 parents 8227200 + ae01e41 commit fcf1365
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
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
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
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 fcf1365

Please sign in to comment.