@@ -208,6 +208,18 @@ add_tfunc(typeof, 1, 1, typeof_tfunc)
208
208
add_tfunc (typeassert, 2 , 2 ,
209
209
(A, v, t)-> (isType (t) ? typeintersect (v,t. parameters[1 ]) : Any))
210
210
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
+
211
223
function limit_type_depth (t:: ANY , d:: Int , cov:: Bool , vars)
212
224
if isa (t,TypeVar) || isa (t,TypeConstructor)
213
225
return t
@@ -1508,6 +1520,20 @@ function typeinf_uncached(linfo::LambdaStaticData, atypes::ANY, sparams::SimpleV
1508
1520
# print("typeinf ", ast0, " ", sparams, " ", atypes, "\n")
1509
1521
1510
1522
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
+
1511
1537
# check for recursion
1512
1538
f = inference_stack
1513
1539
while ! isa (f,EmptyCallStack)
0 commit comments