diff --git a/base/generator_eltype.jl b/base/generator_eltype.jl index c5c3d47d44890..24614f00e24cb 100644 --- a/base/generator_eltype.jl +++ b/base/generator_eltype.jl @@ -2,12 +2,14 @@ function eltype(::Type{Generator{A, typeof(identity)}}) where {A} eltype(A) end -function eltype(::Type{Generator{A, Fix1{typeof(getindex), B}}} where {A}) where {B} +function eltype(::Type{Generator{A, Fix1{typeof(getindex), B}}}) where {A, B} if B <: Type # a user may overload `getindex(user_type)` to return a non-`Vector` `AbstractVector` Any + elseif (eltype(A) == keytype(B)) || ((eltype(A) <: Integer) && (keytype(B) <: Integer)) + valtype(B) else - eltype(B) + Any end end diff --git a/test/iterators.jl b/test/iterators.jl index b6515827c1831..dfa7763ac3c58 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -1017,17 +1017,21 @@ end @testset "`eltype` for `Generator` involving `Fix` and `getindex`/`getfield` (issue #41519)" begin @testset "correct `eltype`" begin - f = Base.Fix1(getindex, Int) - i = [3, 7] - r = map(f, i) - t = Iterators.map(f, i) - @test eltype(r) <: @inferred eltype(t) - @test (@inferred Base.IteratorEltype(t)) isa Base.IteratorEltype + for (f, i) ∈ ( + (Base.Fix1(getindex, Int), [3, 7]), + (Base.Fix1(getindex, [3, 7]), [[1 2], [2 1]]), + ) + r = map(f, i) + t = Iterators.map(f, i) + @test eltype(r) <: @inferred eltype(t) + @test (@inferred Base.IteratorEltype(t)) isa Base.IteratorEltype + end end @testset "precise `eltype`" begin for (f, i) ∈ ( (identity, [3, 7]), (Base.Fix1(getindex, [3, 7]), 1:2), + (Base.Fix1(getindex, Dict("a" => 3, "b" => 7)), ["a", "b"]), (Base.Fix1(getfield, (; a = 3, b = 7)), 1:2), (Base.Fix1(getfield, (; a = 3, b = 7)), [:a, :b]), )