Skip to content

Commit 81bea5a

Browse files
Backport "Simplify use of toFunctionType" to LTS (#20600)
Backports #18429 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 567e709 + 96c4f61 commit 81bea5a

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
@@ -630,7 +630,7 @@ class CheckCaptures extends Recheck, SymTransformer:
630630

631631
private def toDepFun(args: List[Type], resultType: Type, isContextual: Boolean)(using Context): Type =
632632
MethodType.companion(isContextual = isContextual)(args, resultType)
633-
.toFunctionType(isJava = false, alwaysDependent = true)
633+
.toFunctionType(alwaysDependent = true)
634634

635635
/** Turn `expected` into a dependent function when `actual` is dependent. */
636636
private def alignDependentFunction(expected: Type, actual: Type)(using Context): Type =
@@ -774,7 +774,7 @@ class CheckCaptures extends Recheck, SymTransformer:
774774
adaptFun(actual, rinfo.paramInfos, rinfo.resType, expected, covariant, insertBox,
775775
(aargs1, ares1) =>
776776
rinfo.derivedLambdaType(paramInfos = aargs1, resType = ares1)
777-
.toFunctionType(isJava = false, alwaysDependent = true))
777+
.toFunctionType(alwaysDependent = true))
778778
case actual: MethodType =>
779779
adaptFun(actual, actual.paramInfos, actual.resType, expected, covariant, insertBox,
780780
(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)
@@ -233,7 +233,7 @@ extends tpd.TreeTraverser:
233233
tp.derivedAppliedType(tycon1, args.mapConserve(arg => this(arg)))
234234
case tp @ RefinedType(core, rname, rinfo: MethodType) if defn.isFunctionType(tp) =>
235235
val rinfo1 = apply(rinfo)
236-
if rinfo1 ne rinfo then rinfo1.toFunctionType(isJava = false, alwaysDependent = true)
236+
if rinfo1 ne rinfo then rinfo1.toFunctionType(alwaysDependent = true)
237237
else tp
238238
case tp: MethodType =>
239239
tp.derivedLambdaType(

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,20 +1858,18 @@ object Types {
18581858

18591859
/** Turn type into a function type.
18601860
* @pre this is a method type without parameter dependencies.
1861-
* @param dropLast the number of trailing parameters that should be dropped
1862-
* when forming the function type.
1861+
* @param isJava translate repeated params as as java `Array`s?
18631862
* @param alwaysDependent if true, always create a dependent function type.
18641863
*/
1865-
def toFunctionType(isJava: Boolean, dropLast: Int = 0, alwaysDependent: Boolean = false)(using Context): Type = this match {
1864+
def toFunctionType(isJava: Boolean = false, dropLast: Int = 0, alwaysDependent: Boolean = false)(using Context): Type = this match {
18661865
case mt: MethodType if !mt.isParamDependent && !mt.hasErasedParams =>
1867-
val formals1 = if (dropLast == 0) mt.paramInfos else mt.paramInfos dropRight dropLast
18681866
val isContextual = mt.isContextualMethod && !ctx.erasedTypes
18691867
val result1 = mt.nonDependentResultApprox match {
18701868
case res: MethodType => res.toFunctionType(isJava)
18711869
case res => res
18721870
}
18731871
val funType = defn.FunctionOf(
1874-
formals1 mapConserve (_.translateFromRepeated(toArray = isJava)),
1872+
mt.paramInfos.mapConserve(_.translateFromRepeated(toArray = isJava)),
18751873
result1, isContextual)
18761874
if alwaysDependent || mt.isResultDependent then
18771875
RefinedType(funType, nme.apply, mt)

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

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

325325
def recheckClosure(tree: Closure, pt: Type)(using Context): Type =
326326
if tree.tpt.isEmpty then
327-
tree.meth.tpe.widen.toFunctionType(tree.meth.symbol.is(JavaDefined))
327+
tree.meth.tpe.widen.toFunctionType(isJava = tree.meth.symbol.is(JavaDefined))
328328
else
329329
recheck(tree.tpt)
330330

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
@@ -401,7 +401,17 @@ trait TypeAssigner {
401401

402402
def assignType(tree: untpd.Closure, meth: Tree, target: Tree)(using Context): Closure =
403403
tree.withType(
404-
if (target.isEmpty) meth.tpe.widen.toFunctionType(isJava = meth.symbol.is(JavaDefined), tree.env.length)
404+
if target.isEmpty then
405+
def methTypeWithoutEnv(info: Type): Type = info match
406+
case mt: MethodType =>
407+
val dropLast = tree.env.length
408+
val paramNames = mt.paramNames.dropRight(dropLast)
409+
val paramInfos = mt.paramInfos.dropRight(dropLast)
410+
mt.derivedLambdaType(paramNames, paramInfos)
411+
case pt: PolyType =>
412+
pt.derivedLambdaType(resType = methTypeWithoutEnv(pt.resType))
413+
val methodicType = if tree.env.isEmpty then meth.tpe.widen else methTypeWithoutEnv(meth.tpe.widen)
414+
methodicType.toFunctionType(isJava = meth.symbol.is(JavaDefined))
405415
else target.tpe)
406416

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

0 commit comments

Comments
 (0)