You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix duplicate error when using generator in Dict (JuliaLang#53151)
Fixes: JuliaLang#33147
Replaces/Closes: JuliaLang#40445
The difference here, compared to past implementations, is that we use
the zero-cost `isiterable` check on every intermediate step, instead of
wrapping the call in a try/catch and then trying to re-approximate the
`isiterable` afterwards. Some samples:
```julia
julia> Dict(i for i in 1:3)
ERROR: ArgumentError: AbstractDict(kv): kv needs to be an iterator of 2-tuples or pairs
Stacktrace:
[1] _throw_dict_kv_error()
@ Base ./dict.jl:118
[2] grow_to!
@ ./dict.jl:132 [inlined]
[3] dict_with_eltype
@ ./abstractdict.jl:592 [inlined]
[4] Dict(kv::Base.Generator{UnitRange{Int64}, typeof(identity)})
@ Base ./dict.jl:120
[5] top-level scope
@ REPL[1]:1
julia> Dict(i => error("$i") for i in 1:3)
ERROR: 1
Stacktrace:
[1] error(s::String)
@ Base ./error.jl:35
[2] (::var"mmtk#3#4")(i::Int64)
@ Main ./none:0
[3] iterate
@ ./generator.jl:48 [inlined]
[4] grow_to!
@ ./dict.jl:124 [inlined]
[5] dict_with_eltype
@ ./abstractdict.jl:592 [inlined]
[6] Dict(kv::Base.Generator{UnitRange{Int64}, var"mmtk#3#4"})
@ Base ./dict.jl:120
[7] top-level scope
@ REPL[2]:1
```
The other unrelated change here is that `dest = empty(dest, typeof(k),
typeof(v))` is made conditional, so we do not unconditionally construct
an empty Dict in order to discard it and allocate an exact duplicate of
it, but only do so if inference wasn't precise originally.
Co-authored-by: Curtis Vogt <[email protected]>
Copy file name to clipboardExpand all lines: base/abstractdict.jl
+49Lines changed: 49 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -579,6 +579,55 @@ _tablesz(x::T) where T <: Integer = x < 16 ? T(16) : one(T)<<(top_set_bit(x-one(
579
579
580
580
TP{K,V} = Union{Type{Tuple{K,V}},Type{Pair{K,V}}}
581
581
582
+
# This error is thrown if `grow_to!` cannot validate the contents of the iterator argument to it, which it does by testing the iteration protocol (isiterable) on it each time it is about to start iteration on it
583
+
_throw_dict_kv_error() =throw(ArgumentError("AbstractDict(kv): kv needs to be an iterator of 2-tuples or pairs"))
0 commit comments