Skip to content

Fix #3912: Account for units and blocks in inline and macros #4012

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 3 commits into from
Feb 21, 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
25 changes: 23 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -321,16 +321,22 @@ class ReifyQuotes extends MacroTransformWithImplicits {
val last = enteredSyms
stats.foreach(markDef)
mapOverTree(last)
case Inlined(call, bindings, expansion @ Select(body, name)) if expansion.symbol.isSplice =>

case Inlined(call, bindings, InlineSplice(expansion @ Select(body, name))) =>
// To maintain phase consistency, we move the binding of the this parameter into the spliced code
val (splicedBindings, stagedBindings) = bindings.partition {
case vdef: ValDef => vdef.symbol.is(Synthetic) // Assume that only _this bindings are tagged with Synthetic
case _ => false
}

val tree1 =
if (level == 0) cpy.Inlined(tree)(call, stagedBindings, Splicer.splice(seq(splicedBindings, body)))
else seq(stagedBindings, cpy.Select(expansion)(cpy.Inlined(tree)(call, splicedBindings, body), name))
transform(tree1)
val tree2 = transform(tree1)

// due to value-discarding which converts an { e } into { e; () })
if (tree.tpe =:= defn.UnitType) Block(tree2 :: Nil, Literal(Constant(())))
else tree2
case _: Import =>
tree
case tree: DefDef if tree.symbol.is(Macro) && level == 0 =>
Expand All @@ -343,5 +349,20 @@ class ReifyQuotes extends MacroTransformWithImplicits {
checkLevel(mapOverTree(enteredSyms))
}
}

/** InlineSplice is used to detect cases where the expansion
* consists of a (possibly multiple & nested) block or a sole expression.
*/
object InlineSplice {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how performance sensitive this code is but you could return a Tree instead of Option[Select] to avoid boxing (and EmptyTree instead of None)

Copy link
Contributor Author

@biboudis biboudis Feb 21, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need that .get method on unapply's return type so isn't Option unavoidable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe Tree qualifies for both product pattern and name based pattern. And scalac supports name based pattern

def unapply(tree: Tree)(implicit ctx: Context): Option[Select] = {
tree match {
case expansion: Select if expansion.symbol.isSplice =>
Some(expansion)
case Block(List(stat), Literal(Constant(()))) => unapply(stat)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you give an example of when this happen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this is related to the implicit conversion related to value discarding (simply put when an e returns a value and it's called from a function of expected type Unit and it is converted into { e; () }). This manifested in quoted code as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For example inline foo: Unit = 4 will become inline foo: Unit = { 4; () } and then be inlined as { 4; () }. In our case we have a ~xyz instead of 4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it! I would add a comment explaining why this is needed (i.e. the compiler can insert ()).

case Block(Nil, expr) => unapply(expr)
case _ => None
}
}
}
}
}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.quoted._
import dotty.tools.dotc.interpreter._

/** Utility class to slice quoted expressions */
/** Utility class to splice quoted expressions */
object Splicer {
import tpd._

/** Splice the Tree for a Quoted expression. `~'(xyz)` becomes `xyz`
* and for `~xyz` the tree of `xyz` is interpreted for which the
* resulting expression is return as a `Tree`
* resulting expression is returned as a `Tree`
*/
def splice(tree: Tree)(implicit ctx: Context): Tree = tree match {
case Quoted(quotedTree) => quotedTree
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/i3912-1/i3912_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted._

object Macros {
inline def foo(): Int = { ~impl() }

def impl(): Expr[Int] = '(1)
}
7 changes: 7 additions & 0 deletions tests/pos/i3912-1/i3912_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted._
import Macros._

class Test {
val a: Unit = foo()

}
7 changes: 7 additions & 0 deletions tests/pos/i3912-2/i3912_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import scala.quoted._

object Macros {
inline def foo2(): Unit = ~impl()

def impl(): Expr[Int] = '(1)
}
6 changes: 6 additions & 0 deletions tests/pos/i3912-2/i3912_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.quoted._
import Macros._

class Test {
val a2: Unit = foo2()
}
11 changes: 11 additions & 0 deletions tests/pos/i3912-3/i3912_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted._

object Macros {
inline def foo3(): Int = {
{
~impl()
}
}

def impl(): Expr[Int] = '(1)
}
6 changes: 6 additions & 0 deletions tests/pos/i3912-3/i3912_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.quoted._
import Macros._

class Test {
val a3: Unit = foo3()
}