Skip to content

Remove Macro flag from Dotty macros #4855

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

Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Fix #4803: Remove restriction
Allow top-level ~ in and non static transparent method:
* Class methods (including inner and anonymous classes)
* Methods in def/val/var
  • Loading branch information
nicolasstucki committed Jul 27, 2018
commit c27ca822e69c544ff65de7bec9dad3eb103ae988
2 changes: 1 addition & 1 deletion bench/tests/power-macro/PowerMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ object PowerMacro {

def powerCode(n: Long, x: Expr[Double]): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ { val y = ~x * ~x; ~powerCode(n / 2, '(y)) } }
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode(n / 2, '(y)) }
else '{ ~x * ~powerCode(n - 1, x) }

}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -565,11 +565,11 @@ class ReifyQuotes extends MacroTransformWithImplicits {
case _: Import =>
tree
case tree: DefDef if tree.symbol.is(Macro) && level == 0 =>
if (enclosingInlineds.nonEmpty)
return EmptyTree // Already checked at definition site and already inlined
markDef(tree)
tree.rhs match {
case InlineSplice(_) =>
if (!tree.symbol.isStatic)
ctx.error("Transparent macro method must be a static method.", tree.pos)
mapOverTree(enteredSyms) // Ignore output, only check PCP
cpy.DefDef(tree)(rhs = defaultValue(tree.rhs.tpe))
case _ =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import scala.quoted._

class Foo {
transparent def foo: Unit = ~Foo.impl // error
transparent def foo: Unit = ~Foo.impl
object Bar {
transparent def foo: Unit = ~Foo.impl // error
transparent def foo: Unit = ~Foo.impl
}
}

object Foo {
class Baz {
transparent def foo: Unit = ~impl // error
transparent def foo: Unit = ~impl
}
object Quox {
transparent def foo: Unit = ~Foo.impl
Expand Down
8 changes: 8 additions & 0 deletions tests/run/i4803.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1.0
1.5
2.25
7.59375
1.0
1.5
2.25
7.59375
20 changes: 20 additions & 0 deletions tests/run/i4803/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

class Num2(x: Double) {
transparent def power(transparent n: Long) = ~PowerMacro.powerCode('(x), n)
}

object Test {
def main(args: Array[String]): Unit = {
val n = new Num(1.5)
println(n.power(0))
println(n.power(1))
println(n.power(2))
println(n.power(5))

val n2 = new Num2(1.5)
println(n.power(0))
println(n.power(1))
println(n.power(2))
println(n.power(5))
}
}
12 changes: 12 additions & 0 deletions tests/run/i4803/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted._

object PowerMacro {
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
else '{ ~x * ~powerCode(x, n - 1) }
}

class Num(x: Double) {
transparent def power(transparent n: Long) = ~PowerMacro.powerCode('(x), n)
}
4 changes: 4 additions & 0 deletions tests/run/i4803b.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
1.0
1.5
2.25
7.59375
18 changes: 18 additions & 0 deletions tests/run/i4803b/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@


class Nums {
class Num(x: Double) {
transparent def power(transparent n: Long) = ~PowerMacro.powerCode('(x), n)
}
}

object Test {
def main(args: Array[String]): Unit = {
val nums = new Nums
val n = new nums.Num(1.5)
println(n.power(0))
println(n.power(1))
println(n.power(2))
println(n.power(5))
}
}
8 changes: 8 additions & 0 deletions tests/run/i4803b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._

object PowerMacro {
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
else '{ ~x * ~powerCode(x, n - 1) }
}
8 changes: 8 additions & 0 deletions tests/run/i4803c.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
1.0
1.5
2.25
7.59375
1.0
1.5
2.25
7.59375
22 changes: 22 additions & 0 deletions tests/run/i4803c/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

object Test {
def main(args: Array[String]): Unit = {
class Num(x: Double) {
transparent def power(transparent n: Long) = ~PowerMacro.powerCode('(x), n)
}
val n = new Num(1.5)
println(n.power(0))
println(n.power(1))
println(n.power(2))
println(n.power(5))

transparent def power(x: Double, transparent n: Long) = ~PowerMacro.powerCode('(x), n)

val x: Double = 1.5

println(power(x, 0))
println(power(x, 1))
println(power(x, 2))
println(power(x, 5))
}
}
8 changes: 8 additions & 0 deletions tests/run/i4803c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._

object PowerMacro {
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
else '{ ~x * ~powerCode(x, n - 1) }
}
3 changes: 3 additions & 0 deletions tests/run/i4803d.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.0
2.25
12.25
17 changes: 17 additions & 0 deletions tests/run/i4803d/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

object Test {
def main(args: Array[String]): Unit = {
val x1: Double = 0
val x2: Double = 1.5
val x3: Double = 3.5

println(power2(x1))
println(power2(x2))
println(power2(x3))
}

transparent def power2(x: Double) = {
transparent def power(x: Double, transparent n: Long) = ~PowerMacro.powerCode('(x), n)
power(x, 2)
}
}
8 changes: 8 additions & 0 deletions tests/run/i4803d/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import scala.quoted._

object PowerMacro {
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
else '{ ~x * ~powerCode(x, n - 1) }
}
14 changes: 14 additions & 0 deletions tests/run/i4803e/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

object Test {
def main(args: Array[String]): Unit = {
val x1: Double = 0
val x2: Double = 1.5
val x3: Double = 3.5

println(power2(x1))
println(power2(x2))
println(power2(x3))
}

transparent def power2(x: Double) = ~PowerMacro.power2('(x))
}
11 changes: 11 additions & 0 deletions tests/run/i4803e/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.quoted._

object PowerMacro {
def power2(x: Expr[Double]) = '{
transparent def power(x: Double, n: Long): Double =
if (n == 0) 1.0
else if (n % 2 == 0) { val y = x * x; power(y, n / 2) }
else x * power(x, n - 1)
power(~x, 2)
}
}
14 changes: 14 additions & 0 deletions tests/run/i4803f/App_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@

object Test {
def main(args: Array[String]): Unit = {
val x1: Double = 0
val x2: Double = 1.5
val x3: Double = 3.5

println(power2(x1))
println(power2(x2))
println(power2(x3))
}

transparent def power2(x: Double) = ~PowerMacro.power2('(x))
}
13 changes: 13 additions & 0 deletions tests/run/i4803f/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.quoted._

object PowerMacro {
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
if (n == 0) '(1.0)
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
else '{ ~x * ~powerCode(x, n - 1) }

def power2(x: Expr[Double]) = '{
transparent def power(x: Double): Double = ~powerCode('(x), 2)
power(~x)
}
}