diff options
author | Martin Odersky <odersky@gmail.com> | 2016-05-24 15:57:44 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2016-07-11 13:34:58 +0200 |
commit | 5866d0d16c79ca5c62507bdcb7d87669426e86d6 (patch) | |
tree | 8bdce25a51cb7c36656e77abce59c6f9132807ca /src/dotty/tools/dotc/core/Types.scala | |
parent | 6ca5b9d4675c20002ca65dab3cacf32caade3933 (diff) | |
download | dotty-5866d0d16c79ca5c62507bdcb7d87669426e86d6.tar.gz dotty-5866d0d16c79ca5c62507bdcb7d87669426e86d6.tar.bz2 dotty-5866d0d16c79ca5c62507bdcb7d87669426e86d6.zip |
Allow refinements of new types
Previously a refinement could only apply to a type bound in
the parent. This restriction needs to be dropped for the new
encoding of hk type parameters.
Diffstat (limited to 'src/dotty/tools/dotc/core/Types.scala')
-rw-r--r-- | src/dotty/tools/dotc/core/Types.scala | 52 |
1 files changed, 38 insertions, 14 deletions
diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala index f514a329e..632ab823a 100644 --- a/src/dotty/tools/dotc/core/Types.scala +++ b/src/dotty/tools/dotc/core/Types.scala @@ -2849,8 +2849,12 @@ object Types { unique(new CachedClassInfo(prefix, cls, classParents, decls, selfInfo)) } - /** Type bounds >: lo <: hi */ - abstract case class TypeBounds(lo: Type, hi: Type) extends CachedProxyType with TypeType { + /** Type bounds >: lo <: hi + * @param bindingKind: If != NoBinding, it indicates that this is + * an introduction of a higher-kinded type parameter. + * In that case it also defines the variance of the parameter. + */ + abstract case class TypeBounds(lo: Type, hi: Type)(val bindingKind: BindingKind) extends CachedProxyType with TypeType { assert(lo.isInstanceOf[TermType]) assert(hi.isInstanceOf[TermType]) @@ -2860,9 +2864,9 @@ object Types { override def underlying(implicit ctx: Context): Type = hi /** The non-alias type bounds type with given bounds */ - def derivedTypeBounds(lo: Type, hi: Type)(implicit ctx: Context) = - if ((lo eq this.lo) && (hi eq this.hi) && (variance == 0)) this - else TypeBounds(lo, hi) + def derivedTypeBounds(lo: Type, hi: Type, bk: BindingKind = this.bindingKind)(implicit ctx: Context) = + if ((lo eq this.lo) && (hi eq this.hi) && (bk == this.bindingKind) && (variance == 0)) this + else TypeBounds(lo, hi, bk) /** If this is an alias, a derived alias with the new variance, * Otherwise the type itself. @@ -2884,12 +2888,12 @@ object Types { def & (that: TypeBounds)(implicit ctx: Context): TypeBounds = if ((this.lo frozen_<:< that.lo) && (that.hi frozen_<:< this.hi)) that else if ((that.lo frozen_<:< this.lo) && (this.hi frozen_<:< that.hi)) this - else TypeBounds(this.lo | that.lo, this.hi & that.hi) + else TypeBounds(this.lo | that.lo, this.hi & that.hi, this.bindingKind join that.bindingKind) def | (that: TypeBounds)(implicit ctx: Context): TypeBounds = if ((this.lo frozen_<:< that.lo) && (that.hi frozen_<:< this.hi)) this else if ((that.lo frozen_<:< this.lo) && (this.hi frozen_<:< that.hi)) that - else TypeBounds(this.lo & that.lo, this.hi | that.hi) + else TypeBounds(this.lo & that.lo, this.hi | that.hi, this.bindingKind join that.bindingKind) override def & (that: Type)(implicit ctx: Context) = that match { case that: TypeBounds => this & that @@ -2909,6 +2913,7 @@ object Types { /** If this type and that type have the same variance, this variance, otherwise 0 */ final def commonVariance(that: TypeBounds): Int = (this.variance + that.variance) / 2 + override def computeHash = doHash(variance, lo, hi) override def equals(that: Any): Boolean = that match { case that: TypeBounds => (this.lo eq that.lo) && (this.hi eq that.hi) && this.variance == that.variance @@ -2920,11 +2925,9 @@ object Types { if (lo eq hi) s"TypeAlias($lo, $variance)" else s"TypeBounds($lo, $hi)" } - class RealTypeBounds(lo: Type, hi: Type) extends TypeBounds(lo, hi) { - override def computeHash = doHash(variance, lo, hi) - } + class RealTypeBounds(lo: Type, hi: Type, bk: BindingKind) extends TypeBounds(lo, hi)(bk) - abstract class TypeAlias(val alias: Type, override val variance: Int) extends TypeBounds(alias, alias) { + abstract class TypeAlias(val alias: Type, override val variance: Int) extends TypeBounds(alias, alias)(NoBinding) { /** pre: this is a type alias */ def derivedTypeAlias(alias: Type, variance: Int = this.variance)(implicit ctx: Context) = if ((alias eq this.alias) && (variance == this.variance)) this @@ -2952,12 +2955,11 @@ object Types { class CachedTypeAlias(alias: Type, variance: Int, hc: Int) extends TypeAlias(alias, variance) { myHash = hc - override def computeHash = doHash(variance, lo, hi) } object TypeBounds { - def apply(lo: Type, hi: Type)(implicit ctx: Context): TypeBounds = - unique(new RealTypeBounds(lo, hi)) + def apply(lo: Type, hi: Type, bk: BindingKind = NoBinding)(implicit ctx: Context): TypeBounds = + unique(new RealTypeBounds(lo, hi, bk)) def empty(implicit ctx: Context) = apply(defn.NothingType, defn.AnyType) def upper(hi: Type)(implicit ctx: Context) = apply(defn.NothingType, hi) def lower(lo: Type)(implicit ctx: Context) = apply(lo, defn.AnyType) @@ -2969,6 +2971,28 @@ object Types { def unapply(tp: TypeAlias): Option[Type] = Some(tp.alias) } + /** A value class defining the interpretation of a TypeBounds + * as either a regular type bounds or a binding (i.e. introduction) of a + * higher-kinded type parameter. + */ + class BindingKind(val n: Byte) extends AnyVal { + def join(that: BindingKind) = + if (this == that) this + else if (this == NoBinding) that + else if (that == NoBinding) this + else NonvariantBinding + } + + val NoBinding = new BindingKind(0) // Regular type bounds + val ContravariantBinding = new BindingKind(1) // Bounds for contravariant hk type param + val NonvariantBinding = new BindingKind(2) // Bounds for nonvariant hk type param + val CovariantBinding = new BindingKind(3) // Bounds for covariant hk type param + + object BindingKind { + def fromVariance(v: Int): BindingKind = new BindingKind((v + NonvariantBinding.n).toByte) + def toVariance(bk: BindingKind): Int = bk.n + } + // ----- Annotated and Import types ----------------------------------------------- /** An annotated type tpe @ annot */ |