Skip to content

Commit

Permalink
fix #39117, overly-specific type from default_eltype (#39130)
Browse files Browse the repository at this point in the history
(cherry picked from commit 66c9f6a)
  • Loading branch information
JeffBezanson authored and KristofferC committed Jan 8, 2021
1 parent 7224671 commit 38f3b1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
16 changes: 15 additions & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,20 @@ function _collect_indices(indsA, A)
copyto!(B, CartesianIndices(axes(B)), A, CartesianIndices(indsA))
end

# NOTE: this function is not meant to be called, only inferred, for the
# purpose of bounding the types of values generated by an iterator.
function _iterator_upper_bound(itr)
x = iterate(itr)
while x !== nothing
val = getfield(x, 1)
if inferencebarrier(nothing)
return val
end
x = iterate(itr, getfield(x, 2))
end
throw(nothing)
end

# define this as a macro so that the call to Core.Compiler
# gets inlined into the caller before recursion detection
# gets a chance to see it, so that recursive calls to the caller
Expand All @@ -635,7 +649,7 @@ if isdefined(Core, :Compiler)
if $I isa Generator && ($I).f isa Type
($I).f
else
Core.Compiler.return_type(first, Tuple{typeof($I)})
Core.Compiler.return_type(_iterator_upper_bound, Tuple{typeof($I)})
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ end
d = Dict(i==1 ? (1=>2) : (2.0=>3.0) for i=1:2)
@test isa(d, Dict{Real,Real})
@test d == Dict{Real,Real}(2.0=>3.0, 1=>2)

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

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

0 comments on commit 38f3b1e

Please sign in to comment.