Skip to content

Commit 9fd494a

Browse files
committed
Make body of quotes inlinable
This adds inline accessors for private/protected members if needed as we do with inline defs. Fixes #8208 Fixes #12948
1 parent d0c0eff commit 9fd494a

File tree

7 files changed

+51
-2
lines changed

7 files changed

+51
-2
lines changed

compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala

+4-1
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,12 @@ trait QuotesAndSplices {
6666
typedTypeApply(untpd.TypeApply(untpd.ref(defn.QuotedTypeModule_of.termRef), tree.quoted :: Nil), pt)(using quoteContext).select(nme.apply).appliedTo(qctx)
6767
else
6868
typedApply(untpd.Apply(untpd.ref(defn.QuotedRuntime_exprQuote.termRef), tree.quoted), pt)(using pushQuotes(qctx)).select(nme.apply).appliedTo(qctx)
69-
tree1.withSpan(tree.span)
69+
makeInlineable(tree1.withSpan(tree.span))
7070
}
7171

72+
private def makeInlineable(tree: Tree)(using Context): Tree =
73+
ctx.compilationUnit.inlineAccessors.makeInlineable(tree)
74+
7275
/** Translate `${ t: Expr[T] }` into expression `t.splice` while tracking the quotation level in the context */
7376
def typedSplice(tree: untpd.Splice, pt: Type)(using Context): Tree = {
7477
record("typedSplice")

tests/neg-macros/i7068-c.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ def species(using quoted.Quotes) = '{
22
case object Bar // error
33
case class FooT() // error
44
${
5-
case object Baz // ok
5+
case object Baz // error
66
???
77
}
88
FooT()
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package mylib
2+
import scala.quoted.*
3+
4+
object Main:
5+
protected def foo: Unit = {}
6+
inline def fooCaller: Unit = foo
7+
inline def fooCallerM: Unit = ${ fooMacro }
8+
def fooMacro(using Quotes): Expr[Unit] =
9+
'{ foo }

tests/pos-macros/i12948/Test_2.scala

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import mylib.Main
2+
3+
object Test:
4+
Main.fooCaller
5+
Main.fooCallerM

tests/pos-macros/i12948b.scala

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted.*
2+
3+
package foo {
4+
trait Bar:
5+
def bar(using Quotes) = '{ Baz }
6+
7+
private[foo] object Baz
8+
}

tests/pos-macros/i8208/Macros_1.scala

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package playground
2+
3+
import scala.quoted._
4+
5+
object X {
6+
7+
inline def power(n: Int, x: Double): Double =
8+
${ powerImpl('n, 'x) }
9+
10+
private def powerImpl(nExpr: Expr[Int], xExpr: Expr[Double])(using Quotes): Expr[Double] =
11+
nExpr match {
12+
case Expr(n1) => '{ 42.0 }
13+
case _ => '{ dynamicPower($nExpr, $xExpr) }
14+
}
15+
16+
private def dynamicPower(n: Int, x: Double): Double = {
17+
println(s"dynamic: $n^$x")
18+
if (n == 0) 1.0
19+
else if (n % 2 == 0) dynamicPower(n / 2, x * x)
20+
else x * dynamicPower(n - 1, x)
21+
}
22+
}

tests/pos-macros/i8208/Test_2.scala

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import playground.X
2+
def test(x: Int) = X.power(x, 2)

0 commit comments

Comments
 (0)