Skip to content

Commit

Permalink
Fix bug copying expressions that do not contain variables
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed Feb 3, 2025
1 parent b734e12 commit 9145948
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/copy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ end

const ReferenceMap = GenericReferenceMap{Float64}

# Some fallbacks for Base types that we should pass through un-changed
Base.getindex(::GenericReferenceMap, x::Number) = x
Base.getindex(::GenericReferenceMap, x::AbstractString) = x
Base.getindex(::GenericReferenceMap, x::Symbol) = x

function Base.getindex(map::GenericReferenceMap, vref::GenericVariableRef)
return GenericVariableRef(map.model, map.index_map[index(vref)])
end
Expand Down
14 changes: 14 additions & 0 deletions test/test_model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1465,4 +1465,18 @@ function test_solver_name_error()
return
end

function test_copy_base_types()
model = Model()
@expression(model, a, 1.0)
@expression(model, b, "Bb")
@expression(model, c, :c)
@expression(model, d, BigInt(1) + BigInt(2) * im)
new_model = copy(model)
@test new_model[:a] === 1.0
@test new_model[:b] == "Bb"
@test new_model[:c] == :c
@test new_model[:d] == BigInt(1) + BigInt(2) * im
return
end

end # module TestModels

0 comments on commit 9145948

Please sign in to comment.