Skip to content

Commit e8a85e6

Browse files
committed
Properly handle by-name function types in repl
Fixes scala#18756
1 parent 38559d7 commit e8a85e6

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,19 @@ class ElimByName extends MiniPhase, InfoTransformer:
131131

132132
override def transformApply(tree: Apply)(using Context): Tree =
133133
trace(s"transforming ${tree.show} at phase ${ctx.phase}", show = true) {
134-
134+
def stripTyped(t: Tree): Tree = t match
135+
case Typed(expr, _) => stripTyped(expr)
136+
case _ => t
135137
def transformArg(arg: Tree, formal: Type): Tree = formal match
136138
case defn.ByNameFunction(formalResult) =>
137-
def stripTyped(t: Tree): Tree = t match
138-
case Typed(expr, _) => stripTyped(expr)
139-
case _ => t
139+
stripTyped(arg) match
140+
case Apply(Select(qual, nme.apply), Nil)
141+
if isByNameRef(qual) && (isPureExpr(qual) || qual.symbol.isAllOf(InlineParam)) =>
142+
qual
143+
case _ =>
144+
if isByNameRef(arg) || arg.symbol.name.is(SuperArgName) then arg
145+
else byNameClosure(arg, formalResult)
146+
case ExprType(formalResult) =>
140147
stripTyped(arg) match
141148
case Apply(Select(qual, nme.apply), Nil)
142149
if isByNameRef(qual) && (isPureExpr(qual) || qual.symbol.isAllOf(InlineParam)) =>

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ class SpecializeFunctions extends MiniPhase {
7171
override def transformApply(tree: Apply)(using Context) =
7272
tree match {
7373
case Apply(fun: NameTree, args) if fun.name == nme.apply && args.size <= 3 && fun.symbol.maybeOwner.isType =>
74-
val argTypes = fun.tpe.widen.firstParamTypes.map(_.widenSingleton.dealias)
74+
val argTypes = fun.tpe.widen.firstParamTypes.map(_.widenSingleton.dealias).map {
75+
case ExprType(resType) => defn.FunctionOf(Nil, resType, isContextual = true)
76+
case arg => arg
77+
}
7578
val retType = tree.tpe.widenSingleton.dealias
7679
val isSpecializable =
7780
defn.isSpecializableFunction(

compiler/test-resources/repl/i18756

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
scala> def f: ( => Int) => Int = i => i ; f(1)
2+
def f: (=> Int) => Int
3+
val res0: Int = 1
4+
scala> f(1)
5+
val res1: Int = 1

0 commit comments

Comments
 (0)