Skip to content

Commit 8324940

Browse files
committed
Simplify existsPart
1 parent e90c953 commit 8324940

File tree

3 files changed

+13
-15
lines changed

3 files changed

+13
-15
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,12 +1355,10 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
13551355
* for equality would give the wrong result, so we should not use the sets
13561356
* for comparisons.
13571357
*/
1358-
def canCompare(ts: Set[Type]) = ctx.phase.isTyper || {
1359-
val hasSkolems = new ExistsAccumulator(_.isInstanceOf[SkolemType]) {
1360-
override def stopAtStatic = true
1361-
}
1362-
!ts.exists(hasSkolems(false, _))
1363-
}
1358+
def canCompare(ts: Set[Type]) =
1359+
ctx.phase.isTyper
1360+
|| !ts.exists(_.existsPart(_.isInstanceOf[SkolemType], stopAtStatic = true))
1361+
13641362
def verified(result: Boolean): Boolean =
13651363
if Config.checkAtomsComparisons then
13661364
try

compiler/src/dotty/tools/dotc/core/Types.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -422,8 +422,8 @@ object Types {
422422

423423
/** Returns true if there is a part of this type that satisfies predicate `p`.
424424
*/
425-
final def existsPart(p: Type => Boolean, forceLazy: Boolean = true)(using Context): Boolean =
426-
new ExistsAccumulator(p, forceLazy).apply(false, this)
425+
final def existsPart(p: Type => Boolean, stopAtStatic: Boolean = false, forceLazy: Boolean = true)(using Context): Boolean =
426+
new ExistsAccumulator(p, stopAtStatic, forceLazy).apply(false, this)
427427

428428
/** Returns true if all parts of this type satisfy predicate `p`.
429429
*/
@@ -5686,11 +5686,12 @@ object Types {
56865686
protected def traverseChildren(tp: Type): Unit = foldOver((), tp)
56875687
}
56885688

5689-
class ExistsAccumulator(p: Type => Boolean, forceLazy: Boolean = true)(using Context) extends TypeAccumulator[Boolean] {
5690-
override def stopAtStatic: Boolean = false
5689+
class ExistsAccumulator(
5690+
p: Type => Boolean,
5691+
override val stopAtStatic: Boolean,
5692+
forceLazy: Boolean)(using Context) extends TypeAccumulator[Boolean]:
56915693
def apply(x: Boolean, tp: Type): Boolean =
56925694
x || p(tp) || (forceLazy || !tp.isInstanceOf[LazyRef]) && foldOver(x, tp)
5693-
}
56945695

56955696
class ForeachAccumulator(p: Type => Unit, override val stopAtStatic: Boolean)(using Context) extends TypeAccumulator[Unit] {
56965697
def apply(x: Unit, tp: Type): Unit = foldOver(p(tp), tp)

compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -669,12 +669,11 @@ class Scala2Unpickler(bytes: Array[Byte], classRoot: ClassDenotation, moduleClas
669669
}
670670
// Cannot use standard `existsPart` method because it calls `lookupRefined`
671671
// which can cause CyclicReference errors.
672-
val isBoundAccumulator = new ExistsAccumulator(isBound) {
673-
override def foldOver(x: Boolean, tp: Type): Boolean = tp match {
672+
val isBoundAccumulator = new ExistsAccumulator(isBound, stopAtStatic = true, forceLazy = true):
673+
override def foldOver(x: Boolean, tp: Type): Boolean = tp match
674674
case tp: TypeRef => applyToPrefix(x, tp)
675675
case _ => super.foldOver(x, tp)
676-
}
677-
}
676+
678677
def removeSingleton(tp: Type): Type =
679678
if (tp isRef defn.SingletonClass) defn.AnyType else tp
680679
def elim(tp: Type): Type = tp match {

0 commit comments

Comments
 (0)