Skip to content

Commit 9d9814b

Browse files
committed
fix #14009; check for types growing due to recursion and widen them
1 parent bbbd9d2 commit 9d9814b

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

base/inference.jl

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ add_tfunc(typeof, 1, 1, typeof_tfunc)
208208
add_tfunc(typeassert, 2, 2,
209209
(A, v, t)->(isType(t) ? typeintersect(v,t.parameters[1]) : Any))
210210

211+
function type_depth(t::ANY, d::Int=0)
212+
if isa(t,Union)
213+
t === Bottom && return d
214+
return maximum(x->type_depth(x, d+1), t.types)
215+
elseif isa(t,DataType)
216+
P = t.parameters
217+
isempty(P) && return d
218+
return maximum(x->type_depth(x, d+1), P)
219+
end
220+
return d
221+
end
222+
211223
function limit_type_depth(t::ANY, d::Int, cov::Bool, vars)
212224
if isa(t,TypeVar) || isa(t,TypeConstructor)
213225
return t
@@ -1508,6 +1520,20 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV
15081520
#print("typeinf ", ast0, " ", sparams, " ", atypes, "\n")
15091521

15101522
global inference_stack, CYCLE_ID
1523+
1524+
f = inference_stack
1525+
while !isa(f,EmptyCallStack)
1526+
if is(f.ast,ast0)
1527+
# impose limit if we recur and the argument types grow beyond MAX_TYPE_DEPTH
1528+
td = type_depth(atypes)
1529+
if td > MAX_TYPE_DEPTH && td > type_depth(f.types)
1530+
atypes = limit_type_depth(atypes, 0, true, [])
1531+
break
1532+
end
1533+
end
1534+
f = f.prev
1535+
end
1536+
15111537
# check for recursion
15121538
f = inference_stack
15131539
while !isa(f,EmptyCallStack)

test/core.jl

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3586,3 +3586,13 @@ function __f_isa_arg_1()
35863586
length(a)
35873587
end
35883588
@test __f_isa_arg_1() == 1
3589+
3590+
# non-terminating inference, issue #14009
3591+
type A14009{T}; end
3592+
A14009{T}(a::T) = A14009{T}()
3593+
f14009(a) = rand(Bool) ? f14009(A14009(a)) : a
3594+
code_typed(f14009, (Int,))
3595+
3596+
type B14009{T}; end
3597+
g14009(a) = g14009(B14009{a})
3598+
code_typed(g14009, (Type{Int},))

0 commit comments

Comments
 (0)