Skip to content

Make body of quotes inlinable #13188

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
Aug 4, 2021
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
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)