Skip to content

Commit

Permalink
Fix duplicate error when using generator in Dict
Browse files Browse the repository at this point in the history
Fixes: #33147
  • Loading branch information
omus committed Apr 12, 2021
1 parent c3c8ffa commit 60d1660
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
8 changes: 7 additions & 1 deletion base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,13 @@ function Dict(kv)
try
dict_with_eltype((K, V) -> Dict{K, V}, kv, eltype(kv))
catch
if !isiterable(typeof(kv)) || !all(x->isa(x,Union{Tuple,Pair}),kv)
valid_iterator = try
isiterable(typeof(kv)) && all(x->isa(x,Union{Tuple,Pair}),kv)
catch
true # An exception has occurred during iteration. Rethrow the original error
end

if !valid_iterator
throw(ArgumentError("Dict(kv): kv needs to be an iterator of tuples or pairs"))
else
rethrow()
Expand Down
9 changes: 9 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,15 @@ end

# issue #39117
@test Dict(t[1]=>t[2] for t in zip((1,"2"), (2,"2"))) == Dict{Any,Any}(1=>2, "2"=>"2")

@testset "issue #33147" begin
try
Dict(i => error("$i") for i in 1:3)
catch ex
@test ex isa ErrorException
@test length(Base.catch_stack()) == 1
end
end
end

@testset "type of Dict constructed from varargs of Pairs" begin
Expand Down

0 comments on commit 60d1660

Please sign in to comment.