andyferris / Dictionaries.jl

An alternative interface for dictionaries in Julia, for improved productivity and performance
Other
278 stars 28 forks source link

Issues with `undef` #78

Closed mtfishman closed 2 years ago

mtfishman commented 2 years ago

Hey @andyferris, some more issue coming your way:

julia> using Dictionaries

julia> d = similar(Indices(1:4), String);

julia> d
4-element Dictionary{Int64, String}
 1 │ #undef
 2 │ #undef
 3 │ #undef
 4 │ #undef

julia> show(d);
{ERROR: UndefRefError: access to undefined reference
Stacktrace:
  [1] getindex
    @ ./array.jl:861 [inlined]
  [2] iterate (repeats 2 times)
    @ ./array.jl:835 [inlined]
  [3] iterate
    @ ./iterators.jl:159 [inlined]
  [4] iterate
    @ ./iterators.jl:158 [inlined]
  [5] Dictionary{Int64, String}(inds::Indices{Int64}, values::Vector{String})
    @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:123
  [6] convert
    @ ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:158 [inlined]
  [7] PairDictionary
    @ ~/.julia/packages/Dictionaries/nswsB/src/pairs.jl:3 [inlined]
  [8] pairs
    @ ~/.julia/packages/Dictionaries/nswsB/src/pairs.jl:28 [inlined]
  [9] show(io::Base.TTY, d::Dictionary{Int64, String})
    @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/show.jl:25
 [10] show(x::Dictionary{Int64, String})
    @ Base ./show.jl:393
 [11] top-level scope
    @ REPL[25]:1

julia> pairs(d)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex
   @ ./array.jl:861 [inlined]
 [2] iterate (repeats 2 times)
   @ ./array.jl:835 [inlined]
 [3] iterate
   @ ./iterators.jl:159 [inlined]
 [4] iterate
   @ ./iterators.jl:158 [inlined]
 [5] Dictionary{Int64, String}(inds::Indices{Int64}, values::Vector{String})
   @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:123
 [6] convert
   @ ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:158 [inlined]
 [7] PairDictionary
   @ ~/.julia/packages/Dictionaries/nswsB/src/pairs.jl:3 [inlined]
 [8] pairs(d::Dictionary{Int64, String})
   @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/pairs.jl:28
 [9] top-level scope
   @ REPL[26]:1

julia> typeof(d)(d)
ERROR: UndefRefError: access to undefined reference
Stacktrace:
  [1] getindex
    @ ./array.jl:861 [inlined]
  [2] iterate (repeats 2 times)
    @ ./array.jl:835 [inlined]
  [3] iterate
    @ ./iterators.jl:159 [inlined]
  [4] iterate
    @ ./iterators.jl:158 [inlined]
  [5] Dictionary{Int64, String}(inds::Indices{Int64}, values::Vector{String})
    @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:123
  [6] convert
    @ ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:158 [inlined]
  [7] Enumerate (repeats 2 times)
    @ ./iterators.jl:127 [inlined]
  [8] enumerate
    @ ./iterators.jl:153 [inlined]
  [9] Dictionary{Int64, String}(inds::Indices{Int64}, values::Dictionary{Int64, String})
    @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:123
 [10] Dictionary{Int64, String}(indexable::Dictionary{Int64, String})
    @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:62
 [11] top-level scope
    @ REPL[27]:1

julia> versioninfo()
Julia Version 1.7.0
Commit 3bf9d17731 (2021-11-30 12:12 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) E-2176M  CPU @ 2.70GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-12.0.1 (ORCJIT, skylake)
Environment:
  JULIA_EDITOR = vim

(@v1.7) pkg> st Dictionaries
      Status `~/.julia/environments/v1.7/Project.toml`
  [85a47980] Dictionaries v0.3.16

It looks like some of these can get fixed with a definition like the following:

Dictionaries.Dictionary{I, T}(inds::Indices{I}, values::Vector{T}) where {I, T} = Dictionary{I, T}(copy(inds), copy(values), nothing)

i.e. short-circuit the Dictionary constructor in the case when the types match, though when I define that there is still an issue in show:

julia> show(d)
{ERROR: UndefRefError: access to undefined reference
Stacktrace:
 [1] getindex
   @ ./array.jl:861 [inlined]
 [2] gettokenvalue
   @ ~/.julia/packages/Dictionaries/nswsB/src/Dictionary.jl:294 [inlined]
 [3] gettokenvalue
   @ ~/.julia/packages/Dictionaries/nswsB/src/pairs.jl:47 [inlined]
 [4] iterate
   @ ~/.julia/packages/Dictionaries/nswsB/src/AbstractDictionary.jl:54 [inlined]
 [5] show(io::Base.TTY, d::Dictionary{Int64, String})
   @ Dictionaries ~/.julia/packages/Dictionaries/nswsB/src/show.jl:25
 [6] show(x::Dictionary{Int64, String})
   @ Base ./show.jl:393
 [7] top-level scope
   @ REPL[40]:1

I'm happy to make a PR, though perhaps you have another approach for fixing the Dictionary constructor when there are undefined values.

andyferris commented 2 years ago

Thanks for the report. Yeah this seems like more than one bug, which we should fix.

andyferris commented 2 years ago

Sorry it took so long to get to this (been on Christmas vacation).

mtfishman commented 2 years ago

No worries, hope you had a nice break and thanks for the fix!