From 9145948118c9ed41a99115907f7cc3f74235dbc6 Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 4 Feb 2025 08:22:27 +1300 Subject: [PATCH] Fix bug copying expressions that do not contain variables --- src/copy.jl | 5 +++++ test/test_model.jl | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/copy.jl b/src/copy.jl index ae8c9c8aa15..7ffa2c7b09b 100644 --- a/src/copy.jl +++ b/src/copy.jl @@ -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 diff --git a/test/test_model.jl b/test/test_model.jl index 74e5d7046cd..b09956248ae 100644 --- a/test/test_model.jl +++ b/test/test_model.jl @@ -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