Skip to content

Fix #3898: Do not interpret bindings for inlined parameters #4019

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 2 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
13 changes: 9 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,15 @@ class ReifyQuotes extends MacroTransformWithImplicits {
stats.foreach(markDef)
mapOverTree(last)
case Inlined(call, bindings, expansion @ Select(body, name)) if expansion.symbol.isSplice =>
// To maintain phase consistency, convert inlined expressions of the form
// `{ bindings; ~expansion }` to `~{ bindings; expansion }`
if (level == 0) transform(Splicer.splice(cpy.Inlined(tree)(call, bindings, body)))
else transform(cpy.Select(expansion)(cpy.Inlined(tree)(call, bindings, 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)
case _: Import =>
tree
case tree: DefDef if tree.symbol.is(Macro) && level == 0 =>
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ object Splicer {
case tree: RefTree => reflectiveSplice(tree)
case tree: Apply => reflectiveSplice(tree)
case tree: Inlined => reflectiveSplice(tree)
case tree: Block => reflectiveSplice(tree)
}

/** Splice the Tree for a Quoted expression which is constructed via a reflective call to the given method */
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class Inliner(call: tpd.Tree, rhs: tpd.Tree)(implicit ctx: Context) {
case tpe: ThisType if !canElideThis(tpe) && !thisProxy.contains(tpe.cls) =>
val proxyName = s"${tpe.cls.name}_this".toTermName
val proxyType = tpe.asSeenFrom(prefix.tpe, meth.owner)
thisProxy(tpe.cls) = newSym(proxyName, EmptyFlags, proxyType).termRef
thisProxy(tpe.cls) = newSym(proxyName, Synthetic, proxyType).termRef
if (!tpe.cls.isStaticOwner)
registerType(meth.owner.thisType) // make sure we have a base from which to outer-select
case tpe: NamedType
Expand Down
5 changes: 5 additions & 0 deletions tests/pos/i3898/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted._
object Macro {
inline def ff(args: Any*): String = ~impl('(args))
def impl(args: Expr[Seq[Any]]): Expr[String] = ""
}
6 changes: 6 additions & 0 deletions tests/pos/i3898/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
def x: Int = 5
Macro.ff(x)
}
}
5 changes: 5 additions & 0 deletions tests/pos/i3898b/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted._
object Macro {
inline def ff(x: Int, inline y: Int): String = ~impl('(x))
def impl(x: Expr[Int]): Expr[String] = ""
}
6 changes: 6 additions & 0 deletions tests/pos/i3898b/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object Test {
def main(args: Array[String]): Unit = {
def z: Int = 5
Macro.ff(z, 5)
}
}
5 changes: 5 additions & 0 deletions tests/pos/i3898c/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted._
object Macro {
inline def ff(x: Int, inline y: Int): String = ~impl('(x))
def impl(x: Expr[Int]): Expr[String] = ""
}
9 changes: 9 additions & 0 deletions tests/pos/i3898c/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
object Test {
def main(args: Array[String]): Unit = {
val a = '{
def z: Int = 5
Macro.ff(z, 5)
}

}
}
3 changes: 3 additions & 0 deletions tests/pos/quote-interpolator-core/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ object FInterpolation {
val args1: Expr[Seq[Any]] = liftSeq(args)
'{ (~str).format(~args1: _*) }
}

def hello = "hello"

}
3 changes: 3 additions & 0 deletions tests/pos/quote-interpolator-core/quoted_2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ object Test {
println(ff"integer: ${5}%d")
println(ff"string: ${"l"}%s")
println(ff"${5}%s, ${6}%d, ${"hello"}%s")
val hello = "hello"
println(ff"${5}%s, ${6}%d, ${hello}%s")
println(ff"${5}%s, ${6}%d, ${FInterpolation.hello}%s")
}