Skip to content

Commit

Permalink
Properly handle by-name function types in repl
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Oct 25, 2023
1 parent 38559d7 commit e8a85e6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
15 changes: 11 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/ElimByName.scala
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,19 @@ class ElimByName extends MiniPhase, InfoTransformer:

override def transformApply(tree: Apply)(using Context): Tree =
trace(s"transforming ${tree.show} at phase ${ctx.phase}", show = true) {

def stripTyped(t: Tree): Tree = t match
case Typed(expr, _) => stripTyped(expr)
case _ => t
def transformArg(arg: Tree, formal: Type): Tree = formal match
case defn.ByNameFunction(formalResult) =>
def stripTyped(t: Tree): Tree = t match
case Typed(expr, _) => stripTyped(expr)
case _ => t
stripTyped(arg) match
case Apply(Select(qual, nme.apply), Nil)
if isByNameRef(qual) && (isPureExpr(qual) || qual.symbol.isAllOf(InlineParam)) =>
qual
case _ =>
if isByNameRef(arg) || arg.symbol.name.is(SuperArgName) then arg
else byNameClosure(arg, formalResult)
case ExprType(formalResult) =>
stripTyped(arg) match
case Apply(Select(qual, nme.apply), Nil)
if isByNameRef(qual) && (isPureExpr(qual) || qual.symbol.isAllOf(InlineParam)) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ class SpecializeFunctions extends MiniPhase {
override def transformApply(tree: Apply)(using Context) =
tree match {
case Apply(fun: NameTree, args) if fun.name == nme.apply && args.size <= 3 && fun.symbol.maybeOwner.isType =>
val argTypes = fun.tpe.widen.firstParamTypes.map(_.widenSingleton.dealias)
val argTypes = fun.tpe.widen.firstParamTypes.map(_.widenSingleton.dealias).map {
case ExprType(resType) => defn.FunctionOf(Nil, resType, isContextual = true)
case arg => arg
}
val retType = tree.tpe.widenSingleton.dealias
val isSpecializable =
defn.isSpecializableFunction(
Expand Down
5 changes: 5 additions & 0 deletions compiler/test-resources/repl/i18756
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
scala> def f: ( => Int) => Int = i => i ; f(1)
def f: (=> Int) => Int
val res0: Int = 1
scala> f(1)
val res1: Int = 1

0 comments on commit e8a85e6

Please sign in to comment.