Skip to content

Commit 934255f

Browse files
committed
Simplify and use toFunctionType
1 parent c077aab commit 934255f

File tree

6 files changed

+28
-16
lines changed

6 files changed

+28
-16
lines changed

compiler/src/dotty/tools/dotc/ast/TreeInfo.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ trait TypedTreeInfo extends TreeInfo[Type] { self: Trees.Instance[Type] =>
954954
def isStructuralTermSelectOrApply(tree: Tree)(using Context): Boolean = {
955955
def isStructuralTermSelect(tree: Select) =
956956
def hasRefinement(qualtpe: Type): Boolean = qualtpe.dealias match
957-
case defn.PolyFunctionOf(_) =>
957+
case defn.FunctionOf(_) =>
958958
false
959959
case RefinedType(parent, rname, rinfo) =>
960960
rname == tree.name || hasRefinement(parent)

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ class CheckCaptures extends Recheck, SymTransformer:
410410
else if meth == defn.Caps_unsafeBoxFunArg then
411411
mapArgUsing: tp =>
412412
val defn.FunctionOf(mt: MethodType) = tp.dealias: @unchecked
413-
defn.FunctionOf(mt.derivedLambdaType(resType = mt.resType.forceBoxStatus(true)))
413+
mt.derivedLambdaType(resType = mt.resType.forceBoxStatus(true))
414+
.toFunctionType()
414415
else
415416
super.recheckApply(tree, pt) match
416417
case appType @ CapturingType(appType1, refs) =>
@@ -706,7 +707,7 @@ class CheckCaptures extends Recheck, SymTransformer:
706707
case defn.FunctionOf(mt: MethodType) =>
707708
actual.dealias match
708709
case defn.FunctionOf(mt2: MethodType) if mt2.isResultDependent =>
709-
mt.toFunctionType(isJava = false, alwaysDependent = true)
710+
mt.toFunctionType(alwaysDependent = true)
710711
case _ =>
711712
expected
712713
case _ =>
@@ -848,7 +849,7 @@ class CheckCaptures extends Recheck, SymTransformer:
848849
adaptFun(actual, rinfo.paramInfos, rinfo.resType, expected, covariant, insertBox,
849850
(aargs1, ares1) =>
850851
rinfo.derivedLambdaType(paramInfos = aargs1, resType = ares1)
851-
.toFunctionType(isJava = false, alwaysDependent = true))
852+
.toFunctionType(alwaysDependent = true))
852853
case actual: MethodType =>
853854
adaptFun(actual, actual.paramInfos, actual.resType, expected, covariant, insertBox,
854855
(aargs1, ares1) =>

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ extends tpd.TreeTraverser:
4040
MethodType.companion(
4141
isContextual = defn.isContextFunctionClass(tycon.classSymbol),
4242
)(argTypes, resType)
43-
.toFunctionType(isJava = false, alwaysDependent = true)
43+
.toFunctionType(alwaysDependent = true)
4444

4545
/** If `tp` is an unboxed capturing type or a function returning an unboxed capturing type,
4646
* convert it to be boxed.
@@ -57,7 +57,7 @@ extends tpd.TreeTraverser:
5757
case tp1 @ RefinedType(_, _, rinfo: MethodType) if defn.isFunctionType(tp1) =>
5858
val boxedRinfo = recur(rinfo)
5959
if boxedRinfo eq rinfo then tp
60-
else boxedRinfo.toFunctionType(isJava = false, alwaysDependent = true)
60+
else boxedRinfo.toFunctionType(alwaysDependent = true)
6161
case tp1: MethodOrPoly =>
6262
val res = tp1.resType
6363
val boxedRes = recur(res)
@@ -151,7 +151,7 @@ extends tpd.TreeTraverser:
151151
tp.derivedAppliedType(tycon1, args.mapConserve(arg => this(arg)))
152152
case tp @ RefinedType(core, rname, rinfo: MethodType) if defn.isFunctionType(tp) =>
153153
val rinfo1 = apply(rinfo)
154-
if rinfo1 ne rinfo then rinfo1.toFunctionType(isJava = false, alwaysDependent = true)
154+
if rinfo1 ne rinfo then rinfo1.toFunctionType(alwaysDependent = true)
155155
else tp
156156
case tp: MethodType =>
157157
tp.derivedLambdaType(
@@ -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 = defn.PolyFunctionOf(mt)
200+
val fntpe = mt.toFunctionType()
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/Types.scala

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1872,7 +1872,7 @@ object Types {
18721872
* when forming the function type.
18731873
* @param alwaysDependent if true, always create a dependent function type.
18741874
*/
1875-
def toFunctionType(isJava: Boolean, dropLast: Int = 0, alwaysDependent: Boolean = false)(using Context): Type = this match {
1875+
def toFunctionType(isJava: Boolean = false, dropLast: Int = 0, alwaysDependent: Boolean = false)(using Context): Type = this match {
18761876
case mt: MethodType =>
18771877
assert(!mt.isParamDependent)
18781878
def nonDependentFunType =
@@ -1886,15 +1886,26 @@ object Types {
18861886
formals1 mapConserve (_.translateFromRepeated(toArray = isJava)),
18871887
result1, isContextual)
18881888
if mt.hasErasedParams then
1889-
defn.PolyFunctionOf(mt)
1889+
assert(isValidPolyFunctionInfo(mt), s"Not a valid PolyFunction refinement: $mt")
1890+
RefinedType(defn.PolyFunctionType, nme.apply, mt)
18901891
else if alwaysDependent || mt.isResultDependent then
18911892
RefinedType(nonDependentFunType, nme.apply, mt)
18921893
else nonDependentFunType
1893-
case poly @ PolyType(_, mt: MethodType) =>
1894-
assert(!mt.isParamDependent)
1895-
defn.PolyFunctionOf(poly)
1894+
case poly: PolyType =>
1895+
assert(isValidPolyFunctionInfo(poly), s"Not a valid PolyFunction refinement: $poly")
1896+
RefinedType(defn.PolyFunctionType, nme.apply, poly)
18961897
}
18971898

1899+
private def isValidPolyFunctionInfo(info: Type)(using Context): Boolean =
1900+
def isValidMethodType(info: Type) = info match
1901+
case info: MethodType =>
1902+
!info.resType.isInstanceOf[MethodOrPoly] // Has only one parameter list
1903+
&& !info.isParamDependent
1904+
case _ => false
1905+
info match
1906+
case info: PolyType => isValidMethodType(info.resType)
1907+
case _ => isValidMethodType(info)
1908+
18981909
/** The signature of this type. This is by default NotAMethod,
18991910
* but is overridden for PolyTypes, MethodTypes, and TermRef types.
19001911
* (the reason why we deviate from the "final-method-with-pattern-match-in-base-class"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ class Splicing extends MacroTransform:
197197
if tree.isTerm then
198198
if isCaptured(tree.symbol) then
199199
val tpe = tree.tpe.widenTermRefExpr match {
200-
case tpw: MethodicType => tpw.toFunctionType(isJava = false)
200+
case tpw: MethodicType => tpw.toFunctionType()
201201
case tpw => tpw
202202
}
203203
spliced(tpe)(capturedTerm(tree))
@@ -291,7 +291,7 @@ class Splicing extends MacroTransform:
291291

292292
private def capturedTerm(tree: Tree)(using Context): Tree =
293293
val tpe = tree.tpe.widenTermRefExpr match
294-
case tpw: MethodicType => tpw.toFunctionType(isJava = false)
294+
case tpw: MethodicType => tpw.toFunctionType()
295295
case tpw => tpw
296296
capturedTerm(tree, tpe)
297297

compiler/src/dotty/tools/dotc/typer/ProtoTypes.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ object ProtoTypes {
847847
if (rt eq mt.resultType) tp
848848
else mt.derivedLambdaType(resType = rt)
849849
case _ =>
850-
val ft = defn.FunctionOf(mt.derivedLambdaType(resType = rt))
850+
val ft = mt.derivedLambdaType(resType = rt).toFunctionType()
851851
if mt.paramInfos.nonEmpty || (ft frozen_<:< pt) then ft else rt
852852
}
853853
}

0 commit comments

Comments
 (0)