Skip to content

Commit a223bc7

Browse files
committed
address review comments
1 parent 57c24b2 commit a223bc7

File tree

1 file changed

+25
-9
lines changed

1 file changed

+25
-9
lines changed

base/inference.jl

+25-9
Original file line numberDiff line numberDiff line change
@@ -753,27 +753,32 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool, vars::Vector{TypeVar}=TypeV
753753
return R
754754
end
755755

756+
# limit the complexity of type `t` to be simpler than the comparison type `compare`
757+
# no new values may be introduced, so the parameter `source` encodes the set of all values already present
756758
function limit_type_size(t::ANY, compare::ANY, source::ANY)
757759
source = svec(unwrap_unionall(compare), unwrap_unionall(source))
758760
source[1] === source[2] && (source = svec(source[1]))
759761
type_more_complex(t, compare, source, TUPLE_COMPLEXITY_LIMIT_DEPTH) || return t
760762
r = _limit_type_size(t, compare, source)
761-
#@assert !isa(t, Type) || t <: r
763+
@assert t <: r
764+
#@assert r === _limit_type_size(r, t, source) # this monotonicity constraint is slightly stronger than actually required,
765+
# since we only actually need to demonstrate that repeated application would reaches a fixed point,
766+
#not that it is already at the fixed point
762767
return r
763768
end
764769

765770
sym_isless(a::Symbol, b::Symbol) = ccall(:strcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}), a, b) < 0
766771

767772
function type_more_complex(t::ANY, c::ANY, sources::SimpleVector, tupledepth::Int)
768-
# detect cases where the comparison is already trivially true
773+
# detect cases where the comparison is trivial
769774
if t === c
770775
return false
771776
elseif t === Union{}
772777
return false # Bottom is as simple as they come
773778
elseif isa(t, DataType) && isempty(t.parameters)
774779
return false # fastpath: unparameterized types are always finite
775-
elseif tupledepth > 0 && isa(t, Type) && isa(c, Type) && c !== Union{} && c <: t
776-
return false # t is already wider than the comparison
780+
elseif tupledepth > 0 && isa(unwrap_unionall(t), DataType) && isa(c, Type) && c !== Union{} && c <: t
781+
return false # t is already wider than the comparison in the type lattice
777782
elseif tupledepth > 0 && is_derived_type_from_any(unwrap_unionall(t), sources)
778783
return false # t isn't something new
779784
end
@@ -828,7 +833,7 @@ function type_more_complex(t::ANY, c::ANY, sources::SimpleVector, tupledepth::In
828833
!tPi.abstract && !cPi.abstract &&
829834
sym_isless(cPi.name.name, tPi.name.name)
830835
# allow collect on (anonymous) Generators to nest, provided that their functions are appropriately ordered
831-
# TODO: is there a better way
836+
# TODO: is there a better way?
832837
continue
833838
end
834839
end
@@ -903,8 +908,8 @@ function _limit_type_size(t::ANY, c::ANY, sources::SimpleVector) # type vs. comp
903908
return t # easy case
904909
elseif isa(t, DataType) && isempty(t.parameters)
905910
return t # fast path: unparameterized are always simple
906-
elseif isa(t, Type) && isa(c, Type) && c !== Union{} && c <: t
907-
return t # t is already wider than the comparison
911+
elseif isa(unwrap_unionall(t), DataType) && isa(c, Type) && c !== Union{} && c <: t
912+
return t # t is already wider than the comparison in the type lattice
908913
elseif is_derived_type_from_any(unwrap_unionall(t), sources)
909914
return t # t isn't something new
910915
end
@@ -966,7 +971,7 @@ function _limit_type_size(t::ANY, c::ANY, sources::SimpleVector) # type vs. comp
966971
Q[np] = tuple_tail_elem(Bottom, Any[ tP[i] for i in np:length(tP) ])
967972
end
968973
for i = 1:np
969-
Q[i] = _limit_type_size(Q[i], cP[i], sources) # TODO: apply type_more_complex here?
974+
Q[i] = _limit_type_size(Q[i], cP[i], sources)
970975
end
971976
return Tuple{Q...}
972977
end
@@ -978,7 +983,18 @@ function _limit_type_size(t::ANY, c::ANY, sources::SimpleVector) # type vs. comp
978983
is_derived_type_from_any(tt, sources) && return t
979984
end
980985
end
981-
return t.name.wrapper
986+
if isvarargtype(t)
987+
# never replace Vararg with non-Vararg
988+
return Vararg
989+
end
990+
widert = t.name.wrapper
991+
if !(t <: widert)
992+
# This can happen when a typevar has bounds too wide for its context, e.g.
993+
# `Complex{T} where T` is not a subtype of `Complex`. In that case widen even
994+
# faster to something safe to ensure the result is a supertype of the input.
995+
return Any
996+
end
997+
return widert
982998
end
983999
return Any
9841000
end

0 commit comments

Comments
 (0)