@@ -296,35 +296,50 @@ const tupleref_tfunc = function (A, t, i)
296
296
end
297
297
t_func[tupleref] = (2 , 2 , tupleref_tfunc)
298
298
299
- function quantify_type (t, vars)
300
- # all types of the form S{...,T,...} in invariant position where T
301
- # is a bound variable are replaced with _<:S
299
+ function limit_type_depth (t:: ANY , d:: Int , cov:: Bool , vars)
302
300
if isa (t,TypeVar) || isa (t,TypeConstructor)
303
301
return t
304
302
end
305
303
if isa (t,Tuple)
306
- return map (x-> quantify_type (x, vars), t)
307
- end
308
- if isa (t,UnionType)
309
- return Union (quantify_type (t. types, vars)... )
310
- end
311
- if isa (t,DataType)
304
+ if d > MAX_TYPE_DEPTH
305
+ R = Tuple
306
+ else
307
+ R = map (x-> limit_type_depth (x, d+ 1 , cov, vars), t)
308
+ end
309
+ elseif isa (t,UnionType)
310
+ if d > MAX_TYPE_DEPTH
311
+ R = Any
312
+ else
313
+ R = Union (limit_type_depth (t. types, d, cov, vars)... )
314
+ end
315
+ elseif isa (t,DataType)
312
316
P = t. parameters
313
317
if P === ()
314
318
return t
315
319
end
316
- P = quantify_type (P, vars)
317
- t = t. name. primary{P... }
318
- for i = 1 : length (P)
319
- Pi = P[i]
320
- if isa (Pi,TypeVar) && contains_is (vars, Pi)
321
- return TypeVar (:_ ,t)
320
+ if d > MAX_TYPE_DEPTH
321
+ R = t. name. primary
322
+ else
323
+ Q = map (x-> limit_type_depth (x, d+ 1 , false , vars), P)
324
+ if ! cov && any (p-> contains_is (vars,p), Q)
325
+ R = TypeVar (:_ ,t. name. primary)
326
+ push! (vars, R)
327
+ return R
328
+ else
329
+ R = t. name. primary{Q... }
322
330
end
323
331
end
332
+ else
333
+ return t
324
334
end
325
- return t
335
+ if ! cov && d > MAX_TYPE_DEPTH
336
+ R = TypeVar (:_ ,R)
337
+ push! (vars, R)
338
+ end
339
+ return R
326
340
end
327
341
342
+
328
343
const getfield_tfunc = function (A, s0, name)
329
344
s = s0
330
345
if isType (s)
@@ -362,11 +377,7 @@ const getfield_tfunc = function (A, s0, name)
362
377
end
363
378
for i= 1 : length (s. names)
364
379
if is (s. names[i],fld)
365
- ft = s. types[i]
366
- if isleaftype (s) || ! any (x-> isa (x,TypeVar), s. parameters)
367
- return ft
368
- end
369
- return quantify_type (ft, s. parameters)
380
+ return limit_type_depth (s. types[i], 0 , true , {s. parameters... })
370
381
end
371
382
end
372
383
return None
@@ -1165,6 +1176,9 @@ typeinf(linfo,atypes::ANY,sparams::ANY,def) = typeinf(linfo,atypes,sparams,def,t
1165
1176
1166
1177
CYCLE_ID = 1
1167
1178
1179
+ # trace_inf = false
1180
+ # enable_trace_inf() = (global trace_inf=true)
1181
+
1168
1182
# def is the original unspecialized version of a method. we aggregate all
1169
1183
# saved type inference data there.
1170
1184
function typeinf (linfo:: LambdaStaticData ,atypes:: Tuple ,sparams:: Tuple , def, cop)
@@ -1197,7 +1211,9 @@ function typeinf(linfo::LambdaStaticData,atypes::Tuple,sparams::Tuple, def, cop)
1197
1211
# if dbg
1198
1212
# print("typeinf ", linfo.name, " ", object_id(ast0), "\n")
1199
1213
# end
1200
- # print("typeinf ", linfo.name, " ", atypes, " ", linfo.file,":",linfo.line,"\n")
1214
+ # if trace_inf
1215
+ # print("typeinf ", linfo.name, " ", atypes, " ", linfo.file,":",linfo.line,"\n")
1216
+ # end
1201
1217
# if isdefined(:STDOUT)
1202
1218
# write(STDOUT, "typeinf ")
1203
1219
# write(STDOUT, string(linfo.name))
0 commit comments