-
Notifications
You must be signed in to change notification settings - Fork 1k
/
OrderingConstraint.scala
658 lines (574 loc) · 26 KB
/
OrderingConstraint.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
package dotty.tools
package dotc
package core
import Types._, Contexts._, Symbols._, Decorators._, TypeApplications._
import util.SimpleIdentityMap
import collection.mutable
import printing.Printer
import printing.Texts._
import config.Config
import config.Printers.constr
import reflect.ClassTag
import annotation.tailrec
import annotation.internal.sharable
object OrderingConstraint {
type ArrayValuedMap[T] = SimpleIdentityMap[TypeLambda, Array[T]]
/** The type of `OrderingConstraint#boundsMap` */
type ParamBounds = ArrayValuedMap[Type]
/** The type of `OrderingConstraint#lowerMap`, `OrderingConstraint#upperMap` */
type ParamOrdering = ArrayValuedMap[List[TypeParamRef]]
/** A new constraint with given maps */
private def newConstraint(boundsMap: ParamBounds, lowerMap: ParamOrdering, upperMap: ParamOrdering)(using Context) : OrderingConstraint =
if boundsMap.isEmpty && lowerMap.isEmpty && upperMap.isEmpty then
empty
else
val result = new OrderingConstraint(boundsMap, lowerMap, upperMap)
ctx.run.nn.recordConstraintSize(result, result.boundsMap.size)
result
/** A lens for updating a single entry array in one of the three constraint maps */
abstract class ConstraintLens[T <: AnyRef: ClassTag] {
def entries(c: OrderingConstraint, poly: TypeLambda): Array[T] | Null
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[T])(using Context): OrderingConstraint
def initial: T
def apply(c: OrderingConstraint, poly: TypeLambda, idx: Int): T = {
val es = entries(c, poly)
if (es == null) initial else es(idx)
}
/** The `current` constraint but with the entry for `param` updated to `entry`.
* `current` is used linearly. If it is different from `prev` it is
* known to be dead after the call. Hence it is OK to update destructively
* parts of `current` which are not shared by `prev`.
*/
def update(prev: OrderingConstraint, current: OrderingConstraint,
poly: TypeLambda, idx: Int, entry: T)(using Context): OrderingConstraint = {
var es = entries(current, poly)
// TODO: investigate why flow typing is not working on `es`
if (es != null && (es.nn(idx) eq entry)) current
else {
val result =
if (es == null) {
es = Array.fill(poly.paramNames.length)(initial)
updateEntries(current, poly, es.nn)
}
else {
val prev_es = entries(prev, poly)
if (prev_es == null || (es.nn ne prev_es.nn))
current // can re-use existing entries array.
else {
es = es.nn.clone
updateEntries(current, poly, es.nn)
}
}
es.nn(idx) = entry
result
}
}
def update(prev: OrderingConstraint, current: OrderingConstraint,
param: TypeParamRef, entry: T)(using Context): OrderingConstraint =
update(prev, current, param.binder, param.paramNum, entry)
def map(prev: OrderingConstraint, current: OrderingConstraint,
poly: TypeLambda, idx: Int, f: T => T)(using Context): OrderingConstraint =
update(prev, current, poly, idx, f(apply(current, poly, idx)))
def map(prev: OrderingConstraint, current: OrderingConstraint,
param: TypeParamRef, f: T => T)(using Context): OrderingConstraint =
map(prev, current, param.binder, param.paramNum, f)
}
val boundsLens: ConstraintLens[Type] = new ConstraintLens[Type] {
def entries(c: OrderingConstraint, poly: TypeLambda): Array[Type] | Null =
c.boundsMap(poly)
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[Type])(using Context): OrderingConstraint =
newConstraint(c.boundsMap.updated(poly, entries), c.lowerMap, c.upperMap)
def initial = NoType
}
val lowerLens: ConstraintLens[List[TypeParamRef]] = new ConstraintLens[List[TypeParamRef]] {
def entries(c: OrderingConstraint, poly: TypeLambda): Array[List[TypeParamRef]] | Null =
c.lowerMap(poly)
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[List[TypeParamRef]])(using Context): OrderingConstraint =
newConstraint(c.boundsMap, c.lowerMap.updated(poly, entries), c.upperMap)
def initial = Nil
}
val upperLens: ConstraintLens[List[TypeParamRef]] = new ConstraintLens[List[TypeParamRef]] {
def entries(c: OrderingConstraint, poly: TypeLambda): Array[List[TypeParamRef]] | Null =
c.upperMap(poly)
def updateEntries(c: OrderingConstraint, poly: TypeLambda, entries: Array[List[TypeParamRef]])(using Context): OrderingConstraint =
newConstraint(c.boundsMap, c.lowerMap, c.upperMap.updated(poly, entries))
def initial = Nil
}
@sharable
val empty = new OrderingConstraint(SimpleIdentityMap.empty, SimpleIdentityMap.empty, SimpleIdentityMap.empty)
}
import OrderingConstraint._
/** Constraint over undetermined type parameters that keeps separate maps to
* reflect parameter orderings.
* @param boundsMap a map from TypeLambda to arrays.
* Each array contains twice the number of entries as there a type parameters
* in the TypeLambda. The first half of the array contains the type bounds that constrain the
* lambda's type parameters. The second half might contain type variables that
* track the corresponding parameters, or is left empty (filled with nulls).
* An instantiated type parameter is represented by having its instance type in
* the corresponding array entry. The dual use of arrays for poly params
* and typevars is to save space and hopefully gain some speed.
*
* @param lowerMap a map from TypeLambdas to arrays. Each array entry corresponds
* to a parameter P of the type lambda; it contains all constrained parameters
* Q that are known to be smaller than P, i.e. Q <: P.
* @param upperMap a map from TypeLambdas to arrays. Each array entry corresponds
* to a parameter P of the type lambda; it contains all constrained parameters
* Q that are known to be greater than P, i.e. P <: Q.
*/
class OrderingConstraint(private val boundsMap: ParamBounds,
private val lowerMap : ParamOrdering,
private val upperMap : ParamOrdering) extends Constraint {
import UnificationDirection.*
type This = OrderingConstraint
// ----------- Basic indices --------------------------------------------------
/** The number of type parameters in the given entry array */
private def paramCount(entries: Array[Type]) = entries.length >> 1
/** The type variable corresponding to parameter numbered `n`, null if none was created */
private def typeVar(entries: Array[Type], n: Int): Type | Null =
entries(paramCount(entries) + n)
/** The `boundsMap` entry corresponding to `param` */
def entry(param: TypeParamRef): Type = {
val entries = boundsMap(param.binder)
if (entries == null) NoType
else entries(param.paramNum)
}
// ----------- Contains tests --------------------------------------------------
def contains(pt: TypeLambda): Boolean = boundsMap(pt) != null
def contains(param: TypeParamRef): Boolean = {
val entries = boundsMap(param.binder)
entries != null && isBounds(entries(param.paramNum))
}
def contains(tvar: TypeVar): Boolean = {
val origin = tvar.origin
val entries = boundsMap(origin.binder)
val pnum = origin.paramNum
entries != null && isBounds(entries(pnum)) && (typeVar(entries, pnum) eq tvar)
}
// ---------- Dependency handling ----------------------------------------------
def lower(param: TypeParamRef): List[TypeParamRef] = lowerLens(this, param.binder, param.paramNum)
def upper(param: TypeParamRef): List[TypeParamRef] = upperLens(this, param.binder, param.paramNum)
def minLower(param: TypeParamRef): List[TypeParamRef] = {
val all = lower(param)
all.filterNot(p => all.exists(isLess(p, _)))
}
def minUpper(param: TypeParamRef): List[TypeParamRef] = {
val all = upper(param)
all.filterNot(p => all.exists(isLess(_, p)))
}
def exclusiveLower(param: TypeParamRef, butNot: TypeParamRef): List[TypeParamRef] =
lower(param).filterNot(isLess(_, butNot))
def exclusiveUpper(param: TypeParamRef, butNot: TypeParamRef): List[TypeParamRef] =
upper(param).filterNot(isLess(butNot, _))
// ---------- Info related to TypeParamRefs -------------------------------------------
def isLess(param1: TypeParamRef, param2: TypeParamRef): Boolean =
upper(param1).contains(param2)
def nonParamBounds(param: TypeParamRef)(using Context): TypeBounds =
entry(param).bounds
def typeVarOfParam(param: TypeParamRef): Type =
val entries = boundsMap(param.binder)
if entries == null then NoType
else
val tvar = typeVar(entries, param.paramNum)
if tvar == null then NoType
else tvar
// ---------- Adding TypeLambdas --------------------------------------------------
/** The bound type `tp` without constrained parameters which are clearly
* dependent. A parameter in an upper bound is clearly dependent if it appears
* in a hole of a context H given by:
*
* H = []
* H & T
* T & H
*
* (the idea is that a parameter P in a H context is guaranteed to be a supertype of the
* bounded parameter.)
* Analogously, a parameter in a lower bound is clearly dependent if it appears
* in a hole of a context H given by:
*
* L = []
* L | T
* T | L
*
* "Clearly dependent" is not synonymous with "dependent" in the sense
* it is defined in `dependentParams`. Dependent parameters are handled
* in `updateEntry`. The idea of stripping off clearly dependent parameters
* and to handle them separately is for efficiency, so that type expressions
* used as bounds become smaller.
*
* TODO: try to do without stripping? It would mean it is more efficient
* to pull out full bounds from a constraint.
*
* @param isUpper If true, `bound` is an upper bound, else a lower bound.
*/
private def stripParams(
tp: Type,
todos: mutable.ListBuffer[(OrderingConstraint, TypeParamRef) => OrderingConstraint],
isUpper: Boolean)(using Context): Type = tp match {
case param: TypeParamRef if contains(param) =>
todos += (if isUpper then order(_, _, param) else order(_, param, _))
NoType
case tp: TypeBounds =>
val lo1 = stripParams(tp.lo, todos, !isUpper).orElse(defn.NothingType)
val hi1 = stripParams(tp.hi, todos, isUpper).orElse(tp.topType)
tp.derivedTypeBounds(lo1, hi1)
case tp: AndType if isUpper =>
val tp1 = stripParams(tp.tp1, todos, isUpper)
val tp2 = stripParams(tp.tp2, todos, isUpper)
if (tp1.exists)
if (tp2.exists) tp.derivedAndType(tp1, tp2)
else tp1
else tp2
case tp: OrType if !isUpper =>
val tp1 = stripParams(tp.tp1, todos, isUpper)
val tp2 = stripParams(tp.tp2, todos, isUpper)
if (tp1.exists)
if (tp2.exists) tp.derivedOrType(tp1, tp2)
else tp1
else tp2
case _ =>
tp
}
def add(poly: TypeLambda, tvars: List[TypeVar])(using Context): This = {
assert(!contains(poly))
val nparams = poly.paramNames.length
val entries1 = new Array[Type](nparams * 2)
poly.paramInfos.copyToArray(entries1, 0)
tvars.copyToArray(entries1, nparams)
newConstraint(boundsMap.updated(poly, entries1), lowerMap, upperMap).init(poly)
}
/** Split dependent parameters off the bounds for parameters in `poly`.
* Update all bounds to be normalized and update ordering to account for
* dependent parameters.
*/
private def init(poly: TypeLambda)(using Context): This = {
var current = this
val todos = new mutable.ListBuffer[(OrderingConstraint, TypeParamRef) => OrderingConstraint]
var i = 0
val dropWildcards = AvoidWildcardsMap()
while (i < poly.paramNames.length) {
val param = poly.paramRefs(i)
val bounds = dropWildcards(nonParamBounds(param))
val stripped = stripParams(bounds, todos, isUpper = true)
current = updateEntry(current, param, stripped)
while todos.nonEmpty do
current = todos.head(current, param)
todos.dropInPlace(1)
i += 1
}
current.checkNonCyclic()
}
// ---------- Updates ------------------------------------------------------------
/** If `inst` is a TypeBounds, make sure it does not contain toplevel
* references to `param` (see `Constraint#occursAtToplevel` for a definition
* of "toplevel").
* Any such references are replaced by `Nothing` in the lower bound and `Any`
* in the upper bound.
* References can be direct or indirect through instantiations of other
* parameters in the constraint.
*/
private def ensureNonCyclic(param: TypeParamRef, inst: Type)(using Context): Type =
def recur(tp: Type, fromBelow: Boolean): Type = tp match
case tp: AndOrType =>
val r1 = recur(tp.tp1, fromBelow)
val r2 = recur(tp.tp2, fromBelow)
if (r1 eq tp.tp1) && (r2 eq tp.tp2) then tp
else tp.match
case tp: OrType =>
TypeComparer.lub(r1, r2, isSoft = tp.isSoft)
case _ =>
r1 & r2
case tp: TypeParamRef =>
if tp eq param then
if fromBelow then defn.NothingType else defn.AnyType
else entry(tp) match
case NoType => tp
case TypeBounds(lo, hi) => if lo eq hi then recur(lo, fromBelow) else tp
case inst => recur(inst, fromBelow)
case tp: TypeVar =>
val underlying1 = recur(tp.underlying, fromBelow)
if underlying1 ne tp.underlying then underlying1 else tp
case tp: AnnotatedType =>
val parent1 = recur(tp.parent, fromBelow)
if parent1 ne tp.parent then tp.derivedAnnotatedType(parent1, tp.annot) else tp
case _ =>
val tp1 = tp.dealiasKeepAnnots
if tp1 ne tp then
val tp2 = recur(tp1, fromBelow)
if tp2 ne tp1 then tp2 else tp
else tp
inst match
case bounds: TypeBounds =>
bounds.derivedTypeBounds(
recur(bounds.lo, fromBelow = true),
recur(bounds.hi, fromBelow = false))
case _ =>
inst
end ensureNonCyclic
/** Add the fact `param1 <: param2` to the constraint `current` and propagate
* `<:<` relationships between parameters ("edges") but not bounds.
*/
def order(current: This, param1: TypeParamRef, param2: TypeParamRef, direction: UnificationDirection = NoUnification)(using Context): This =
// /!\ Careful here: we're adding constraints on `current`, not `this`, so
// think twice when using an instance method! We only need to pass `this` as
// the `prev` argument in methods on `ConstraintLens`.
// TODO: Refactor this code to take `prev` as a parameter and add
// constraints on `this` instead?
if param1 == param2 || current.isLess(param1, param2) then current
else
assert(current.contains(param1), i"$param1")
assert(current.contains(param2), i"$param2")
val unifying = direction != NoUnification
val newUpper = {
val up = current.exclusiveUpper(param2, param1)
if unifying then
// Since param2 <:< param1 already holds now, filter out param1 to avoid adding
// duplicated orderings.
val filtered = up.filterNot(_ eq param1)
// Only add bounds for param2 if it will be kept in the constraint after unification.
if direction == KeepParam2 then
param2 :: filtered
else
filtered
else
param2 :: up
}
val newLower = {
val lower = current.exclusiveLower(param1, param2)
if unifying then
// Similarly, filter out param2 from lowerly-ordered parameters
// to avoid duplicated orderings.
val filtered = lower.filterNot(_ eq param2)
// Only add bounds for param1 if it will be kept in the constraint after unification.
if direction == KeepParam1 then
param1 :: filtered
else
filtered
else
param1 :: lower
}
val current1 = newLower.foldLeft(current)(upperLens.map(this, _, _, newUpper ::: _))
val current2 = newUpper.foldLeft(current1)(lowerLens.map(this, _, _, newLower ::: _))
current2
end if
end order
/** The list of parameters P such that, for a fresh type parameter Q:
*
* Q <: tp implies Q <: P and isUpper = true, or
* tp <: Q implies P <: Q and isUpper = false
*/
private def dependentParams(tp: Type, isUpper: Boolean)(using Context): List[TypeParamRef] = tp match
case param: TypeParamRef if contains(param) =>
param :: (if (isUpper) upper(param) else lower(param))
case tp: AndType if isUpper =>
dependentParams(tp.tp1, isUpper) | (dependentParams(tp.tp2, isUpper))
case tp: OrType if !isUpper =>
dependentParams(tp.tp1, isUpper).intersect(dependentParams(tp.tp2, isUpper))
case EtaExpansion(tycon) =>
dependentParams(tycon, isUpper)
case _ =>
Nil
private def updateEntry(current: This, param: TypeParamRef, tp: Type)(using Context): This = {
if Config.checkNoWildcardsInConstraint then assert(!tp.containsWildcardTypes)
var current1 = boundsLens.update(this, current, param, tp)
tp match {
case TypeBounds(lo, hi) =>
for p <- dependentParams(lo, isUpper = false) do
current1 = order(current1, p, param)
for p <- dependentParams(hi, isUpper = true) do
current1 = order(current1, param, p)
case _ =>
}
current1
}
/** The public version of `updateEntry`. Guarantees that there are no cycles */
def updateEntry(param: TypeParamRef, tp: Type)(using Context): This =
updateEntry(this, param, ensureNonCyclic(param, tp)).checkNonCyclic()
def addLess(param1: TypeParamRef, param2: TypeParamRef, direction: UnificationDirection)(using Context): This =
order(this, param1, param2, direction).checkNonCyclic()
// ---------- Replacements and Removals -------------------------------------
/** A new constraint which is derived from this constraint by removing
* the type parameter `param` from the domain and replacing all top-level occurrences
* of the parameter elsewhere in the constraint by type `tp`.
*/
def replace(param: TypeParamRef, tp: Type)(using Context): OrderingConstraint =
val replacement = tp.dealiasKeepAnnots.stripTypeVar
if param == replacement then this.checkNonCyclic()
else
assert(replacement.isValueTypeOrLambda)
var current =
if isRemovable(param.binder) then remove(param.binder)
else updateEntry(this, param, replacement)
def removeParam(ps: List[TypeParamRef]) = ps.filterConserve(param ne _)
def replaceParam(tp: Type, atPoly: TypeLambda, atIdx: Int): Type =
current.ensureNonCyclic(atPoly.paramRefs(atIdx), tp.substParam(param, replacement))
current.foreachParam { (p, i) =>
current = boundsLens.map(this, current, p, i, replaceParam(_, p, i))
current = lowerLens.map(this, current, p, i, removeParam)
current = upperLens.map(this, current, p, i, removeParam)
}
current.checkNonCyclic()
end replace
def remove(pt: TypeLambda)(using Context): This = {
def removeFromOrdering(po: ParamOrdering) = {
def removeFromBoundss(key: TypeLambda, bndss: Array[List[TypeParamRef]]): Array[List[TypeParamRef]] = {
val bndss1 = bndss.map(_.filterConserve(_.binder ne pt))
if (bndss.corresponds(bndss1)(_ eq _)) bndss else bndss1
}
po.remove(pt).mapValuesNow(removeFromBoundss)
}
newConstraint(boundsMap.remove(pt), removeFromOrdering(lowerMap), removeFromOrdering(upperMap))
.checkNonCyclic()
}
def isRemovable(pt: TypeLambda): Boolean = {
val entries = boundsMap(pt).nn
@tailrec def allRemovable(last: Int): Boolean =
if (last < 0) true
else typeVar(entries, last) match {
case tv: TypeVar => tv.inst.exists && allRemovable(last - 1)
case _ => false
}
allRemovable(paramCount(entries) - 1)
}
// ----------- Joins -----------------------------------------------------
def hasConflictingTypeVarsFor(tl: TypeLambda, that: Constraint): Boolean =
contains(tl) && that.contains(tl) &&
// Since TypeVars are allocated in bulk for each type lambda, we only have
// to check the first one to find out if some of them are different.
(this.typeVarOfParam(tl.paramRefs(0)) ne that.typeVarOfParam(tl.paramRefs(0)))
def subst(from: TypeLambda, to: TypeLambda)(using Context): OrderingConstraint =
def swapKey[T](m: ArrayValuedMap[T]) = m.remove(from).updated(to, m(from).nn)
var current = newConstraint(swapKey(boundsMap), swapKey(lowerMap), swapKey(upperMap))
def subst[T <: Type](x: T): T = x.subst(from, to).asInstanceOf[T]
current.foreachParam {(p, i) =>
current = boundsLens.map(this, current, p, i, subst)
current = lowerLens.map(this, current, p, i, _.map(subst))
current = upperLens.map(this, current, p, i, _.map(subst))
}
constr.println(i"renamed $this to $current")
current.checkNonCyclic()
def instType(tvar: TypeVar): Type = entry(tvar.origin) match
case _: TypeBounds => NoType
case tp: TypeParamRef => typeVarOfParam(tp).orElse(tp)
case tp => tp
def ensureFresh(tl: TypeLambda)(using Context): TypeLambda =
if (contains(tl)) {
var paramInfos = tl.paramInfos
if (tl.isInstanceOf[HKLambda]) {
// HKLambdas are hash-consed, need to create an artificial difference by adding
// a LazyRef to a bound.
val TypeBounds(lo, hi) :: pinfos1 = tl.paramInfos: @unchecked
paramInfos = TypeBounds(lo, LazyRef.of(hi)) :: pinfos1
}
ensureFresh(tl.newLikeThis(tl.paramNames, paramInfos, tl.resultType))
}
else tl
def checkConsistentVars()(using Context): Unit =
for param <- domainParams do
typeVarOfParam(param) match
case tvar: TypeVar =>
assert(tvar.origin == param, i"mismatch $tvar, $param")
case _ =>
// ---------- Exploration --------------------------------------------------------
def domainLambdas: List[TypeLambda] = boundsMap.keys
def domainParams: List[TypeParamRef] =
for {
(poly, entries) <- boundsMap.toList
n <- 0 until paramCount(entries)
if entries(n).exists
}
yield poly.paramRefs(n)
def forallParams(p: TypeParamRef => Boolean): Boolean =
boundsMap.forallBinding { (poly, entries) =>
!0.until(paramCount(entries)).exists(i => isBounds(entries(i)) && !p(poly.paramRefs(i)))
}
def foreachParam(p: (TypeLambda, Int) => Unit): Unit =
boundsMap.foreachBinding { (poly, entries) =>
0.until(poly.paramNames.length).foreach(p(poly, _))
}
def foreachTypeVar(op: TypeVar => Unit): Unit =
boundsMap.foreachBinding { (poly, entries) =>
var i = 0
val limit = paramCount(entries)
while i < limit do
typeVar(entries, i) match
case tv: TypeVar if !tv.inst.exists => op(tv)
case _ =>
i += 1
}
private var myUninstVars: mutable.ArrayBuffer[TypeVar] | Null = _
/** The uninstantiated typevars of this constraint */
def uninstVars: collection.Seq[TypeVar] = {
if (myUninstVars == null || myUninstVars.uncheckedNN.exists(_.inst.exists)) {
myUninstVars = new mutable.ArrayBuffer[TypeVar]
boundsMap.foreachBinding { (poly, entries) =>
for (i <- 0 until paramCount(entries))
typeVar(entries, i) match {
case tv: TypeVar if !tv.inst.exists && isBounds(entries(i)) => myUninstVars.uncheckedNN += tv
case _ =>
}
}
}
myUninstVars.uncheckedNN
}
// ---------- Checking -----------------------------------------------
def checkNonCyclic()(using Context): this.type =
if Config.checkConstraintsNonCyclic then
domainParams.foreach { param =>
val inst = entry(param)
assert(!isLess(param, param),
s"cyclic ordering involving $param in ${this.show}, upper = $inst")
assert(!occursAtToplevel(param, inst),
s"cyclic bound for $param: ${inst.show} in ${this.show}")
}
this
def occursAtToplevel(param: TypeParamRef, inst: Type)(using Context): Boolean =
def occurs(tp: Type)(using Context): Boolean = tp match
case tp: AndOrType =>
occurs(tp.tp1) || occurs(tp.tp2)
case tp: TypeParamRef =>
(tp eq param) || entry(tp).match
case NoType => false
case TypeBounds(lo, hi) => (lo eq hi) && occurs(lo)
case inst => occurs(inst)
case tp: TypeVar =>
occurs(tp.underlying)
case TypeBounds(lo, hi) =>
occurs(lo) || occurs(hi)
case _ =>
val tp1 = tp.dealias
(tp1 ne tp) && occurs(tp1)
occurs(inst)
end occursAtToplevel
override def checkClosed()(using Context): Unit =
def isFreeTypeParamRef(tp: Type) = tp match
case TypeParamRef(binder: TypeLambda, _) => !contains(binder)
case _ => false
def checkClosedType(tp: Type | Null, where: String) =
if tp != null then
assert(!tp.existsPart(isFreeTypeParamRef), i"unclosed constraint: $this refers to $tp in $where")
boundsMap.foreachBinding((_, tps) => tps.foreach(checkClosedType(_, "bounds")))
lowerMap.foreachBinding((_, paramss) => paramss.foreach(_.foreach(checkClosedType(_, "lower"))))
upperMap.foreachBinding((_, paramss) => paramss.foreach(_.foreach(checkClosedType(_, "upper"))))
end checkClosed
// ---------- Printing -----------------------------------------------------
override def toText(printer: Printer): Text =
printer.toText(this)
override def toString: String = {
def entryText(tp: Type): String = tp match {
case tp: TypeBounds => tp.toString
case _ => " := " + tp
}
val constrainedText =
" constrained types = " + domainLambdas.mkString("\n")
val boundsText =
" bounds = " + {
val assocs =
for (param <- domainParams)
yield
s"${param.binder.paramNames(param.paramNum)}: ${entryText(entry(param))}"
assocs.mkString("\n")
}
constrainedText + "\n" + boundsText
}
}