Skip to content

Fix #4734: Pickle positions for quotes #4885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions compiler/src/dotty/tools/dotc/core/quoted/PickledQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.NameKinds
import dotty.tools.dotc.core.Mode
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Types.Type
import dotty.tools.dotc.core.tasty.TreePickler.Hole
import dotty.tools.dotc.core.tasty.{TastyPickler, TastyPrinter, TastyString}
import dotty.tools.dotc.core.tasty.{PositionPickler, TastyPickler, TastyPrinter, TastyString}
import dotty.tools.dotc.core.tasty.TreeUnpickler.UnpickleMode

import scala.quoted.Types._
Expand Down Expand Up @@ -66,13 +67,13 @@ object PickledQuotes {
/** Unpickle the tree contained in the TastyExpr */
private def unpickleExpr(expr: TastyExpr[_])(implicit ctx: Context): Tree = {
val tastyBytes = TastyString.unpickle(expr.tasty)
unpickle(tastyBytes, expr.args, isType = false)
unpickle(tastyBytes, expr.args, isType = false)(ctx.addMode(Mode.ReadPositions))
}

/** Unpickle the tree contained in the TastyType */
private def unpickleType(ttpe: TastyType[_])(implicit ctx: Context): Tree = {
val tastyBytes = TastyString.unpickle(ttpe.tasty)
unpickle(tastyBytes, ttpe.args, isType = true)
unpickle(tastyBytes, ttpe.args, isType = true)(ctx.addMode(Mode.ReadPositions))
}

// TASTY picklingtests/pos/quoteTest.scala
Expand All @@ -85,6 +86,8 @@ object PickledQuotes {
treePkl.compactify()
pickler.addrOfTree = treePkl.buf.addrOfTree
pickler.addrOfSym = treePkl.addrOfSym
if (tree.pos.exists)
new PositionPickler(pickler, treePkl.buf.addrOfTree).picklePositions(tree :: Nil)

if (pickling ne noPrinter)
println(i"**** pickling quote of \n${tree.show}")
Expand Down
12 changes: 12 additions & 0 deletions tests/pos/i4734/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.annotation.tailrec
import scala.quoted._

object Macros {
transparent def unrolledForeach(f: Int => Int): Int =
~unrolledForeachImpl('(f))

def unrolledForeachImpl(f: Expr[Int => Int]): Expr[Int] = '{
val size: Int = 5
(~f)(3)
}
}
8 changes: 8 additions & 0 deletions tests/pos/i4734/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._
import Macros._

object Test {
def main(args: Array[String]): Unit = {
unrolledForeach((x: Int) => 2)
}
}
21 changes: 21 additions & 0 deletions tests/run/i4734.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
0
2
4
6
8
10
12
14
16
18
20
22
24
26
28
30
32
34
36
38
40
33 changes: 33 additions & 0 deletions tests/run/i4734/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import scala.annotation.tailrec
import scala.quoted._

object Macros {
transparent def unrolledForeach(seq: IndexedSeq[Int], f: => Int => Unit, transparent unrollSize: Int): Unit = // or f: Int => Unit
~unrolledForeachImpl('(seq), '(f), unrollSize)

def unrolledForeachImpl(seq: Expr[IndexedSeq[Int]], f: Expr[Int => Unit], unrollSize: Int): Expr[Unit] = '{
val size = (~seq).length
assert(size % (~unrollSize.toExpr) == 0) // for simplicity of the implementation
var i = 0
while (i < size) {
~{
for (j <- new UnrolledRange(0, unrollSize)) '{
val index = i + ~j.toExpr
val element = (~seq)(index)
~f('(element)) // or `(~f)(element)` if `f` should not be inlined
}
}
i += ~unrollSize.toExpr
}

}

class UnrolledRange(start: Int, end: Int) {
def foreach(f: Int => Expr[Unit]): Expr[Unit] = {
@tailrec def loop(i: Int, acc: Expr[Unit]): Expr[Unit] =
if (i >= 0) loop(i - 1, '{ ~f(i); ~acc })
else acc
loop(end - 1, '())
}
}
}
9 changes: 9 additions & 0 deletions tests/run/i4734/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._
import Macros._

object Test {
def main(args: Array[String]): Unit = {
val seq = IndexedSeq.tabulate[Int](21)(x => x)
unrolledForeach(seq, (x: Int) => println(2*x), 3)
}
}