Skip to content

Commit 66c9f6a

Browse files
authored
fix #39117, overly-specific type from default_eltype (#39130)
1 parent 59eb9f9 commit 66c9f6a

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

base/array.jl

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,6 +624,20 @@ function _collect_indices(indsA, A)
624624
copyto!(B, CartesianIndices(axes(B)), A, CartesianIndices(indsA))
625625
end
626626

627+
# NOTE: this function is not meant to be called, only inferred, for the
628+
# purpose of bounding the types of values generated by an iterator.
629+
function _iterator_upper_bound(itr)
630+
x = iterate(itr)
631+
while x !== nothing
632+
val = getfield(x, 1)
633+
if inferencebarrier(nothing)
634+
return val
635+
end
636+
x = iterate(itr, getfield(x, 2))
637+
end
638+
throw(nothing)
639+
end
640+
627641
# define this as a macro so that the call to Core.Compiler
628642
# gets inlined into the caller before recursion detection
629643
# gets a chance to see it, so that recursive calls to the caller
@@ -635,7 +649,7 @@ if isdefined(Core, :Compiler)
635649
if $I isa Generator && ($I).f isa Type
636650
($I).f
637651
else
638-
Core.Compiler.return_type(first, Tuple{typeof($I)})
652+
Core.Compiler.return_type(_iterator_upper_bound, Tuple{typeof($I)})
639653
end
640654
end
641655
end

test/dict.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@ end
159159
d = Dict(i==1 ? (1=>2) : (2.0=>3.0) for i=1:2)
160160
@test isa(d, Dict{Real,Real})
161161
@test d == Dict{Real,Real}(2.0=>3.0, 1=>2)
162+
163+
# issue #39117
164+
@test Dict(t[1]=>t[2] for t in zip((1,"2"), (2,"2"))) == Dict{Any,Any}(1=>2, "2"=>"2")
162165
end
163166

164167
@testset "type of Dict constructed from varargs of Pairs" begin

0 commit comments

Comments
 (0)