Skip to content

Commit 10b5ea2

Browse files
Backport "Add defn.RefinedFunctionOf extractor" to LTS (#20601)
Backports #18432 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 96c4f61 + ffedd75 commit 10b5ea2

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class CheckCaptures extends Recheck, SymTransformer:
187187
capt.println(i"solving $t")
188188
refs.solve()
189189
traverse(parent)
190-
case t @ RefinedType(_, nme.apply, rinfo) if defn.isFunctionType(t) =>
190+
case t @ defn.RefinedFunctionOf(rinfo) =>
191191
traverse(rinfo)
192192
case tp: TypeVar =>
193193
case tp: TypeRef =>
@@ -769,7 +769,7 @@ class CheckCaptures extends Recheck, SymTransformer:
769769
case actual @ AppliedType(tycon, args) if defn.isNonRefinedFunction(actual) =>
770770
adaptFun(actual, args.init, args.last, expected, covariant, insertBox,
771771
(aargs1, ares1) => actual.derivedAppliedType(tycon, aargs1 :+ ares1))
772-
case actual @ RefinedType(_, _, rinfo: MethodType) if defn.isFunctionType(actual) =>
772+
case actual @ defn.RefinedFunctionOf(rinfo: MethodType) =>
773773
// TODO Find a way to combine handling of generic and dependent function types (here and elsewhere)
774774
adaptFun(actual, rinfo.paramInfos, rinfo.resType, expected, covariant, insertBox,
775775
(aargs1, ares1) =>
@@ -779,11 +779,11 @@ class CheckCaptures extends Recheck, SymTransformer:
779779
adaptFun(actual, actual.paramInfos, actual.resType, expected, covariant, insertBox,
780780
(aargs1, ares1) =>
781781
actual.derivedLambdaType(paramInfos = aargs1, resType = ares1))
782-
case actual @ RefinedType(p, nme, rinfo: PolyType) if defn.isFunctionType(actual) =>
782+
case actual @ defn.RefinedFunctionOf(rinfo: PolyType) =>
783783
adaptTypeFun(actual, rinfo.resType, expected, covariant, insertBox,
784784
ares1 =>
785785
val rinfo1 = rinfo.derivedLambdaType(rinfo.paramNames, rinfo.paramInfos, ares1)
786-
val actual1 = actual.derivedRefinedType(p, nme, rinfo1)
786+
val actual1 = actual.derivedRefinedType(actual.parent, actual.refinedName, rinfo1)
787787
actual1
788788
)
789789
case _ =>
@@ -996,7 +996,7 @@ class CheckCaptures extends Recheck, SymTransformer:
996996
case CapturingType(parent, refs) =>
997997
healCaptureSet(refs)
998998
traverse(parent)
999-
case tp @ RefinedType(parent, rname, rinfo: MethodType) if defn.isFunctionType(tp) =>
999+
case defn.RefinedFunctionOf(rinfo: MethodType) =>
10001000
traverse(rinfo)
10011001
case tp: TermLambda =>
10021002
val saved = allowed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extends tpd.TreeTraverser:
5454
val boxedRes = recur(res)
5555
if boxedRes eq res then tp
5656
else tp1.derivedAppliedType(tycon, args.init :+ boxedRes)
57-
case tp1 @ RefinedType(_, _, rinfo: MethodType) if defn.isFunctionType(tp1) =>
57+
case tp1 @ defn.RefinedFunctionOf(rinfo: MethodType) =>
5858
val boxedRinfo = recur(rinfo)
5959
if boxedRinfo eq rinfo then tp
6060
else boxedRinfo.toFunctionType(alwaysDependent = true)
@@ -231,7 +231,7 @@ extends tpd.TreeTraverser:
231231
tp.derivedAppliedType(tycon1, args1 :+ res1)
232232
else
233233
tp.derivedAppliedType(tycon1, args.mapConserve(arg => this(arg)))
234-
case tp @ RefinedType(core, rname, rinfo: MethodType) if defn.isFunctionType(tp) =>
234+
case defn.RefinedFunctionOf(rinfo: MethodType) =>
235235
val rinfo1 = apply(rinfo)
236236
if rinfo1 ne rinfo then rinfo1.toFunctionType(alwaysDependent = true)
237237
else tp

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,20 @@ class Definitions {
11201120
}
11211121
}
11221122

1123+
object RefinedFunctionOf {
1124+
/** Matches a refined `PolyFunction`/`FunctionN[...]`/`ContextFunctionN[...]`.
1125+
* Extracts the method type type and apply info.
1126+
*/
1127+
def unapply(tpe: RefinedType)(using Context): Option[MethodOrPoly] = {
1128+
tpe.refinedInfo match
1129+
case mt: MethodOrPoly
1130+
if tpe.refinedName == nme.apply
1131+
&& (tpe.parent.derivesFrom(defn.PolyFunctionClass) || isFunctionNType(tpe.parent)) =>
1132+
Some(mt)
1133+
case _ => None
1134+
}
1135+
}
1136+
11231137
object PolyOrErasedFunctionOf {
11241138
/** Matches a refined `PolyFunction` or `ErasedFunction` type and extracts the apply info.
11251139
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,8 +4055,8 @@ object Types {
40554055
tp.derivedAppliedType(tycon, addInto(args.head) :: Nil)
40564056
case tp @ AppliedType(tycon, args) if defn.isFunctionNType(tp) =>
40574057
wrapConvertible(tp.derivedAppliedType(tycon, args.init :+ addInto(args.last)))
4058-
case tp @ RefinedType(parent, rname, rinfo) if defn.isFunctionType(tp) =>
4059-
wrapConvertible(tp.derivedRefinedType(parent, rname, addInto(rinfo)))
4058+
case tp @ defn.RefinedFunctionOf(rinfo) =>
4059+
wrapConvertible(tp.derivedRefinedType(tp.parent, tp.refinedName, addInto(rinfo)))
40604060
case tp: MethodOrPoly =>
40614061
tp.derivedLambdaType(resType = addInto(tp.resType))
40624062
case ExprType(resType) =>

0 commit comments

Comments
 (0)