From f4adc5886471a663b426107970c09a47d3f8cd2a Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 15 Sep 2020 10:17:02 +1200 Subject: [PATCH] Remove invalid type assertion in CleverDicts --- src/Utilities/CleverDicts.jl | 9 +++++---- test/Utilities/CleverDicts.jl | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Utilities/CleverDicts.jl b/src/Utilities/CleverDicts.jl index c1a4d16dc0..28a3b9ef7b 100644 --- a/src/Utilities/CleverDicts.jl +++ b/src/Utilities/CleverDicts.jl @@ -257,8 +257,9 @@ Base.haskey(::CleverDict, key) = false # Here, we implement the iterate functions for our `CleverDict`. For either # backend (`OrderedDict` or `Vector`+`BitSet`) we return the same State type # so that `iterate` returns the same -# type regardless of the backing datastructure. To help inference, we annotate -# the return type. +# type regardless of the backing datastructure. To help inference, we convert +# the return type. Don't use a type-assertion because this doesn't support `V` +# being an abstract type. struct State int::Int64 @@ -288,7 +289,7 @@ function Base.iterate( return nothing else el, i = itr - return el::Pair{K, V}, State(i) + return convert(Pair{K, V}, el), State(i) end end end @@ -318,7 +319,7 @@ function Base.iterate( return nothing else el, i = itr - return el::Pair{K, V}, State(i) + return convert(Pair{K, V}, el), State(i) end end end diff --git a/test/Utilities/CleverDicts.jl b/test/Utilities/CleverDicts.jl index 72f3119fc7..9647d71c2e 100644 --- a/test/Utilities/CleverDicts.jl +++ b/test/Utilities/CleverDicts.jl @@ -16,6 +16,24 @@ CleverDicts.index_to_key(::Type{MyKey}, index::Int) = MyKey{index}() @test d[MyKey{1}()] == "first" end + @testset "Abstract Value" begin + d = CleverDicts.CleverDict{MathOptInterface.VariableIndex, Any}() + key = CleverDicts.add_item(d, :a) + @test key == MathOptInterface.VariableIndex(1) + @test d[MathOptInterface.VariableIndex(1)] == :a + key = CleverDicts.add_item(d, "b") + @test key == MathOptInterface.VariableIndex(2) + @test d[MathOptInterface.VariableIndex(2)] == "b" + for (k, v) in d + if k.value == 1 + @test v == :a + else + @test k.value == 2 + @test v == "b" + end + end + end + @testset "get/set" begin d = CleverDicts.CleverDict{MathOptInterface.VariableIndex, String}() key = CleverDicts.add_item(d, "first")