Skip to content

Commit 5a7ac03

Browse files
committed
Refactor & and | on TypeBounds
Generate Type aliases only when original type(s) were aliases. Also, TypeBounds.real never generates an alias.
1 parent a5d2dc3 commit 5a7ac03

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

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

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2416,25 +2416,15 @@ object Types {
24162416
case _ => lo <:< tp && tp <:< hi
24172417
}
24182418

2419-
def & (that: TypeBounds)(implicit ctx: Context): TypeBounds = {
2420-
val v = this commonVariance that
2421-
if (v != 0) {
2422-
val thisAlias = this.asInstanceOf[TypeAlias]
2423-
if (v > 0) thisAlias.derivedTypeAlias(this.hi & that.hi, v)
2424-
else thisAlias.derivedTypeAlias(this.lo | that.lo, v)
2425-
}
2426-
else derivedTypeBounds(this.lo | that.lo, this.hi & that.hi)
2427-
}
2419+
def & (that: TypeBounds)(implicit ctx: Context): TypeBounds =
2420+
if (this.lo <:< that.lo && that.hi <:< this.hi) that
2421+
else if (that.lo <:< this.lo && this.hi <:< that.hi) this
2422+
else TypeBounds.real(this.lo | that.lo, this.hi & that.hi)
24282423

2429-
def | (that: TypeBounds)(implicit ctx: Context): TypeBounds = {
2430-
val v = this commonVariance that
2431-
if (v != 0) {
2432-
val thisAlias = this.asInstanceOf[TypeAlias]
2433-
if (v > 0) thisAlias.derivedTypeAlias(this.hi | that.hi, v)
2434-
else thisAlias.derivedTypeAlias(this.lo & that.lo, v)
2435-
}
2436-
else derivedTypeBounds(this.lo & that.lo, this.hi | that.hi)
2437-
}
2424+
def | (that: TypeBounds)(implicit ctx: Context): TypeBounds =
2425+
if (this.lo <:< that.lo && that.hi <:< this.hi) this
2426+
else if (that.lo <:< this.lo && this.hi <:< that.hi) that
2427+
else TypeBounds.real(this.lo & that.lo, this.hi | that.hi)
24382428

24392429
override def & (that: Type)(implicit ctx: Context) = that match {
24402430
case that: TypeBounds => this & that
@@ -2469,6 +2459,20 @@ object Types {
24692459
def derivedTypeAlias(tp: Type, variance: Int = this.variance)(implicit ctx: Context) =
24702460
if (lo eq tp) this
24712461
else TypeAlias(tp, variance)
2462+
2463+
override def & (that: TypeBounds)(implicit ctx: Context): TypeBounds = {
2464+
val v = this commonVariance that
2465+
if (v > 0) derivedTypeAlias(this.hi & that.hi, v)
2466+
else if (v < 0) derivedTypeAlias(this.lo | that.lo, v)
2467+
else super.& (that)
2468+
}
2469+
2470+
override def | (that: TypeBounds)(implicit ctx: Context): TypeBounds = {
2471+
val v = this commonVariance that
2472+
if (v > 0) derivedTypeAlias(this.hi | that.hi, v)
2473+
else if (v < 0) derivedTypeAlias(this.lo & that.lo, v)
2474+
else super.| (that)
2475+
}
24722476
}
24732477

24742478
class CachedTypeAlias(alias: Type, variance: Int, hc: Int) extends TypeAlias(alias, variance) {
@@ -2478,8 +2482,7 @@ object Types {
24782482

24792483
object TypeBounds {
24802484
def real(lo: Type, hi: Type)(implicit ctx: Context): TypeBounds =
2481-
if (lo eq hi) TypeAlias(lo, 0)
2482-
else unique(new RealTypeBounds(lo, hi))
2485+
unique(new RealTypeBounds(lo, hi))
24832486
def orAlias(lo: Type, hi: Type)(implicit ctx: Context): TypeBounds =
24842487
if (lo eq hi) TypeAlias(lo, 0)
24852488
else unique(new RealTypeBounds(lo, hi))

0 commit comments

Comments
 (0)