Skip to content

Commit

Permalink
disable unsafe 'end' syntax (#730)
Browse files Browse the repository at this point in the history
  • Loading branch information
mlubin committed Apr 28, 2016
1 parent 10769fb commit bbf2725
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
11 changes: 9 additions & 2 deletions src/JuMPContainer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,16 @@ Base.length(x::JuMPDict) = length(x.tupledict)

Base.ndims{T,N}(x::JuMPDict{T,N}) = N
Base.abs(x::JuMPDict) = map(abs, x)
# avoid dangerous behavior with "end" (#730)
Base.endof(x::JuMPArray) = error("endof() (and \"end\" syntax) not implemented for JuMPArray objects.")
Base.size(x::JuMPArray) = error("size (and \"end\" syntax) not implemented for JuMPArray objects. Use JuMP.size if you want to access the dimensions.")
Base.size(x::JuMPArray,k) = error("size (and \"end\" syntax) not implemented for JuMPArray objects. Use JuMP.size if you want to access the dimensions.")
size(x::JuMPArray) = size(x.innerArray)
size(x::JuMPArray,k) = size(x.innerArray,k)
# for uses of size() within JuMP
size(x) = Base.size(x)
size(x,k) = Base.size(x,k)
# delegate one-argument functions
Base.size(x::JuMPArray) = size(x.innerArray)
Base.size(x::JuMPArray,k) = size(x.innerArray,k)
Compat.issymmetric(x::JuMPArray) = Compat.issymmetric(x.innerArray)

Base.eltype{T}(x::JuMPContainer{T}) = T
Expand Down
10 changes: 6 additions & 4 deletions test/variable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ facts("[variable] getvalue on empty things") do

@fact getvalue(x) --> Array(Float64, 4, 0, 3)
@fact typeof(getvalue(y)) <: JuMP.JuMPArray{Float64} --> true
@fact size(getvalue(y)) --> (4,0,3)
@fact JuMP.size(getvalue(y)) --> (4,0,3)
@fact typeof(getvalue(z)) --> JuMP.JuMPArray{Float64,3,Tuple{UnitRange{Int},Set{Any},UnitRange{Int}}}
@fact length(getvalue(z)) --> 0
end
Expand Down Expand Up @@ -213,11 +213,13 @@ end

facts("[variable] Can't use end for indexing a JuMPContainer") do
m = Model()
@defVar(m, x[0:2,1:4])
@defVar(m, y[i=1:4,j=1:4;true])
@variable(m, x[0:2,1:4])
@variable(m, y[i=1:4,j=1:4;true])
@variable(m, z[0:2])
@fact_throws x[end,1]
@fact_throws x[end-1]
@fact x[0,end-1] --> x[0,3]
@fact_throws x[0,end-1]
@fact_throws y[end,end-1]
@fact_throws y[end,1]
@fact_throws z[end]
end

0 comments on commit bbf2725

Please sign in to comment.