@@ -660,21 +660,27 @@ function type_depth(t::ANY)
660
660
return 0
661
661
end
662
662
663
- function limit_type_depth (t:: ANY , d:: Int , cov:: Bool = true , var:: Union{Void,TypeVar} = nothing )
663
+ function limit_type_depth (t:: ANY , d:: Int )
664
+ r = limit_type_depth (t, d, true , TypeVar[])
665
+ @assert ! isa (t, Type) || t <: r
666
+ return r
667
+ end
668
+
669
+ function limit_type_depth (t:: ANY , d:: Int , cov:: Bool , vars:: Vector{TypeVar} = TypeVar[])
664
670
if isa (t,Union)
665
671
if d > MAX_TYPE_DEPTH
666
672
return Any
667
673
end
668
- return Union{map (x-> limit_type_depth (x, d+ 1 , cov, var ), (t. a,t. b))... }
674
+ return Union{map (x-> limit_type_depth (x, d+ 1 , cov, vars ), (t. a,t. b))... }
669
675
elseif isa (t,UnionAll)
670
676
v = t. var
671
677
if v. ub === Any
672
678
if v. lb === Bottom
673
- return UnionAll (t. var, limit_type_depth (t. body, d, cov, var ))
679
+ return UnionAll (t. var, limit_type_depth (t. body, d, cov, vars ))
674
680
end
675
681
ub = Any
676
682
else
677
- ub = limit_type_depth (v. ub, d+ 1 , true , nothing )
683
+ ub = limit_type_depth (v. ub, d+ 1 , true )
678
684
end
679
685
if v. lb === Bottom || type_depth (v. lb) > MAX_TYPE_DEPTH
680
686
# note: lower bounds need to be widened by making them lower
@@ -683,25 +689,27 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool=true, var::Union{Void,TypeVa
683
689
lb = v. lb
684
690
end
685
691
v2 = TypeVar (v. name, lb, ub)
686
- return UnionAll (v2, limit_type_depth (t{v2}, d, cov, var ))
692
+ return UnionAll (v2, limit_type_depth (t{v2}, d, cov, vars ))
687
693
elseif ! isa (t,DataType)
688
694
return t
689
695
end
690
696
P = t. parameters
691
697
isempty (P) && return t
692
698
if d > MAX_TYPE_DEPTH
693
699
cov && return t. name. wrapper
694
- # TODO mutating a TypeVar is not great style
695
- var . ub = t . name . wrapper
700
+ var = TypeVar ( :_ , t . name . wrapper)
701
+ push! (vars, var)
696
702
return var
697
703
end
698
704
stillcov = cov && (t. name === Tuple. name)
705
+ Q = map (x-> limit_type_depth (x, d+ 1 , stillcov, vars), P)
706
+ R = t. name. wrapper{Q... }
699
707
if cov && ! stillcov
700
- var = TypeVar (:_ )
708
+ for var in vars
709
+ R = UnionAll (var, R)
710
+ end
701
711
end
702
- Q = map (x-> limit_type_depth (x, d+ 1 , stillcov, var), P)
703
- R = t. name. wrapper{Q... }
704
- return (cov && ! stillcov) ? UnionAll (var, R) : R
712
+ return R
705
713
end
706
714
707
715
const DataType_name_fieldindex = fieldindex (DataType, :name )
0 commit comments