Skip to content

Commit

Permalink
Make body of quotes inlinable
Browse files Browse the repository at this point in the history
This adds inline accessors for private/protected members if needed as we do with inline defs.

Fixes scala#8208
Fixes scala#12948
  • Loading branch information
nicolasstucki committed Jul 28, 2021
1 parent c53cc91 commit 0d23351
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 2 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,12 @@ trait QuotesAndSplices {
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedTypeModule_of.termRef), tree.quoted :: Nil), pt)(using quoteContext).select(nme.apply).appliedTo(qctx)
else
typedApply(untpd.Apply(untpd.ref(defn.QuotedRuntime_exprQuote.termRef), tree.quoted), pt)(using pushQuotes(qctx)).select(nme.apply).appliedTo(qctx)
tree1.withSpan(tree.span)
makeInlineable(tree1.withSpan(tree.span))
}

private def makeInlineable(tree: Tree)(using Context): Tree =
ctx.compilationUnit.inlineAccessors.makeInlineable(tree)

/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {
record("typedSplice")
Expand Down
2 changes: 1 addition & 1 deletion tests/neg-macros/i7068-c.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ def species(using quoted.Quotes) = '{
case object Bar // error
case class FooT() // error
${
case object Baz // ok
case object Baz // error
???
}
FooT()
Expand Down
9 changes: 9 additions & 0 deletions tests/pos-macros/i12948/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package mylib
import scala.quoted.*

object Main:
protected def foo: Unit = {}
inline def fooCaller: Unit = foo
inline def fooCallerM: Unit = ${ fooMacro }
def fooMacro(using Quotes): Expr[Unit] =
'{ foo }
5 changes: 5 additions & 0 deletions tests/pos-macros/i12948/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import mylib.Main

object Test:
Main.fooCaller
Main.fooCallerM
22 changes: 22 additions & 0 deletions tests/pos-macros/i8208/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package playground

import scala.quoted._

object X {

inline def power(n: Int, x: Double): Double =
${ powerImpl('n, 'x) }

private def powerImpl(nExpr: Expr[Int], xExpr: Expr[Double])(using Quotes): Expr[Double] =
nExpr match {
case Expr(n1) => '{ 42.0 }
case _ => '{ dynamicPower($nExpr, $xExpr) }
}

private def dynamicPower(n: Int, x: Double): Double = {
println(s"dynamic: $n^$x")
if (n == 0) 1.0
else if (n % 2 == 0) dynamicPower(n / 2, x * x)
else x * dynamicPower(n - 1, x)
}
}
2 changes: 2 additions & 0 deletions tests/pos-macros/i8208/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import playground.X
def test(x: Int) = X.power(x, 2)

0 comments on commit 0d23351

Please sign in to comment.