@@ -198,29 +198,34 @@ private[internal] trait GlbLubs {
198
198
199
199
/** From a list of types, retain only maximal types as determined by the partial order `po`. */
200
200
private def maxTypes (ts : List [Type ])(po : (Type , Type ) => Boolean ): List [Type ] = {
201
- def loop (ts : List [Type ]): List [Type ] = ts match {
202
- case t :: ts1 =>
203
- val ts2 = loop(ts1.filterNot(po(_, t)))
204
- if (ts2.exists(po(t, _))) ts2 else t :: ts2
205
- case Nil => Nil
201
+ @ tailrec
202
+ def loop (remaining : List [Type ], hs : List [Type ]): List [Type ] = remaining match {
203
+ case h :: rest =>
204
+ loop(rest.filterNot(po(_, h)), h :: hs)
205
+ case _ =>
206
+ def sieve (res : List [Type ], todo : List [Type ]): List [Type ] = todo match {
207
+ case h :: tail =>
208
+ val res1 = if (res.exists(po(h, _))) res else h :: res
209
+ sieve(res1, tail)
210
+ case _ => res
211
+ }
212
+ sieve(Nil , hs)
206
213
}
207
214
208
215
// The order here matters because type variables and
209
216
// wildcards can act both as subtypes and supertypes.
210
- val (ts2, ts1) = partitionConserve(ts) { tp =>
211
- isWildCardOrNonGroundTypeVarCollector.collect(tp).isDefined
212
- }
217
+ val (wilds, ts1) = partitionConserve(ts)(isWildCardOrNonGroundTypeVarCollector.collect(_).isDefined)
213
218
214
- loop(ts1 ::: ts2 )
219
+ loop(ts1 ::: wilds, Nil )
215
220
}
216
221
217
222
/** Eliminate from list of types all elements which are a supertype
218
- * of some other element of the list. */
223
+ * of some other element of the list. */
219
224
private def elimSuper (ts : List [Type ]): List [Type ] =
220
225
maxTypes(ts)((t1, t2) => t2 <:< t1)
221
226
222
227
/** Eliminate from list of types all elements which are a subtype
223
- * of some other element of the list. */
228
+ * of some other element of the list. */
224
229
@ tailrec private def elimSub (ts : List [Type ], depth : Depth ): List [Type ] = {
225
230
val ts1 = maxTypes(ts)(isSubType(_, _, depth.decr))
226
231
if (ts1.lengthCompare(1 ) <= 0 ) ts1 else {
0 commit comments