@@ -1451,11 +1451,11 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1451
1451
else typedFunctionValue(tree, pt)
1452
1452
1453
1453
def typedFunctionType (tree : untpd.Function , pt : Type )(using Context ): Tree = {
1454
- val untpd .Function (args, body ) = tree
1455
- body match
1456
- case untpd.CapturesAndResult (refs, result ) =>
1454
+ val untpd .Function (args, result ) = tree
1455
+ result match
1456
+ case untpd.CapturesAndResult (refs, result1 ) =>
1457
1457
return typedUnadapted(untpd.makeRetaining(
1458
- cpy.Function (tree)(args, result ), refs, tpnme.retains), pt)
1458
+ cpy.Function (tree)(args, result1 ), refs, tpnme.retains), pt)
1459
1459
case _ =>
1460
1460
var (funFlags, erasedParams) = tree match {
1461
1461
case tree : untpd.FunctionWithMods => (tree.mods.flags, tree.erasedParams)
@@ -1467,37 +1467,28 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1467
1467
val isImpure = funFlags.is(Impure )
1468
1468
1469
1469
/** Typechecks dependent function type with given parameters `params` */
1470
- def typedDependent (params : List [untpd.ValDef ])(using Context ): Tree =
1471
- val fixThis = new untpd.UntypedTreeMap :
1472
- // pretype all references of this in outer context,
1473
- // so that they do not refer to the refined type being constructed
1474
- override def transform (tree : untpd.Tree )(using Context ): untpd.Tree = tree match
1475
- case This (id) => untpd.TypedSplice (typedExpr(tree)(using ctx.outer))
1476
- case _ => super .transform(tree)
1477
-
1470
+ def typedDependent (params : List [untpd.ValDef ], result : untpd.Tree )(using Context ): Tree =
1478
1471
val params1 =
1479
1472
if funFlags.is(Given ) then params.map(_.withAddedFlags(Given ))
1480
1473
else params
1481
- val params2 = params1.map(fixThis.transformSub)
1482
- val params3 = params2.zipWithConserve(erasedParams) { (arg, isErased) =>
1474
+ val params2 = params1.zipWithConserve(erasedParams): (arg, isErased) =>
1483
1475
if isErased then arg.withAddedFlags(Erased ) else arg
1484
- }
1485
- val appDef0 = untpd.DefDef (nme.apply, List (params3), body, EmptyTree ).withSpan(tree.span)
1476
+ val appDef0 = untpd.DefDef (nme.apply, List (params2), result, EmptyTree ).withSpan(tree.span)
1486
1477
index(appDef0 :: Nil )
1487
1478
val appDef = typed(appDef0).asInstanceOf [DefDef ]
1488
1479
val mt = appDef.symbol.info.asInstanceOf [MethodType ]
1489
1480
if (mt.isParamDependent)
1490
1481
report.error(em " $mt is an illegal function type because it has inter-parameter dependencies " , tree.srcPos)
1491
1482
// Restart typechecking if there are erased classes that we want to mark erased
1492
1483
if mt.erasedParams.zip(mt.paramInfos.map(_.isErasedClass)).exists((paramErased, classErased) => classErased && ! paramErased) then
1493
- val newParams = params3 .zipWithConserve(mt.paramInfos.map(_.isErasedClass)) { (arg, isErasedClass) =>
1484
+ val newParams = params2 .zipWithConserve(mt.paramInfos.map(_.isErasedClass)) { (arg, isErasedClass) =>
1494
1485
if isErasedClass then arg.withAddedFlags(Erased ) else arg
1495
1486
}
1496
- return typedDependent(newParams)
1487
+ return typedDependent(newParams, result )
1497
1488
val core =
1498
1489
if mt.hasErasedParams then TypeTree (defn.ErasedFunctionClass .typeRef)
1499
1490
else
1500
- val resTpt = TypeTree (mt.nonDependentResultApprox).withSpan(body .span)
1491
+ val resTpt = TypeTree (mt.nonDependentResultApprox).withSpan(result .span)
1501
1492
val paramTpts = appDef.termParamss.head.map(p => TypeTree (p.tpt.tpe).withSpan(p.tpt.span))
1502
1493
val funSym = defn.FunctionSymbol (numArgs, isContextual, isImpure)
1503
1494
val tycon = TypeTree (funSym.typeRef)
@@ -1507,19 +1498,28 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
1507
1498
1508
1499
args match {
1509
1500
case ValDef (_, _, _) :: _ =>
1510
- typedDependent(args.asInstanceOf [List [untpd.ValDef ]])(
1501
+ val fixThis = new untpd.UntypedTreeMap :
1502
+ // pretype all references of this so that they do not refer to the
1503
+ // refined type being constructed
1504
+ override def transform (tree : untpd.Tree )(using Context ): untpd.Tree = tree match
1505
+ case This (id) => untpd.TypedSplice (typedExpr(tree))
1506
+ case _ => super .transform(tree)
1507
+
1508
+ val untpd .Function (fixedArgs : List [untpd.ValDef ] @ unchecked, fixedResult) =
1509
+ fixThis.transform(tree): @ unchecked
1510
+ typedDependent(fixedArgs, fixedResult)(
1511
1511
using ctx.fresh.setOwner(newRefinedClassSymbol(tree.span)).setNewScope)
1512
1512
case _ =>
1513
1513
if erasedParams.contains(true ) then
1514
1514
typedFunctionType(desugar.makeFunctionWithValDefs(tree, pt), pt)
1515
1515
else
1516
1516
val funSym = defn.FunctionSymbol (numArgs, isContextual, isImpure)
1517
- val result = typed(cpy.AppliedTypeTree (tree)(untpd.TypeTree (funSym.typeRef), args :+ body ), pt)
1517
+ val funTpt = typed(cpy.AppliedTypeTree (tree)(untpd.TypeTree (funSym.typeRef), args :+ result ), pt)
1518
1518
// if there are any erased classes, we need to re-do the typecheck.
1519
- result match
1519
+ funTpt match
1520
1520
case r : AppliedTypeTree if r.args.exists(_.tpe.isErasedClass) =>
1521
1521
typedFunctionType(desugar.makeFunctionWithValDefs(tree, pt), pt)
1522
- case _ => result
1522
+ case _ => funTpt
1523
1523
}
1524
1524
}
1525
1525
0 commit comments