Skip to content

Commit 5e8f78a

Browse files
committed
Allow limit_type_depth to introduce more than one new TypeVar
Fixes #20615.
1 parent cb1aae9 commit 5e8f78a

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

base/inference.jl

+9-12
Original file line numberDiff line numberDiff line change
@@ -660,21 +660,21 @@ function type_depth(t::ANY)
660660
return 0
661661
end
662662

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, cov::Bool=true, vars::Vector{TypeVar}=TypeVar[])
664664
if isa(t,Union)
665665
if d > MAX_TYPE_DEPTH
666666
return Any
667667
end
668-
return Union{map(x->limit_type_depth(x, d+1, cov, var), (t.a,t.b))...}
668+
return Union{map(x->limit_type_depth(x, d+1, cov, vars), (t.a,t.b))...}
669669
elseif isa(t,UnionAll)
670670
v = t.var
671671
if v.ub === Any
672672
if v.lb === Bottom
673-
return UnionAll(t.var, limit_type_depth(t.body, d, cov, var))
673+
return UnionAll(t.var, limit_type_depth(t.body, d, cov, vars))
674674
end
675675
ub = Any
676676
else
677-
ub = limit_type_depth(v.ub, d+1, true, nothing)
677+
ub = limit_type_depth(v.ub, d+1, true)
678678
end
679679
if v.lb === Bottom || type_depth(v.lb) > MAX_TYPE_DEPTH
680680
# note: lower bounds need to be widened by making them lower
@@ -683,25 +683,22 @@ function limit_type_depth(t::ANY, d::Int, cov::Bool=true, var::Union{Void,TypeVa
683683
lb = v.lb
684684
end
685685
v2 = TypeVar(v.name, lb, ub)
686-
return UnionAll(v2, limit_type_depth(t{v2}, d, cov, var))
686+
return UnionAll(v2, limit_type_depth(t{v2}, d, cov, vars))
687687
elseif !isa(t,DataType)
688688
return t
689689
end
690690
P = t.parameters
691691
isempty(P) && return t
692692
if d > MAX_TYPE_DEPTH
693693
cov && return t.name.wrapper
694-
# TODO mutating a TypeVar is not great style
695-
var.ub = t.name.wrapper
694+
var = TypeVar(gensym(), t.name.wrapper)
695+
push!(vars, var)
696696
return var
697697
end
698698
stillcov = cov && (t.name === Tuple.name)
699-
if cov && !stillcov
700-
var = TypeVar(:_)
701-
end
702-
Q = map(x->limit_type_depth(x, d+1, stillcov, var), P)
699+
Q = map(x->limit_type_depth(x, d+1, stillcov, vars), P)
703700
R = t.name.wrapper{Q...}
704-
return (cov && !stillcov) ? UnionAll(var, R) : R
701+
return (cov && !stillcov) ? foldr(UnionAll, R, vars) : R
705702
end
706703

707704
const DataType_name_fieldindex = fieldindex(DataType, :name)

test/inference.jl

+6
Original file line numberDiff line numberDiff line change
@@ -642,3 +642,9 @@ end
642642
# infinite type growth via lower bounds (formed by intersection)
643643
f20267(x::T20267{T}, y::T) where (T) = f20267(Any[1][1], x.inds)
644644
@test Base.return_types(f20267, (Any, Any)) == Any[Union{}]
645+
646+
# issue #20615
647+
let A = 1:2, z = zip(A, A, A, A, A, A, A, A, A, A, A, A)
648+
@test z isa Core.Inference.limit_type_depth(typeof(z), 0)
649+
@test start(z) == (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, (1, 1)))))))))))
650+
end

0 commit comments

Comments
 (0)