aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/dotty/tools/dotc/core')
-rw-r--r--src/dotty/tools/dotc/core/Definitions.scala6
-rw-r--r--src/dotty/tools/dotc/core/TypeApplications.scala80
-rw-r--r--src/dotty/tools/dotc/core/TypeComparer.scala5
-rw-r--r--src/dotty/tools/dotc/core/Types.scala2
-rw-r--r--src/dotty/tools/dotc/core/pickling/UnPickler.scala2
5 files changed, 74 insertions, 21 deletions
diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala
index 0ee0ea1e2..ef16a970d 100644
--- a/src/dotty/tools/dotc/core/Definitions.scala
+++ b/src/dotty/tools/dotc/core/Definitions.scala
@@ -286,7 +286,7 @@ class Definitions {
def unapply(ft: Type): Option[(List[Type], Type)] = { // Dotty deviation: Type annotation needed because inferred type
// is Some[(List[Type], Type)] | None, which is not a legal unapply type.
val tsym = ft.typeSymbol
- lazy val targs = ft.typeArgs
+ lazy val targs = ft.argInfos
if ((FunctionClasses contains tsym) &&
(targs.length - 1 <= MaxFunctionArity) &&
(FunctionClass(targs.length - 1) == tsym)) Some((targs.init, targs.last)) // Dotty deviation: no auto-tupling
@@ -317,7 +317,7 @@ class Definitions {
lazy val RootImports = List[Symbol](JavaLangPackageVal, ScalaPackageVal, ScalaPredefModule, DottyPredefModule)
def isTupleType(tp: Type) = {
- val arity = tp.dealias.typeArgs.length
+ val arity = tp.dealias.argInfos.length
arity <= MaxTupleArity && (tp isRef TupleClass(arity))
}
@@ -329,7 +329,7 @@ class Definitions {
0 <= arity && arity <= MaxFunctionArity && (tp isRef FunctionClass(arity))
}
- def functionArity(tp: Type) = tp.dealias.typeArgs.length - 1
+ def functionArity(tp: Type) = tp.dealias.argInfos.length - 1
// ----- Higher kinds machinery ------------------------------------------
diff --git a/src/dotty/tools/dotc/core/TypeApplications.scala b/src/dotty/tools/dotc/core/TypeApplications.scala
index e854e672f..b4c30d902 100644
--- a/src/dotty/tools/dotc/core/TypeApplications.scala
+++ b/src/dotty/tools/dotc/core/TypeApplications.scala
@@ -12,6 +12,29 @@ import Flags._
import util.Positions.Position
import collection.mutable
+object TypeApplications {
+
+ /** Assert type is not a TypeBounds instance and return it unchanged */
+ val noBounds = (tp: Type) => tp match {
+ case tp: TypeBounds => throw new AssertionError("no TypeBounds allowed")
+ case _ => tp
+ }
+
+ /** If `tp` is a TypeBounds instance return its lower bound else return `tp` */
+ val boundsToLo = (tp: Type) => tp match {
+ case tp: TypeBounds => tp.lo
+ case _ => tp
+ }
+
+ /** If `tp` is a TypeBounds instance return its upper bound else return `tp` */
+ val boundsToHi = (tp: Type) => tp match {
+ case tp: TypeBounds => tp.hi
+ case _ => tp
+ }
+}
+
+import TypeApplications._
+
/** A decorator that provides methods for modeling type application */
class TypeApplications(val self: Type) extends AnyVal {
@@ -135,24 +158,43 @@ class TypeApplications(val self: Type) extends AnyVal {
else TypeAlias(self, v)
}
- /** The type arguments of the base type instance wrt `base` of this type */
- final def baseTypeArgs(base: Symbol)(implicit ctx: Context): List[Type] =
+ /** The type arguments of this type's base type instance wrt.`base`.
+ * Existential types in arguments are returned as TypeBounds instances.
+ */
+ final def baseArgInfos(base: Symbol)(implicit ctx: Context): List[Type] =
if (self derivesFrom base)
- base.typeParams map (param => self.member(param.name).info.argType(param))
+ base.typeParams map (param => self.member(param.name).info.argInfo(param))
else
Nil
+ /** The type arguments of this type's base type instance wrt.`base`.
+ * Existential types in arguments are disallowed.
+ */
+ final def baseArgTypes(base: Symbol)(implicit ctx: Context): List[Type] = baseArgInfos(base) mapConserve noBounds
+
+ /** The type arguments of this type's base type instance wrt.`base`.
+ * Existential types in arguments are approximanted by their lower bound.
+ */
+ final def baseArgTypesLo(base: Symbol)(implicit ctx: Context): List[Type] = baseArgInfos(base) mapConserve boundsToLo
+
+ /** The type arguments of this type's base type instance wrt.`base`.
+ * Existential types in arguments are approximanted by their upper bound.
+ */
+ final def baseArgTypesHi(base: Symbol)(implicit ctx: Context): List[Type] = baseArgInfos(base) mapConserve boundsToHi
+
/** The first type argument of the base type instance wrt `base` of this type */
- final def firstBaseTypeArg(base: Symbol)(implicit ctx: Context): Type = base.typeParams match {
+ final def firstBaseArgInfo(base: Symbol)(implicit ctx: Context): Type = base.typeParams match {
case param :: _ if self derivesFrom base =>
- self.member(param.name).info.argType(param)
+ self.member(param.name).info.argInfo(param)
case _ =>
NoType
}
- /** The base type including all type arguments of this type */
+ /** The base type including all type arguments of this type.
+ * Existential types in arguments are returned as TypeBounds instances.
+ */
final def baseTypeWithArgs(base: Symbol)(implicit ctx: Context): Type =
- self.baseTypeRef(base).appliedTo(baseTypeArgs(base))
+ self.baseTypeRef(base).appliedTo(baseArgInfos(base))
/** Translate a type of the form From[T] to To[T], keep other types as they are.
* `from` and `to` must be static classes, both with one type parameter, and the same variance.
@@ -163,9 +205,10 @@ class TypeApplications(val self: Type) extends AnyVal {
else self
/** If this is an encoding of a (partially) applied type, return its arguments,
- * otherwise return Nil
+ * otherwise return Nil.
+ * Existential types in arguments are returned as TypeBounds instances.
*/
- final def typeArgs(implicit ctx: Context): List[Type] = {
+ final def argInfos(implicit ctx: Context): List[Type] = {
var tparams: List[TypeSymbol] = null
def recur(tp: Type, refineCount: Int): mutable.ListBuffer[Type] = tp.stripTypeVar match {
case tp @ RefinedType(tycon, name) =>
@@ -175,7 +218,7 @@ class TypeApplications(val self: Type) extends AnyVal {
if (tparams == null) tparams = tycon.typeParams
if (buf.size < tparams.length) {
val tparam = tparams(buf.size)
- if (name == tparam.name) buf += tp.refinedInfo.argType(tparam)
+ if (name == tparam.name) buf += tp.refinedInfo.argInfo(tparam)
else null
} else null
}
@@ -187,6 +230,15 @@ class TypeApplications(val self: Type) extends AnyVal {
if (buf == null) Nil else buf.toList
}
+ /** Argument types where existential types in arguments are disallowed */
+ def argTypes(implicit ctx: Context) = argInfos mapConserve noBounds
+
+ /** Argument types where existential types in arguments are approximated by their lower bound */
+ def argTypesLo(implicit ctx: Context) = argInfos mapConserve boundsToLo
+
+ /** Argument types where existential types in arguments are approximated by their upper bound */
+ def argTypesHi(implicit ctx: Context) = argInfos mapConserve boundsToHi
+
/** The core type without any type arguments.
* @param `typeArgs` must be the type arguments of this type.
*/
@@ -201,7 +253,7 @@ class TypeApplications(val self: Type) extends AnyVal {
/** If this is the image of a type argument to type parameter `tparam`,
* recover the type argument, otherwise NoType.
*/
- final def argType(tparam: Symbol)(implicit ctx: Context): Type = self match {
+ final def argInfo(tparam: Symbol)(implicit ctx: Context): Type = self match {
case TypeBounds(lo, hi) =>
if (lo eq hi) hi
else {
@@ -216,7 +268,7 @@ class TypeApplications(val self: Type) extends AnyVal {
/** The element type of a sequence or array */
def elemType(implicit ctx: Context): Type =
- firstBaseTypeArg(defn.SeqClass) orElse firstBaseTypeArg(defn.ArrayClass)
+ firstBaseArgInfo(defn.SeqClass) orElse firstBaseArgInfo(defn.ArrayClass)
/** If this type is of the normalized form Array[...[Array[T]...]
* return the number of Array wrappers and T.
@@ -225,7 +277,7 @@ class TypeApplications(val self: Type) extends AnyVal {
final def splitArray(implicit ctx: Context): (Int, Type) = {
def recur(n: Int, tp: Type): (Int, Type) = tp.stripTypeVar match {
case RefinedType(tycon, _) if tycon isRef defn.ArrayClass =>
- tp.typeArgs match {
+ tp.argInfos match {
case arg :: Nil => recur(n + 1, arg)
case _ => (n, tp)
}
@@ -274,7 +326,7 @@ class TypeApplications(val self: Type) extends AnyVal {
val correspondingParamName: Map[Symbol, TypeName] = {
for {
- (tparam, targ: TypeRef) <- cls.typeParams zip typeArgs
+ (tparam, targ: TypeRef) <- cls.typeParams zip argInfos
if boundSyms contains targ.symbol
} yield targ.symbol -> tparam.name
}.toMap
diff --git a/src/dotty/tools/dotc/core/TypeComparer.scala b/src/dotty/tools/dotc/core/TypeComparer.scala
index 0af6ebf97..d46a8387f 100644
--- a/src/dotty/tools/dotc/core/TypeComparer.scala
+++ b/src/dotty/tools/dotc/core/TypeComparer.scala
@@ -616,7 +616,7 @@ class TypeComparer(initctx: Context) extends DotClass {
*/
def isSubTypeHK(tp1: Type, tp2: Type): Boolean = {
val tparams = tp1.typeParams
- val hkArgs = tp2.typeArgs
+ val hkArgs = tp2.argInfos
(hkArgs.length == tparams.length) && {
val base = tp1.narrow
(tparams, hkArgs).zipped.forall { (tparam, hkArg) =>
@@ -752,7 +752,7 @@ class TypeComparer(initctx: Context) extends DotClass {
/** The least upper bound of two types
* @note We do not admit singleton types in or-types as lubs.
*/
- def lub(tp1: Type, tp2: Type): Type =
+ def lub(tp1: Type, tp2: Type): Type = /*>|>*/ ctx.traceIndented(s"lub(${tp1.show}, ${tp2.show})", typr, show = true) /*<|<*/ {
if (tp1 eq tp2) tp1
else if (!tp1.exists) tp1
else if (!tp2.exists) tp2
@@ -772,6 +772,7 @@ class TypeComparer(initctx: Context) extends DotClass {
}
}
}
+ }
/** The least upper bound of a list of types */
final def lub(tps: List[Type]): Type =
diff --git a/src/dotty/tools/dotc/core/Types.scala b/src/dotty/tools/dotc/core/Types.scala
index bc8d7dfd2..4d26a6735 100644
--- a/src/dotty/tools/dotc/core/Types.scala
+++ b/src/dotty/tools/dotc/core/Types.scala
@@ -96,7 +96,7 @@ object Types {
else thissym eq sym
case this1: RefinedType =>
// make sure all refinements are type arguments
- this1.parent.isRef(sym) && this.typeArgs.nonEmpty
+ this1.parent.isRef(sym) && this.argInfos.nonEmpty
case _ =>
false
}
diff --git a/src/dotty/tools/dotc/core/pickling/UnPickler.scala b/src/dotty/tools/dotc/core/pickling/UnPickler.scala
index c48e71052..c69f60758 100644
--- a/src/dotty/tools/dotc/core/pickling/UnPickler.scala
+++ b/src/dotty/tools/dotc/core/pickling/UnPickler.scala
@@ -76,7 +76,7 @@ object UnPickler {
case tp @ MethodType(paramNames, paramTypes) =>
val lastArg = paramTypes.last
assert(lastArg isRef defn.ArrayClass)
- val elemtp0 :: Nil = lastArg.baseTypeArgs(defn.ArrayClass)
+ val elemtp0 :: Nil = lastArg.baseArgInfos(defn.ArrayClass)
val elemtp = elemtp0 match {
case AndType(t1, t2) if t1.typeSymbol.isAbstractType && (t2 isRef defn.ObjectClass) =>
t1 // drop intersection with Object for abstract types in varargs. UnCurry can handle them.