diff --git a/base/dict.jl b/base/dict.jl index d19d9ed448a055..59a80285f2551d 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -432,7 +432,19 @@ Dict{K }(ps::Pair{K}...,) = Dict{K,Any}(ps) Dict{V }(ps::Pair{TypeVar(:K),V}...,) = Dict{Any,V}(ps) Dict( ps::Pair...) = Dict{Any,Any}(ps) -Dict(kv) = dict_with_eltype(kv, eltype(kv)) +function Dict(kv) + try + Base.dict_with_eltype(kv, eltype(kv)) + catch e + if any(x->isempty(methods(x, (typeof(kv),))), [start, next, done]) || + !all(x->isa(x,Union{Tuple,Pair}),kv) + throw(ArgumentError("Dict(kv): kv needs to be an iterator of tuples or pairs")) + else + rethrow(e) + end + end +end + dict_with_eltype{K,V}(kv, ::Type{Tuple{K,V}}) = Dict{K,V}(kv) dict_with_eltype{K,V}(kv, ::Type{Pair{K,V}}) = Dict{K,V}(kv) dict_with_eltype(kv, t) = Dict{Any,Any}(kv) diff --git a/test/dict.jl b/test/dict.jl index 03e7e4541ca232..35a53b61c42665 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -338,3 +338,9 @@ end d = Dict('a'=>1, 'b'=>1, 'c'=> 3) @test [d[k] for k in keys(d)] == [d[k] for k in eachindex(d)] == [v for (k, v) in d] == [d[x[1]] for (i, x) in enumerate(d)] + + +# Issue 12451 +@test_throws ArgumentError Dict(0) +@test_throws ArgumentError Dict([1]) +@test_throws ArgumentError Dict([(1,2),0])