@@ -753,27 +753,32 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool, vars::Vector{TypeVar}=TypeV
753
753
return R
754
754
end
755
755
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
756
758
function limit_type_size (t:: ANY , compare:: ANY , source:: ANY )
757
759
source = svec (unwrap_unionall (compare), unwrap_unionall (source))
758
760
source[1 ] === source[2 ] && (source = svec (source[1 ]))
759
761
type_more_complex (t, compare, source, TUPLE_COMPLEXITY_LIMIT_DEPTH) || return t
760
762
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
762
767
return r
763
768
end
764
769
765
770
sym_isless (a:: Symbol , b:: Symbol ) = ccall (:strcmp , Int32, (Ptr{UInt8}, Ptr{UInt8}), a, b) < 0
766
771
767
772
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
769
774
if t === c
770
775
return false
771
776
elseif t === Union{}
772
777
return false # Bottom is as simple as they come
773
778
elseif isa (t, DataType) && isempty (t. parameters)
774
779
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
777
782
elseif tupledepth > 0 && is_derived_type_from_any (unwrap_unionall (t), sources)
778
783
return false # t isn't something new
779
784
end
@@ -828,7 +833,7 @@ function type_more_complex(t::ANY, c::ANY, sources::SimpleVector, tupledepth::In
828
833
! tPi. abstract && ! cPi. abstract &&
829
834
sym_isless (cPi. name. name, tPi. name. name)
830
835
# 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?
832
837
continue
833
838
end
834
839
end
@@ -903,8 +908,8 @@ function _limit_type_size(t::ANY, c::ANY, sources::SimpleVector) # type vs. comp
903
908
return t # easy case
904
909
elseif isa (t, DataType) && isempty (t. parameters)
905
910
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
908
913
elseif is_derived_type_from_any (unwrap_unionall (t), sources)
909
914
return t # t isn't something new
910
915
end
@@ -966,7 +971,7 @@ function _limit_type_size(t::ANY, c::ANY, sources::SimpleVector) # type vs. comp
966
971
Q[np] = tuple_tail_elem (Bottom, Any[ tP[i] for i in np: length (tP) ])
967
972
end
968
973
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)
970
975
end
971
976
return Tuple{Q... }
972
977
end
@@ -978,7 +983,18 @@ function _limit_type_size(t::ANY, c::ANY, sources::SimpleVector) # type vs. comp
978
983
is_derived_type_from_any (tt, sources) && return t
979
984
end
980
985
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
982
998
end
983
999
return Any
984
1000
end
0 commit comments