Skip to content

Commit abdc2ca

Browse files
committed
Add defn.PolyFunctionOf.apply
1 parent 8a6ef64 commit abdc2ca

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

compiler/src/dotty/tools/dotc/cc/Setup.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ extends tpd.TreeTraverser:
197197
val mt = ContextualMethodType(paramName :: Nil)(
198198
_ => paramType :: Nil,
199199
mt => if isLast then res else expandThrowsAlias(res, mt :: encl))
200-
val fntpe = RefinedType(defn.PolyFunctionClass.typeRef, nme.apply, mt)
200+
val fntpe = defn.PolyFunctionOf(mt)
201201
if !encl.isEmpty && isLast then
202202
val cs = CaptureSet(encl.map(_.paramRefs.head)*)
203203
CapturingType(fntpe, cs, boxed = false)

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1130,6 +1130,12 @@ class Definitions {
11301130
}
11311131

11321132
object PolyFunctionOf {
1133+
1134+
/** Creates a refined `PolyFunction` with an `apply` method with the given info. */
1135+
def apply(mt: MethodOrPoly)(using Context): Type =
1136+
assert(isValidPolyFunctionInfo(mt), s"Not a valid PolyFunction refinement: $mt")
1137+
RefinedType(PolyFunctionClass.typeRef, nme.apply, mt)
1138+
11331139
/** Matches a refined `PolyFunction` type and extracts the apply info.
11341140
*
11351141
* Pattern: `PolyFunction { def apply: $mt }`
@@ -1139,6 +1145,15 @@ class Definitions {
11391145
if parent.derivesFrom(defn.PolyFunctionClass) =>
11401146
Some(mt)
11411147
case _ => None
1148+
1149+
private def isValidPolyFunctionInfo(info: Type)(using Context): Boolean =
1150+
def isValidMethodType(info: Type) = info match
1151+
case info: MethodType =>
1152+
!info.resType.isInstanceOf[MethodOrPoly] // Has only one parameter list
1153+
case _ => false
1154+
info match
1155+
case info: PolyType => isValidMethodType(info.resType)
1156+
case _ => isValidMethodType(info)
11421157
}
11431158

11441159
object PartialFunctionOf {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1886,13 +1886,13 @@ object Types {
18861886
formals1 mapConserve (_.translateFromRepeated(toArray = isJava)),
18871887
result1, isContextual)
18881888
if mt.hasErasedParams then
1889-
RefinedType(defn.PolyFunctionType, nme.apply, mt)
1889+
defn.PolyFunctionOf(mt)
18901890
else if alwaysDependent || mt.isResultDependent then
18911891
RefinedType(nonDependentFunType, nme.apply, mt)
18921892
else nonDependentFunType
18931893
case poly @ PolyType(_, mt: MethodType) =>
18941894
assert(!mt.isParamDependent)
1895-
RefinedType(defn.PolyFunctionType, nme.apply, poly)
1895+
defn.PolyFunctionOf(poly)
18961896
}
18971897

18981898
/** The signature of this type. This is by default NotAMethod,

0 commit comments

Comments
 (0)