Skip to content

Commit bc92ce8

Browse files
author
Aleksander Boruch-Gruszecki
committed
Apply substParam trick to SmartGADTMap TypeMaps
Makes the code cleaner and does not allocate for trivial cases.
1 parent b749b0f commit bc92ce8

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

compiler/src/dotty/tools/dotc/core/Contexts.scala

+27-25
Original file line numberDiff line numberDiff line change
@@ -792,8 +792,8 @@ object Contexts {
792792
}
793793

794794
def cautiousSubtype(tp1: Type, tp2: Type, isSubtype: Boolean): Boolean = {
795-
val externalizedTp1 = (new TypeVarRemovingMap()(ctx))(tp1)
796-
val externalizedTp2 = (new TypeVarRemovingMap()(ctx))(tp2)
795+
val externalizedTp1 = removeTypeVars(tp1)
796+
val externalizedTp2 = removeTypeVars(tp2)
797797

798798
def descr = {
799799
def op = s"frozen_${if (isSubtype) "<:<" else ">:>"}"
@@ -824,7 +824,7 @@ object Contexts {
824824
return true
825825
}
826826

827-
val internalizedBound = (new TypeVarInsertingMap()(ctx))(bound)
827+
val internalizedBound = insertTypeVars(bound)
828828
val res = stripInst(internalizedBound) match {
829829
case boundTvar: TypeVar =>
830830
if (boundTvar eq symTvar) true
@@ -850,7 +850,7 @@ object Contexts {
850850
case tv =>
851851
def retrieveBounds: TypeBounds = {
852852
val tb = constraint.fullBounds(tv.origin)
853-
(new TypeVarRemovingMap()(ctx))(tb).asInstanceOf[TypeBounds]
853+
removeTypeVars(tb).asInstanceOf[TypeBounds]
854854
}
855855
val res =
856856
if (checkInProgress || ctx.mode.is(Mode.GADTflexible)) retrieveBounds
@@ -883,31 +883,33 @@ object Contexts {
883883
dirtyFlag
884884
)
885885

886+
private def insertTypeVars(tp: Type, map: TypeMap = null)(implicit ctx: Context) = tp match {
887+
case tp: TypeRef =>
888+
val sym = tp.typeSymbol
889+
if (contains(sym)) tvar(sym) else tp
890+
case _ =>
891+
(if (map != null) map else new TypeVarInsertingMap()).mapOver(tp)
892+
}
886893
private final class TypeVarInsertingMap(implicit ctx: Context) extends TypeMap {
887-
override def apply(tp: Type): Type = tp match {
888-
case tp: TypeRef =>
889-
val sym = tp.typeSymbol
890-
if (contains(sym)) tvar(sym) else tp
891-
case _ =>
892-
mapOver(tp)
893-
}
894+
override def apply(tp: Type): Type = insertTypeVars(tp, this)
894895
}
895896

897+
private def removeTypeVars(tp: Type, map: TypeMap = null)(implicit ctx: Context) = tp match {
898+
case tpr: TypeParamRef =>
899+
reverseMapping(tpr) match {
900+
case null => tpr
901+
case sym => sym.typeRef
902+
}
903+
case tv: TypeVar =>
904+
reverseMapping(tv.origin) match {
905+
case null => tv
906+
case sym => sym.typeRef
907+
}
908+
case _ =>
909+
(if (map != null) map else new TypeVarRemovingMap()).mapOver(tp)
910+
}
896911
private final class TypeVarRemovingMap(implicit ctx: Context) extends TypeMap {
897-
override def apply(tp: Type): Type = tp match {
898-
case tpr: TypeParamRef =>
899-
reverseMapping(tpr) match {
900-
case null => tpr
901-
case sym => sym.typeRef
902-
}
903-
case tv: TypeVar =>
904-
reverseMapping(tv.origin) match {
905-
case null => tv
906-
case sym => sym.typeRef
907-
}
908-
case _ =>
909-
mapOver(tp)
910-
}
912+
override def apply(tp: Type): Type = removeTypeVars(tp, this)
911913
}
912914
}
913915

0 commit comments

Comments
 (0)