@@ -50,7 +50,7 @@ trait TypeOps extends TastyKernel { self: TastyUniverse =>
50
50
mkPolyType(hi.typeParams, TypeBounds .bounded(nuLo, hi.resultType.upperBound))
51
51
}
52
52
else bounds match {
53
- case TypeBounds (lo : LambdaEncoding , hi : LambdaEncoding ) => TypeBounds .bounded(lo.toNested,hi.toNested)
53
+ case TypeBounds (lo : LambdaPolyType , hi : LambdaPolyType ) => TypeBounds .bounded(lo.toNested,hi.toNested)
54
54
case _ => bounds
55
55
}
56
56
}
@@ -73,11 +73,11 @@ trait TypeOps extends TastyKernel { self: TastyUniverse =>
73
73
mkAppliedType(tycon, args)
74
74
}
75
75
76
- if (args.exists(tpe => tpe.isInstanceOf [TypeBounds ] | tpe.isInstanceOf [LambdaEncoding ])) {
76
+ if (args.exists(tpe => tpe.isInstanceOf [TypeBounds ] | tpe.isInstanceOf [LambdaPolyType ])) {
77
77
val syms = mutable.ListBuffer .empty[Symbol ]
78
78
def bindWildcards (tpe : Type ) = tpe match {
79
79
case tpe : TypeBounds => ctx.newWildcardSym(tpe).tap(syms += _).pipe(_.ref)
80
- case tpe : LambdaEncoding => tpe.toNested
80
+ case tpe : LambdaPolyType => tpe.toNested
81
81
case tpe => tpe
82
82
}
83
83
val args1 = args.map(bindWildcards)
@@ -221,114 +221,90 @@ trait TypeOps extends TastyKernel { self: TastyUniverse =>
221
221
tpeOrErr.fold(reportThenErrorTpe, identity)
222
222
}
223
223
224
- abstract class LambdaTypeCompanion [N <: Name , PInfo <: Type , LT <: LambdaType ] {
225
- def apply (paramNames : List [N ], paramVariances : List [Variance ])(paramInfosExp : LT => List [PInfo ], resultTypeExp : LT => Type )(implicit ctx : Context ): LT
224
+ def lambdaResultType (resType : Type ): Type = resType match {
225
+ case res : LambdaPolyType => res.toNested
226
+ case res => res
226
227
}
227
228
228
- final class LambdaEncoding (override val typeParams : List [Symbol ], val resTpe : Type ) extends PolyType (typeParams, TypeBounds .addLower(resTpe)) {
229
- def toNested : PolyType = resTpe match {
229
+ abstract class LambdaTypeCompanion [N <: Name , PInfo <: Type , LT <: LambdaType , Res <: Type ] {
230
+ def apply (paramNames : List [TastyName ])(nameMap : TastyName => N , paramInfosExp : LT => List [PInfo ], resultTypeExp : LT => Type )(implicit ctx : Context ): Res
231
+ }
232
+
233
+ final class LambdaPolyType (typeParams : List [Symbol ], resType : Type ) extends PolyType (typeParams, LambdaPolyType .addLower(resType)) {
234
+ def toNested : PolyType = resType match {
230
235
case _ : TypeBounds => this
231
- case _ => mkPolyType(typeParams, resTpe )
236
+ case _ => mkPolyType(typeParams, resType )
232
237
}
233
238
}
234
239
235
- def mkLambdaEncoding (typeParams : List [Symbol ], resTpe : Type ) = new LambdaEncoding (typeParams, resTpe)
236
-
237
- object TypeParamLambda {
238
- def apply (typeParams : List [Symbol ], ret : Type ): LambdaType = new TypeParamLambda (typeParams, ret)
240
+ object LambdaPolyType {
241
+ private def addLower (tpe : Type ): TypeBounds = tpe match {
242
+ case tpe : TypeBounds => tpe
243
+ case tpe => TypeBounds .upper(tpe)
244
+ }
239
245
}
240
246
241
- final class TypeParamLambda (override val typeParams : List [Symbol ], val resType : Type ) extends LambdaType {
242
- type ThisName = TypeName
243
- type PInfo = TypeBounds
247
+ def mkLambdaPolyType (typeParams : List [Symbol ], resTpe : Type ): LambdaPolyType = new LambdaPolyType (typeParams, resTpe)
248
+ def mkLambdaFromParams (typeParams : List [Symbol ], ret : Type ): PolyType = mkPolyType(typeParams, lambdaResultType(ret))
244
249
245
- val paramVariances : List [Variance ] = typeParams.map(_.variance)
246
- val paramNames : List [TypeName ] = typeParams.map(_.name.toTypeName)
247
- val paramInfos : List [TypeBounds ] = typeParams.map(_.tpe.bounds)
250
+ type LambdaType = Type with Lambda
248
251
249
- validateThisLambda()
250
-
251
- override val productPrefix = " TypeParamLambda"
252
- override def canEqual (that : Any ): Boolean = that.isInstanceOf [TypeParamLambda ]
253
- }
254
-
255
- abstract class LambdaType extends Type with Product { lambdaTpe =>
252
+ trait Lambda extends Product with Serializable {
253
+ self : Type =>
256
254
type ThisName <: Name
257
255
type PInfo <: Type
258
256
259
257
val paramNames : List [ThisName ]
260
- val paramVariances : List [Variance ]
261
258
val paramInfos : List [PInfo ]
262
259
val resType : Type
263
260
264
- override final def safeToString : String = {
265
- val args = paramNames.zip(paramInfos).map {
266
- case (name, info) => s " ${name}$info"
267
- }.mkString(" [" , " , " , " ]" )
268
- s " $args =>> $resType"
269
- }
270
-
271
261
def typeParams : List [Symbol ] // deferred to final implementation
272
262
273
263
final protected def validateThisLambda (): Unit = {
274
- assert(resType.isComplete, this )
275
- assert(paramNames.nonEmpty, this )
276
- assert(paramInfos.length == paramNames.length, this )
264
+ assert(resType.isComplete, self )
265
+ assert(paramNames.nonEmpty, self )
266
+ assert(paramInfos.length == paramNames.length, self )
277
267
}
278
268
279
- /** Best effort to transform this to an equivalent canonical representation in scalac.
280
- */
281
- final def canonicalForm (implicit ctx : Context ): Type = {
282
- val res = resType match {
283
- case res : LambdaEncoding => res.toNested
284
- case res => res
285
- }
286
- lambdaTpe match {
287
- case _ : HKTypeLambda => mkLambdaEncoding(typeParams, res)
288
- case _ : TypeParamLambda => mkPolyType(typeParams, res)
289
- }
290
- }.tap(res => ctx.log(s """ |canonical form of ${lambdaTpe.productPrefix}:
291
- | TASTy = $lambdaTpe
292
- | scala.reflect = $res""" .stripMargin))
269
+ override final def productArity : Int = 2
293
270
294
- final def productArity : Int = 2
295
- final def productElement (n : Int ): Any = n match {
271
+ override final def productElement (n : Int ): Any = n match {
296
272
case 0 => paramNames
297
273
case 1 => resType
298
274
case _ => throw new IndexOutOfBoundsException (n.toString)
299
275
}
300
- def canEqual (that : Any ): Boolean = that.isInstanceOf [LambdaType ]
276
+
277
+ def canEqual (that : Any ): Boolean = that.isInstanceOf [Lambda ]
278
+
301
279
override final def equals (that : Any ): Boolean = that match {
302
- case lambdaType : LambdaType =>
303
- (lambdaType .canEqual(this )
304
- && lambdaType .paramNames == paramNames
305
- && lambdaType .resType == resType)
280
+ case that : Lambda =>
281
+ (that .canEqual(self )
282
+ && that .paramNames == paramNames
283
+ && that .resType == resType)
306
284
case _ => false
307
285
}
308
286
}
309
287
310
- object HKTypeLambda extends LambdaTypeCompanion [TypeName , TypeBounds , HKTypeLambda ] {
311
- def apply (paramNames : List [TypeName ], paramVariances : List [ Variance ])(
312
- paramInfosExp : HKTypeLambda => List [TypeBounds ], resultTypeExp : HKTypeLambda => Type )(implicit ctx : Context ): HKTypeLambda =
313
- new HKTypeLambda (paramNames, paramVariances)( paramInfosExp, resultTypeExp)
288
+ object HKTypeLambda extends LambdaTypeCompanion [TypeName , TypeBounds , HKTypeLambda , LambdaPolyType ] {
289
+ def apply (paramNames : List [TastyName ])( nameMap : TastyName => TypeName ,
290
+ paramInfosExp : HKTypeLambda => List [TypeBounds ], resultTypeExp : HKTypeLambda => Type )(implicit ctx : Context ): LambdaPolyType =
291
+ new HKTypeLambda (paramNames)(nameMap, paramInfosExp, resultTypeExp).toPolyType
314
292
}
315
293
316
- final class HKTypeLambda (val paramNames : List [TypeName ], val paramVariances : List [ Variance ])(
294
+ final class HKTypeLambda (params : List [TastyName ])( nameMap : TastyName => TypeName ,
317
295
paramInfosExp : HKTypeLambda => List [TypeBounds ], resultTypeExp : HKTypeLambda => Type )(implicit ctx : Context )
318
- extends LambdaType {
296
+ extends Type with Lambda { hkLambda =>
319
297
type ThisName = TypeName
320
298
type PInfo = TypeBounds
321
299
322
300
private [this ] var myTypeParams : List [Symbol ] = _
323
301
324
302
override val productPrefix = " HKTypeLambda"
325
- val paramInfos : List [TypeBounds ] = paramInfosExp(this )
326
- val resType : Type = resultTypeExp(this )
327
-
328
- validateThisLambda()
303
+ val paramNames : List [TypeName ] = params.map(nameMap)
304
+ val paramInfos : List [TypeBounds ] = paramInfosExp(hkLambda)
329
305
330
- override def typeParams : List [Symbol ] = {
331
- if (myTypeParams `eq` null ) myTypeParams = paramNames.lazyZip(paramInfos).lazyZip(paramVariances ).map {
306
+ override val typeParams : List [Symbol ] = {
307
+ paramNames.lazyZip(paramInfos).lazyZip(params.map(_.variance) ).map {
332
308
case (name, bounds, variance) =>
333
309
val flags0 = Param | Deferred
334
310
val flags = variance match {
@@ -339,9 +315,14 @@ trait TypeOps extends TastyKernel { self: TastyUniverse =>
339
315
val argInfo = normaliseBounds(bounds)
340
316
ctx.owner.newTypeParameter(name, noPosition, flags).setInfo(argInfo)
341
317
}
342
- myTypeParams
343
318
}
344
319
320
+ val resType : Type = lambdaResultType(resultTypeExp(hkLambda))
321
+
322
+ validateThisLambda()
323
+
324
+ final def toPolyType : LambdaPolyType = mkLambdaPolyType(typeParams, resType)
325
+
345
326
override def canEqual (that : Any ): Boolean = that.isInstanceOf [HKTypeLambda ]
346
327
}
347
328
0 commit comments