From c67217594bb40e1eab7e567c97bdf29ac0654864 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sat, 18 Mar 2017 11:48:48 +0100 Subject: Eliminate LambdaAbstract Use fromParams instead. --- .../dotty/tools/dotc/core/TypeApplications.scala | 21 +--------------- compiler/src/dotty/tools/dotc/core/Types.scala | 28 ++++++++++++++++++---- .../dotc/core/unpickleScala2/Scala2Unpickler.scala | 6 +++-- compiler/src/dotty/tools/dotc/typer/Checking.scala | 2 +- compiler/src/dotty/tools/dotc/typer/Inliner.scala | 3 ++- compiler/src/dotty/tools/dotc/typer/Namer.scala | 6 ++--- .../src/dotty/tools/dotc/typer/TypeAssigner.scala | 2 +- 7 files changed, 34 insertions(+), 34 deletions(-) (limited to 'compiler/src') diff --git a/compiler/src/dotty/tools/dotc/core/TypeApplications.scala b/compiler/src/dotty/tools/dotc/core/TypeApplications.scala index 82b184dbc..eb3394082 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeApplications.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeApplications.scala @@ -272,25 +272,6 @@ class TypeApplications(val self: Type) extends AnyVal { self } - /** Lambda abstract `self` with given type parameters. Examples: - * - * type T[X] = U becomes type T = [X] -> U - * type T[X] >: L <: U becomes type T >: L <: ([X] -> U) - */ - def LambdaAbstract(tparams: List[TypeParamInfo])(implicit ctx: Context): Type = { - def expand(tp: Type) = PolyType/*HKTypeLambda*/.fromParams(tparams, tp) - if (tparams.isEmpty) self - else self match { - case self: TypeAlias => - self.derivedTypeAlias(expand(self.alias)) - case self @ TypeBounds(lo, hi) => - self.derivedTypeBounds( - if (lo.isRef(defn.NothingClass)) lo else expand(lo), - expand(hi)) - case _ => expand(self) - } - } - /** Convert a type constructor `TC` which has type parameters `T1, ..., Tn` * in a context where type parameters `U1,...,Un` are expected to * @@ -303,7 +284,7 @@ class TypeApplications(val self: Type) extends AnyVal { */ def EtaExpand(tparams: List[TypeSymbol])(implicit ctx: Context): Type = { val tparamsToUse = if (variancesConform(typeParams, tparams)) tparams else typeParamSymbols - self.appliedTo(tparams map (_.typeRef)).LambdaAbstract(tparamsToUse) + HKTypeLambda.fromParams(tparamsToUse, self.appliedTo(tparams map (_.typeRef))) //.ensuring(res => res.EtaReduce =:= self, s"res = $res, core = ${res.EtaReduce}, self = $self, hc = ${res.hashCode}") } diff --git a/compiler/src/dotty/tools/dotc/core/Types.scala b/compiler/src/dotty/tools/dotc/core/Types.scala index a7401f6f9..4ba0a2924 100644 --- a/compiler/src/dotty/tools/dotc/core/Types.scala +++ b/compiler/src/dotty/tools/dotc/core/Types.scala @@ -2566,12 +2566,11 @@ object Types { protected def paramName(param: ParamInfo.Of[N])(implicit ctx: Context): N = param.paramName - protected def paramInfo(param: ParamInfo.Of[N])(implicit ctx: Context): Type = - param.paramInfo - def fromParams[PI <: ParamInfo.Of[N]](params: List[PI], resultType: Type)(implicit ctx: Context): LT = - apply(params.map(paramName))( - tl => params.map(param => tl.integrate(params, paramInfo(param)).asInstanceOf[PInfo]), + def fromParams[PI <: ParamInfo.Of[N]](params: List[PI], resultType: Type)(implicit ctx: Context): Type = + if (params.isEmpty) resultType + else apply(params.map(paramName))( + tl => params.map(param => tl.integrate(params, param.paramInfo).asInstanceOf[PInfo]), tl => tl.integrate(params, resultType)) } @@ -2755,6 +2754,25 @@ object Types { override def paramName(param: ParamInfo.Of[TypeName])(implicit ctx: Context): TypeName = param.paramName.withVariance(param.paramVariance) + + /** Distributes Lambda inside type bounds. Examples: + * + * type T[X] = U becomes type T = [X] -> U + * type T[X] <: U becomes type T >: Nothign <: ([X] -> U) + * type T[X] >: L <: U becomes type T >: ([X] -> L) <: ([X] -> U) + */ + override def fromParams[PI <: ParamInfo.Of[TypeName]](params: List[PI], resultType: Type)(implicit ctx: Context): Type = { + def expand(tp: Type) = PolyType.fromParams(params, tp) //### + resultType match { + case rt: TypeAlias => + rt.derivedTypeAlias(expand(rt.alias)) + case rt @ TypeBounds(lo, hi) => + rt.derivedTypeBounds( + if (lo.isRef(defn.NothingClass)) lo else expand(lo), expand(hi)) + case rt => + expand(rt) + } + } } object PolyType extends TypeLambdaCompanion[PolyType] { diff --git a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala index 34cbf4677..cf99bb022 100644 --- a/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala +++ b/compiler/src/dotty/tools/dotc/core/unpickleScala2/Scala2Unpickler.scala @@ -42,13 +42,15 @@ object Scala2Unpickler { /** Convert temp poly type to poly type and leave other types alone. */ def translateTempPoly(tp: Type)(implicit ctx: Context): Type = tp match { - case TempPolyType(tparams, restpe) => restpe.LambdaAbstract(tparams) // PolyType.fromParams(tparams, restpe) + case TempPolyType(tparams, restpe) => + (if (tparams.head.owner.isTerm) PolyType else HKTypeLambda) + .fromParams(tparams, restpe) case tp => tp } def addConstructorTypeParams(denot: SymDenotation)(implicit ctx: Context) = { assert(denot.isConstructor) - denot.info = denot.info.LambdaAbstract(denot.owner.typeParams) + denot.info = PolyType.fromParams(denot.owner.typeParams, denot.info) } /** Convert array parameters denoting a repeated parameter of a Java method diff --git a/compiler/src/dotty/tools/dotc/typer/Checking.scala b/compiler/src/dotty/tools/dotc/typer/Checking.scala index 00d36651e..5d1c44efc 100644 --- a/compiler/src/dotty/tools/dotc/typer/Checking.scala +++ b/compiler/src/dotty/tools/dotc/typer/Checking.scala @@ -76,7 +76,7 @@ object Checking { val orderedArgs = if (hasNamedArg(args)) tparams.map(argNamed) else args val bounds = tparams.map(_.paramInfoAsSeenFrom(tycon.tpe).bounds) def instantiate(bound: Type, args: List[Type]) = - bound.LambdaAbstract(tparams).appliedTo(args) + HKTypeLambda.fromParams(tparams, bound).appliedTo(args) checkBounds(orderedArgs, bounds, instantiate) def checkWildcardHKApply(tp: Type, pos: Position): Unit = tp match { diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index 90052a1ed..f6d65fbb9 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -120,7 +120,8 @@ object Inliner { // Abstract accessed type over local refs def abstractQualType(mtpe: Type): Type = if (localRefs.isEmpty) mtpe - else PolyType.fromParams(localRefs.map(_.symbol.asType), mtpe).flatten + else PolyType.fromParams(localRefs.map(_.symbol.asType), mtpe) + .asInstanceOf[PolyType].flatten val accessorType = abstractQualType(addQualType(dealiasMap(accessedType))) val accessor = accessorSymbol(tree, accessorType).asTerm diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index bdf044aa8..ce2030c01 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -115,7 +115,7 @@ trait NamerContextOps { this: Context => if (param.info.isDirectRef(defn.ObjectClass)) param.info = defn.AnyType make.fromSymbols(params.asInstanceOf[List[TermSymbol]], resultType) } - if (typeParams.nonEmpty) monotpe.LambdaAbstract(typeParams.asInstanceOf[List[TypeSymbol]]) + if (typeParams.nonEmpty) PolyType.fromParams(typeParams.asInstanceOf[List[TypeSymbol]], monotpe) else if (valueParamss.isEmpty) ExprType(monotpe) else monotpe } @@ -1151,9 +1151,7 @@ class Namer { typer: Typer => } def typeDefSig(tdef: TypeDef, sym: Symbol, tparamSyms: List[TypeSymbol])(implicit ctx: Context): Type = { - def abstracted(tp: Type): Type = - if (tparamSyms.nonEmpty) tp.LambdaAbstract(tparamSyms) else tp - + def abstracted(tp: Type): Type = HKTypeLambda.fromParams(tparamSyms, tp) val dummyInfo = abstracted(TypeBounds.empty) sym.info = dummyInfo // Temporarily set info of defined type T to ` >: Nothing <: Any. diff --git a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala index 4416428f4..fb1728578 100644 --- a/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala +++ b/compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala @@ -460,7 +460,7 @@ trait TypeAssigner { } def assignType(tree: untpd.LambdaTypeTree, tparamDefs: List[TypeDef], body: Tree)(implicit ctx: Context) = - tree.withType(body.tpe.LambdaAbstract(tparamDefs.map(_.symbol.asType))) + tree.withType(HKTypeLambda.fromParams(tparamDefs.map(_.symbol.asType), body.tpe)) def assignType(tree: untpd.ByNameTypeTree, result: Tree)(implicit ctx: Context) = tree.withType(ExprType(result.tpe)) -- cgit v1.2.3