Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better inference for _apply of iterable #12579

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion base/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,26 @@ function precise_container_types(args, types, vtypes, sv)
return result
end

# simulate iteration protocol on container type up to fixpoint
function abstract_iteration(ty, vtypes, sv)
it = abstract_call(getfield(_topmod(),:start), Any[:_], Any[ty], vtypes, sv, :(start(_)))
isleaftype(it) || return Any
elem = Bottom
while true
state = abstract_call(getfield(_topmod(),:next), Any[:_0,:_1], Any[ty, it], vtypes, sv, :(next(_0,_1)))
isleaftype(state) || return Any
state<:Tuple && !isvatuple(state) && length(state.parameters) == 2 || return Any
it === state.parameters[2] || return Any # only handle iteration where the iteration state has constant type
new_elem = tmerge(elem, state.parameters[1])
if (new_elem <: elem)
break
else
elem = new_elem
end
end
elem
end

# do apply(af, fargs...), where af is a function value
function abstract_apply(af, fargs, aargtypes::Vector{Any}, vtypes, sv, e)
ctypes = precise_container_types(fargs, aargtypes, vtypes, sv)
Expand Down Expand Up @@ -819,8 +839,12 @@ function abstract_apply(af, fargs, aargtypes::Vector{Any}, vtypes, sv, e)
return Tuple
end
is(af,kwcall) && return Any
allargs_type = Any
if length(aargtypes) >= 1
allargs_type = reduce(tmerge, map(aty -> abstract_iteration(aty, vtypes, sv), aargtypes))
end
# apply known function with unknown args => f(Any...)
return abstract_call(af, (), Any[Vararg{Any}], vtypes, sv, ())
return abstract_call(af, (), Any[Vararg{allargs_type}], vtypes, sv, ())
end

function abstract_call(f, fargs, argtypes::Vector{Any}, vtypes, sv::StaticVarInfo, e)
Expand Down