Skip to content

Commit a420d71

Browse files
committed
Generalize/simplify refined function type Erasure
The erasure now supports polymorphic function types with erased parameters. We still need to add support for this in the typer.
1 parent 63fa75a commit a420d71

File tree

2 files changed

+17
-23
lines changed

2 files changed

+17
-23
lines changed

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -560,21 +560,19 @@ object TypeErasure {
560560
case _ => false
561561
}
562562

563-
/** The erasure of `PolyFunction { def apply: $applyInfo }` */
564-
def erasePolyFunctionApply(applyInfo: Type)(using Context): Type =
565-
assert(applyInfo.isInstanceOf[PolyType])
566-
val res = applyInfo.resultType
567-
val paramss = res.paramNamess
568-
assert(paramss.length == 1)
569-
erasure(defn.FunctionType(paramss.head.length,
570-
isContextual = res.isImplicitMethod))
571-
572-
def eraseErasedFunctionApply(erasedFn: MethodType)(using Context): Type =
573-
val fnType = defn.FunctionType(
574-
n = erasedFn.erasedParams.count(_ == false),
575-
isContextual = erasedFn.isContextualMethod,
576-
)
577-
erasure(fnType)
563+
/** The erasure of `(PolyFunction | ErasedFunction) { def apply: $applyInfo }` */
564+
def eraseRefinedFunctionApply(applyInfo: Type)(using Context): Type =
565+
def functionType(info: Type): Type = info match {
566+
case info: PolyType =>
567+
functionType(info.resultType)
568+
case info: MethodType =>
569+
assert(!info.resultType.isInstanceOf[MethodicType])
570+
defn.FunctionType(
571+
n = info.erasedParams.count(_ == false),
572+
isContextual = info.isImplicitMethod,
573+
)
574+
}
575+
erasure(functionType(applyInfo))
578576
}
579577

580578
import TypeErasure._
@@ -659,10 +657,8 @@ class TypeErasure(sourceLanguage: SourceLanguage, semiEraseVCs: Boolean, isConst
659657
else SuperType(eThis, eSuper)
660658
case ExprType(rt) =>
661659
defn.FunctionType(0)
662-
case RefinedType(parent, nme.apply, refinedInfo) if defn.isPolyFunctionType(parent) =>
663-
erasePolyFunctionApply(refinedInfo)
664-
case RefinedType(parent, nme.apply, refinedInfo: MethodType) if defn.isErasedFunctionType(parent) =>
665-
eraseErasedFunctionApply(refinedInfo)
660+
case RefinedType(parent, nme.apply, refinedInfo) if defn.isRefinedFunctionType(parent) =>
661+
eraseRefinedFunctionApply(refinedInfo)
666662
case tp: TypeVar if !tp.isInstantiated =>
667663
assert(inSigName, i"Cannot erase uninstantiated type variable $tp")
668664
WildcardType

compiler/src/dotty/tools/dotc/transform/Erasure.scala

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,8 @@ object Erasure {
679679
// Instead, we manually lookup the type of `apply` in the qualifier.
680680
inContext(preErasureCtx) {
681681
val qualTp = tree.qualifier.typeOpt.widen
682-
if qualTp.derivesFrom(defn.PolyFunctionClass) then
683-
erasePolyFunctionApply(qualTp.select(nme.apply).widen).classSymbol
684-
else if defn.isErasedFunctionType(qualTp) then
685-
eraseErasedFunctionApply(qualTp.select(nme.apply).widen.asInstanceOf[MethodType]).classSymbol
682+
if defn.isRefinedFunctionType(qualTp) then
683+
eraseRefinedFunctionApply(qualTp.select(nme.apply).widen).classSymbol
686684
else
687685
NoSymbol
688686
}

0 commit comments

Comments
 (0)