Skip to content

Commit 8f621c6

Browse files
authored
Simplify use of toFunctionType (#18429)
Make `isJava` by default `false` and remove `dropLast` argument. Part of #18305. Note that the use of `toFunctionType` will increase, and most will not be java functions.
2 parents 8c311a0 + 3419d8a commit 8f621c6

File tree

6 files changed

+22
-14
lines changed

6 files changed

+22
-14
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ class CheckCaptures extends Recheck, SymTransformer:
698698

699699
private def toDepFun(args: List[Type], resultType: Type, isContextual: Boolean)(using Context): Type =
700700
MethodType.companion(isContextual = isContextual)(args, resultType)
701-
.toFunctionType(isJava = false, alwaysDependent = true)
701+
.toFunctionType(alwaysDependent = true)
702702

703703
/** Turn `expected` into a dependent function when `actual` is dependent. */
704704
private def alignDependentFunction(expected: Type, actual: Type)(using Context): Type =
@@ -850,7 +850,7 @@ class CheckCaptures extends Recheck, SymTransformer:
850850
adaptFun(actual, rinfo.paramInfos, rinfo.resType, expected, covariant, insertBox,
851851
(aargs1, ares1) =>
852852
rinfo.derivedLambdaType(paramInfos = aargs1, resType = ares1)
853-
.toFunctionType(isJava = false, alwaysDependent = true))
853+
.toFunctionType(alwaysDependent = true))
854854
case actual: MethodType =>
855855
adaptFun(actual, actual.paramInfos, actual.resType, expected, covariant, insertBox,
856856
(aargs1, ares1) =>

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

Lines changed: 3 additions & 3 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(

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1878,22 +1878,20 @@ object Types {
18781878

18791879
/** Turn type into a function type.
18801880
* @pre this is a method type without parameter dependencies.
1881-
* @param dropLast the number of trailing parameters that should be dropped
1882-
* when forming the function type.
1881+
* @param isJava translate repeated params as as java `Array`s?
18831882
* @param alwaysDependent if true, always create a dependent function type.
18841883
*/
1885-
def toFunctionType(isJava: Boolean, dropLast: Int = 0, alwaysDependent: Boolean = false)(using Context): Type = this match {
1884+
def toFunctionType(isJava: Boolean = false, alwaysDependent: Boolean = false)(using Context): Type = this match {
18861885
case mt: MethodType =>
18871886
assert(!mt.isParamDependent)
18881887
def nonDependentFunType =
1889-
val formals1 = if (dropLast == 0) mt.paramInfos else mt.paramInfos dropRight dropLast
18901888
val isContextual = mt.isContextualMethod && !ctx.erasedTypes
18911889
val result1 = mt.nonDependentResultApprox match {
18921890
case res: MethodType => res.toFunctionType(isJava)
18931891
case res => res
18941892
}
18951893
defn.FunctionOf(
1896-
formals1 mapConserve (_.translateFromRepeated(toArray = isJava)),
1894+
mt.paramInfos.mapConserve(_.translateFromRepeated(toArray = isJava)),
18971895
result1, isContextual)
18981896
if mt.hasErasedParams then
18991897
defn.PolyFunctionOf(mt)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ abstract class Recheck extends Phase, SymTransformer:
333333

334334
def recheckClosure(tree: Closure, pt: Type)(using Context): Type =
335335
if tree.tpt.isEmpty then
336-
tree.meth.tpe.widen.toFunctionType(tree.meth.symbol.is(JavaDefined))
336+
tree.meth.tpe.widen.toFunctionType(isJava = tree.meth.symbol.is(JavaDefined))
337337
else
338338
recheck(tree.tpt)
339339

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/TypeAssigner.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,17 @@ trait TypeAssigner {
405405

406406
def assignType(tree: untpd.Closure, meth: Tree, target: Tree)(using Context): Closure =
407407
tree.withType(
408-
if (target.isEmpty) meth.tpe.widen.toFunctionType(isJava = meth.symbol.is(JavaDefined), tree.env.length)
408+
if target.isEmpty then
409+
def methTypeWithoutEnv(info: Type): Type = info match
410+
case mt: MethodType =>
411+
val dropLast = tree.env.length
412+
val paramNames = mt.paramNames.dropRight(dropLast)
413+
val paramInfos = mt.paramInfos.dropRight(dropLast)
414+
mt.derivedLambdaType(paramNames, paramInfos)
415+
case pt: PolyType =>
416+
pt.derivedLambdaType(resType = methTypeWithoutEnv(pt.resType))
417+
val methodicType = if tree.env.isEmpty then meth.tpe.widen else methTypeWithoutEnv(meth.tpe.widen)
418+
methodicType.toFunctionType(isJava = meth.symbol.is(JavaDefined))
409419
else target.tpe)
410420

411421
def assignType(tree: untpd.CaseDef, pat: Tree, body: Tree)(using Context): CaseDef = {

0 commit comments

Comments
 (0)