From 9fd494ae76f85ee2bbf2ed52739d9564d7a3e880 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 28 Jul 2021 16:56:09 +0200 Subject: [PATCH 1/3] 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 --- .../tools/dotc/typer/QuotesAndSplices.scala | 5 ++++- tests/neg-macros/i7068-c.scala | 2 +- tests/pos-macros/i12948/Macros_1.scala | 9 ++++++++ tests/pos-macros/i12948/Test_2.scala | 5 +++++ tests/pos-macros/i12948b.scala | 8 +++++++ tests/pos-macros/i8208/Macros_1.scala | 22 +++++++++++++++++++ tests/pos-macros/i8208/Test_2.scala | 2 ++ 7 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 tests/pos-macros/i12948/Macros_1.scala create mode 100644 tests/pos-macros/i12948/Test_2.scala create mode 100644 tests/pos-macros/i12948b.scala create mode 100644 tests/pos-macros/i8208/Macros_1.scala create mode 100644 tests/pos-macros/i8208/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index 9d0f5f15de7e..fbae1afcbfbd 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -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") diff --git a/tests/neg-macros/i7068-c.scala b/tests/neg-macros/i7068-c.scala index 7c6714d525c0..b5d416b3240d 100644 --- a/tests/neg-macros/i7068-c.scala +++ b/tests/neg-macros/i7068-c.scala @@ -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() diff --git a/tests/pos-macros/i12948/Macros_1.scala b/tests/pos-macros/i12948/Macros_1.scala new file mode 100644 index 000000000000..a18d9daec2fe --- /dev/null +++ b/tests/pos-macros/i12948/Macros_1.scala @@ -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 } diff --git a/tests/pos-macros/i12948/Test_2.scala b/tests/pos-macros/i12948/Test_2.scala new file mode 100644 index 000000000000..386f1b3bd8f5 --- /dev/null +++ b/tests/pos-macros/i12948/Test_2.scala @@ -0,0 +1,5 @@ +import mylib.Main + +object Test: + Main.fooCaller + Main.fooCallerM diff --git a/tests/pos-macros/i12948b.scala b/tests/pos-macros/i12948b.scala new file mode 100644 index 000000000000..31f7c24bfe99 --- /dev/null +++ b/tests/pos-macros/i12948b.scala @@ -0,0 +1,8 @@ +import scala.quoted.* + +package foo { + trait Bar: + def bar(using Quotes) = '{ Baz } + + private[foo] object Baz +} diff --git a/tests/pos-macros/i8208/Macros_1.scala b/tests/pos-macros/i8208/Macros_1.scala new file mode 100644 index 000000000000..ce21fadc947a --- /dev/null +++ b/tests/pos-macros/i8208/Macros_1.scala @@ -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) + } +} diff --git a/tests/pos-macros/i8208/Test_2.scala b/tests/pos-macros/i8208/Test_2.scala new file mode 100644 index 000000000000..c5e239e50970 --- /dev/null +++ b/tests/pos-macros/i8208/Test_2.scala @@ -0,0 +1,2 @@ +import playground.X +def test(x: Int) = X.power(x, 2) From b523c16edb6841e0db9f864767863126cd539b83 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Mon, 2 Aug 2021 15:11:20 +0200 Subject: [PATCH 2/3] Remove extra test related with issue #13215 --- tests/pos-macros/i12948b.scala | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 tests/pos-macros/i12948b.scala diff --git a/tests/pos-macros/i12948b.scala b/tests/pos-macros/i12948b.scala deleted file mode 100644 index 31f7c24bfe99..000000000000 --- a/tests/pos-macros/i12948b.scala +++ /dev/null @@ -1,8 +0,0 @@ -import scala.quoted.* - -package foo { - trait Bar: - def bar(using Quotes) = '{ Baz } - - private[foo] object Baz -} From 63f0f4365600aa7c0a36d1f01a710aaae72e5177 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Tue, 3 Aug 2021 10:13:58 +0200 Subject: [PATCH 3/3] Update Monocle --- community-build/community-projects/Monocle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/community-build/community-projects/Monocle b/community-build/community-projects/Monocle index 4613afff874c..bc5781caf523 160000 --- a/community-build/community-projects/Monocle +++ b/community-build/community-projects/Monocle @@ -1 +1 @@ -Subproject commit 4613afff874c23d17bde580671ff29c7a13123d0 +Subproject commit bc5781caf523eab7e2c0e92478cbbef568a1be43