Skip to content

Commit fdf9ccc

Browse files
Merge pull request #6381 from dotty-staging/inline-idempotent-singletons-on-quoted-function-beta-reduction
Inline idempotent singletons on quoted function beta-reduction
2 parents 3db347f + 913c374 commit fdf9ccc

File tree

8 files changed

+40
-22
lines changed

8 files changed

+40
-22
lines changed

compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import dotty.tools.dotc.core.StdNames._
1010
import dotty.tools.dotc.core.NameKinds
1111
import dotty.tools.dotc.core.Mode
1212
import dotty.tools.dotc.core.Symbols._
13-
import dotty.tools.dotc.core.Types.Type
13+
import dotty.tools.dotc.core.Types._
1414
import dotty.tools.dotc.core.tasty.TreePickler.Hole
1515
import dotty.tools.dotc.core.tasty.{PositionPickler, TastyPickler, TastyPrinter, TastyString}
1616
import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode
@@ -128,15 +128,19 @@ object PickledQuotes {
128128
}
129129

130130
private def functionAppliedTo(fn: Tree, args: List[Tree])(implicit ctx: Context): Tree = {
131-
val argVals = args.map(arg => SyntheticValDef(NameKinds.UniqueName.fresh("x".toTermName), arg).withSpan(arg.span))
132-
def argRefs() = argVals.map(argVal => ref(argVal.symbol))
131+
val (argVals, argRefs) = args.map(arg => arg.tpe match {
132+
case tpe: SingletonType if isIdempotentExpr(arg) => (Nil, arg)
133+
case _ =>
134+
val argVal = SyntheticValDef(NameKinds.UniqueName.fresh("x".toTermName), arg).withSpan(arg.span)
135+
(argVal :: Nil, ref(argVal.symbol))
136+
}).unzip
133137
def rec(fn: Tree): Tree = fn match {
134138
case Inlined(call, bindings, expansion) =>
135139
// this case must go before closureDef to avoid dropping the inline node
136140
cpy.Inlined(fn)(call, bindings, rec(expansion))
137141
case closureDef(ddef) =>
138142
val paramSyms = ddef.vparamss.head.map(param => param.symbol)
139-
val paramToVals = paramSyms.zip(argRefs()).toMap
143+
val paramToVals = paramSyms.zip(argRefs).toMap
140144
new TreeTypeMap(
141145
oldOwners = ddef.symbol :: Nil,
142146
newOwners = ctx.owner :: Nil,
@@ -145,9 +149,9 @@ object PickledQuotes {
145149
case Block(stats, expr) =>
146150
seq(stats, rec(expr)).withSpan(fn.span)
147151
case _ =>
148-
fn.select(nme.apply).appliedToArgs(argRefs()).withSpan(fn.span)
152+
fn.select(nme.apply).appliedToArgs(argRefs).withSpan(fn.span)
149153
}
150-
Block(argVals, rec(fn))
154+
seq(argVals.flatten, rec(fn))
151155
}
152156

153157
private def classToType(clazz: Class[_])(implicit ctx: Context): Type = {

tests/run-with-compiler/i3876-b.check

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
6
22
{
3-
val x$1: scala.Int = 3
43
def f(x: scala.Int): scala.Int = x.+(x)
5-
f(x$1)
4+
f(3)
65
}

tests/run-with-compiler/i3876-c.check

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
6
22
{
3-
val x$1: scala.Int = 3
43
val f: scala.Function1[scala.Int, scala.Int] {
54
def apply(x: scala.Int): scala.Int
65
} = ((x: scala.Int) => x.+(x))
76

87
(f: scala.Function1[scala.Int, scala.Int] {
98
def apply(x: scala.Int): scala.Int
10-
}).apply(x$1)
9+
}).apply(3)
1110
}

tests/run-with-compiler/i3876-d.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
6
2-
{
3-
val x$1: scala.Int = 3
4-
x$1.+(x$1)
5-
}
2+
3.+(3)

tests/run-with-compiler/i3876-e.check

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
6
3+
{
4+
val x$1: scala.Int = {
5+
scala.Predef.println()
6+
3
7+
}
8+
x$1.+(x$1)
9+
}

tests/run-with-compiler/i3876-e.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.quoted._
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)
5+
6+
val x: Expr[Int] = '{ println(); 3 }
7+
8+
val f4: Expr[Int => Int] = '{
9+
inlineLambda
10+
}
11+
println(f4(x).run)
12+
println(f4(x).show)
13+
}
14+
15+
inline def inlineLambda <: Int => Int = x => x + x
16+
}

tests/run-with-compiler/i3876.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
11
6
2-
{
3-
val x$1: scala.Int = 3
4-
x$1.+(x$1)
5-
}
2+
3.+(3)

tests/run-with-compiler/i5144b.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
{
2-
def f(x: scala.Int): scala.Int = {
3-
val x$1: scala.Int = 42
4-
f(x$1)
5-
}
2+
def f(x: scala.Int): scala.Int = f(42)
63
()
74
}

0 commit comments

Comments
 (0)