Skip to content

Commit

Permalink
Optimise refineUsingParent to use constrained
Browse files Browse the repository at this point in the history
Using `constrained` on a TypeLambda means adding one TypeLambda for all
the type parameters in tp1, while newTypeVar creates a TypeLambda for
each type parameter.
  • Loading branch information
dwijnand committed Oct 16, 2024
1 parent 8d612de commit 28b8c55
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,9 @@ object TypeOps:
}

val inferThisMap = new InferPrefixMap
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds, DepParamName.fresh(tparam.paramName)) }
val tvars = tp1.etaExpand match
case eta: TypeLambda => constrained(eta)
case _ => Nil
val protoTp1 = inferThisMap.apply(tp1).appliedTo(tvars)

if gadtSyms.nonEmpty then
Expand Down
10 changes: 7 additions & 3 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4395,9 +4395,11 @@ object Types extends TypeUtils {

/** Distributes Lambda inside type bounds. Examples:
*
* type T[X] = U becomes type T = [X] -> U
* type T[X] <: U becomes type T >: Nothing <: ([X] -> U)
* type T[X] >: L <: U becomes type T >: ([X] -> L) <: ([X] -> U)
* {{{
* type T[X] = U becomes type T = [X] =>> U
* type T[X] <: U becomes type T >: Nothing <: ([X] =>> U)
* type T[X] >: L <: U becomes type T >: ([X] =>> L) <: ([X] =>> U)
* }}}
*
* The variances of regular TypeBounds types, as well as of match aliases
* and of opaque aliases are always determined from the given parameters
Expand All @@ -4409,13 +4411,15 @@ object Types extends TypeUtils {
*
* Examples:
*
* {{{
* type T[X] >: A // X is invariant
* type T[X] <: List[X] // X is invariant
* type T[X] = List[X] // X is covariant (determined structurally)
* opaque type T[X] = List[X] // X is invariant
* opaque type T[+X] = List[X] // X is covariant
* type T[A, B] = A => B // A is contravariant, B is covariant (determined structurally)
* type T[A, +B] = A => B // A is invariant, B is covariant
* }}}
*/
def boundsFromParams[PI <: ParamInfo.Of[TypeName]](params: List[PI], bounds: TypeBounds)(using Context): TypeBounds = {
def expand(tp: Type, useVariances: Boolean) =
Expand Down

0 comments on commit 28b8c55

Please sign in to comment.