Skip to content

Inference discards bounds on abstract parameters #36454

@timholy

Description

@timholy

Apologies, I'm sure this must have been reported before but a search didn't pick it up.

There appear to be cases where it would be "easy" to preserve bounds on abstract types. One that comes up a lot in my invalidation-squashing is Iterators.Stateful:

julia> code_typed(Iterators.Stateful, (AbstractString,))
1-element Array{Any,1}:
 CodeInfo(
1%1 = invoke Base.Iterators.approx_iter_type($(Expr(:static_parameter, 1))::Type{T} where T)::Type%2 = Core.apply_type(Base.Iterators.Stateful, $(Expr(:static_parameter, 1)), %1)::Type{Base.Iterators.Stateful{_A,_B}} where _B where _A
│   %3 = Base.convert($(Expr(:static_parameter, 1)), itr)::Any%4 = Core.fieldtype(%2, 2)::Type{var"#s428"} where var"#s428"<:(Union{Nothing, _B} where _B)%5 = Base.Iterators.iterate(itr)::Any
│        Core.typeassert(%5, %1)::Any%7 = Base.convert(%4, %5)::Any%8 = %new(%2, %3, %7, 0)::Base.Iterators.Stateful{_A,_B} where _B where _A
└──      return %8
) => Base.Iterators.Stateful{_A,_B} where _B where _A

Given the definition

julia/base/iterators.jl

Lines 1243 to 1246 in d762e8c

@inline function Stateful(itr::T) where {T}
VS = approx_iter_type(T)
return new{T, VS}(itr, iterate(itr)::VS, 0)
end
it would naively seem fairly straightforward to retain _A<:AbstractString.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions