Skip to content

Commit ca58ab2

Browse files
author
Rene Donner
committed
fix #12451 by providing a better error message
1 parent d34ce59 commit ca58ab2

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

base/dict.jl

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,19 @@ Dict{K }(ps::Pair{K}...,) = Dict{K,Any}(ps)
432432
Dict{V }(ps::Pair{TypeVar(:K),V}...,) = Dict{Any,V}(ps)
433433
Dict( ps::Pair...) = Dict{Any,Any}(ps)
434434

435-
Dict(kv) = dict_with_eltype(kv, eltype(kv))
435+
function Dict(kv)
436+
try
437+
Base.dict_with_eltype(kv, eltype(kv))
438+
catch e
439+
if any(x->isempty(methods(x, (typeof(kv),))), [start, next, done]) ||
440+
!all(x->isa(x,Union{Tuple,Pair}),kv)
441+
throw(ArgumentError("Dict(kv): kv needs to be an iterator of tuples or pairs"))
442+
else
443+
rethrow(e)
444+
end
445+
end
446+
end
447+
436448
dict_with_eltype{K,V}(kv, ::Type{Tuple{K,V}}) = Dict{K,V}(kv)
437449
dict_with_eltype{K,V}(kv, ::Type{Pair{K,V}}) = Dict{K,V}(kv)
438450
dict_with_eltype(kv, t) = Dict{Any,Any}(kv)

test/dict.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,3 +338,9 @@ end
338338
d = Dict('a'=>1, 'b'=>1, 'c'=> 3)
339339
@test [d[k] for k in keys(d)] == [d[k] for k in eachindex(d)] ==
340340
[v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)]
341+
342+
343+
# Issue 12451
344+
@test_throws ArgumentError Dict(0)
345+
@test_throws ArgumentError Dict([1])
346+
@test_throws ArgumentError Dict([(1,2),0])

0 commit comments

Comments
 (0)