diff options
author | Martin Odersky <odersky@gmail.com> | 2014-12-10 17:57:55 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2014-12-10 18:54:47 +0100 |
commit | 5a7ac039cef356b24100cc4e9b2cc384bdc61fbc (patch) | |
tree | c17667b7500b553ea24815e590839c2d980e3aa7 /src/dotty/tools/dotc | |
parent | a5d2dc330b7d148eec809343d6906b438f11e746 (diff) | |
download | dotty-5a7ac039cef356b24100cc4e9b2cc384bdc61fbc.tar.gz dotty-5a7ac039cef356b24100cc4e9b2cc384bdc61fbc.tar.bz2 dotty-5a7ac039cef356b24100cc4e9b2cc384bdc61fbc.zip |
Refactor & and | on TypeBounds
Generate Type aliases only when original type(s) were
aliases.
Also, TypeBounds.real never generates an alias.
Diffstat (limited to 'src/dotty/tools/dotc')
-rw-r--r-- | src/dotty/tools/dotc/core/Types.scala | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index 17b0d6f38..49119d283 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -2416,25 +2416,15 @@ object Types { case _ => lo <:< tp && tp <:< hi } - def & (that: TypeBounds)(implicit ctx: Context): TypeBounds = { - val v = this commonVariance that - if (v != 0) { - val thisAlias = this.asInstanceOf[TypeAlias] - if (v > 0) thisAlias.derivedTypeAlias(this.hi & that.hi, v) - else thisAlias.derivedTypeAlias(this.lo | that.lo, v) - } - else derivedTypeBounds(this.lo | that.lo, this.hi & that.hi) - } + def & (that: TypeBounds)(implicit ctx: Context): TypeBounds = + if (this.lo <:< that.lo && that.hi <:< this.hi) that + else if (that.lo <:< this.lo && this.hi <:< that.hi) this + else TypeBounds.real(this.lo | that.lo, this.hi & that.hi) - def | (that: TypeBounds)(implicit ctx: Context): TypeBounds = { - val v = this commonVariance that - if (v != 0) { - val thisAlias = this.asInstanceOf[TypeAlias] - if (v > 0) thisAlias.derivedTypeAlias(this.hi | that.hi, v) - else thisAlias.derivedTypeAlias(this.lo & that.lo, v) - } - else derivedTypeBounds(this.lo & that.lo, this.hi | that.hi) - } + def | (that: TypeBounds)(implicit ctx: Context): TypeBounds = + if (this.lo <:< that.lo && that.hi <:< this.hi) this + else if (that.lo <:< this.lo && this.hi <:< that.hi) that + else TypeBounds.real(this.lo & that.lo, this.hi | that.hi) override def & (that: Type)(implicit ctx: Context) = that match { case that: TypeBounds => this & that @@ -2469,6 +2459,20 @@ object Types { def derivedTypeAlias(tp: Type, variance: Int = this.variance)(implicit ctx: Context) = if (lo eq tp) this else TypeAlias(tp, variance) + + override def & (that: TypeBounds)(implicit ctx: Context): TypeBounds = { + val v = this commonVariance that + if (v > 0) derivedTypeAlias(this.hi & that.hi, v) + else if (v < 0) derivedTypeAlias(this.lo | that.lo, v) + else super.& (that) + } + + override def | (that: TypeBounds)(implicit ctx: Context): TypeBounds = { + val v = this commonVariance that + if (v > 0) derivedTypeAlias(this.hi | that.hi, v) + else if (v < 0) derivedTypeAlias(this.lo & that.lo, v) + else super.| (that) + } } class CachedTypeAlias(alias: Type, variance: Int, hc: Int) extends TypeAlias(alias, variance) { @@ -2478,8 +2482,7 @@ object Types { object TypeBounds { def real(lo: Type, hi: Type)(implicit ctx: Context): TypeBounds = - if (lo eq hi) TypeAlias(lo, 0) - else unique(new RealTypeBounds(lo, hi)) + unique(new RealTypeBounds(lo, hi)) def orAlias(lo: Type, hi: Type)(implicit ctx: Context): TypeBounds = if (lo eq hi) TypeAlias(lo, 0) else unique(new RealTypeBounds(lo, hi)) |