Skip to content

Commit e11b714

Browse files
committed
Fix #4803: Remove restriction
Allow top-level ~ in and non static inline method: * Class methods (including inner and anonymous classes) * Methods in def/val/var
1 parent 2f85824 commit e11b714

File tree

12 files changed

+112
-6
lines changed

12 files changed

+112
-6
lines changed

bench/tests/power-macro/PowerMacro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ object PowerMacro {
66

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

1212
}

compiler/src/dotty/tools/dotc/transform/ReifyQuotes.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,6 @@ class ReifyQuotes extends MacroTransformWithImplicits {
568568
markDef(tree)
569569
tree.rhs match {
570570
case InlineSplice(_) =>
571-
if (!tree.symbol.isStatic)
572-
ctx.error("Transparent macro method must be a static method.", tree.pos)
573571
mapOverTree(enteredSyms) // Ignore output, only check PCP
574572
cpy.DefDef(tree)(rhs = defaultValue(tree.rhs.tpe))
575573
case _ =>

tests/neg/quote-non-static-macro.scala renamed to tests/pos/quote-non-static-macro.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import scala.quoted._
22

33
class Foo {
4-
transparent def foo: Unit = ~Foo.impl // error
4+
transparent def foo: Unit = ~Foo.impl
55
object Bar {
6-
transparent def foo: Unit = ~Foo.impl // error
6+
transparent def foo: Unit = ~Foo.impl
77
}
88
}
99

1010
object Foo {
1111
class Baz {
12-
transparent def foo: Unit = ~impl // error
12+
transparent def foo: Unit = ~impl
1313
}
1414
object Quox {
1515
transparent def foo: Unit = ~Foo.impl

tests/run/i4803.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1.0
2+
1.5
3+
2.25
4+
7.59375
5+
1.0
6+
1.5
7+
2.25
8+
7.59375

tests/run/i4803/App_2.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
2+
class Num2(x: Double) {
3+
inline def power(inline n: Long) = ~PowerMacro.powerCode('(x), n)
4+
}
5+
6+
object Test {
7+
def main(args: Array[String]): Unit = {
8+
val n = new Num(1.5)
9+
println(n.power(0))
10+
println(n.power(1))
11+
println(n.power(2))
12+
println(n.power(5))
13+
14+
val n2 = new Num2(1.5)
15+
println(n.power(0))
16+
println(n.power(1))
17+
println(n.power(2))
18+
println(n.power(5))
19+
}
20+
}

tests/run/i4803/Macro_1.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.quoted._
2+
3+
object PowerMacro {
4+
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
5+
if (n == 0) '(1.0)
6+
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
7+
else '{ ~x * ~powerCode(x, n - 1) }
8+
}
9+
10+
class Num(x: Double) {
11+
inline def power(inline n: Long) = ~PowerMacro.powerCode('(x), n)
12+
}

tests/run/i4803b.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
1.0
2+
1.5
3+
2.25
4+
7.59375

tests/run/i4803b/App_2.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
class Nums {
4+
class Num(x: Double) {
5+
inline def power(inline n: Long) = ~PowerMacro.powerCode('(x), n)
6+
}
7+
}
8+
9+
object Test {
10+
def main(args: Array[String]): Unit = {
11+
val nums = new Nums
12+
val n = new nums.Num(1.5)
13+
println(n.power(0))
14+
println(n.power(1))
15+
println(n.power(2))
16+
println(n.power(5))
17+
}
18+
}

tests/run/i4803b/Macro_1.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._
2+
3+
object PowerMacro {
4+
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
5+
if (n == 0) '(1.0)
6+
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
7+
else '{ ~x * ~powerCode(x, n - 1) }
8+
}

tests/run/i4803c.check

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
1.0
2+
1.5
3+
2.25
4+
7.59375
5+
1.0
6+
1.5
7+
2.25
8+
7.59375

tests/run/i4803c/App_2.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
object Test {
3+
def main(args: Array[String]): Unit = {
4+
class Num(x: Double) {
5+
inline def power(inline n: Long) = ~PowerMacro.powerCode('(x), n)
6+
}
7+
val n = new Num(1.5)
8+
println(n.power(0))
9+
println(n.power(1))
10+
println(n.power(2))
11+
println(n.power(5))
12+
13+
inline def power(x: Double, inline n: Long) = ~PowerMacro.powerCode('(x), n)
14+
15+
val x: Double = 1.5
16+
17+
println(power(x, 0))
18+
println(power(x, 1))
19+
println(power(x, 2))
20+
println(power(x, 5))
21+
}
22+
}

tests/run/i4803c/Macro_1.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import scala.quoted._
2+
3+
object PowerMacro {
4+
def powerCode(x: Expr[Double], n: Long): Expr[Double] =
5+
if (n == 0) '(1.0)
6+
else if (n % 2 == 0) '{ val y = ~x * ~x; ~powerCode('(y), n / 2) }
7+
else '{ ~x * ~powerCode(x, n - 1) }
8+
}

0 commit comments

Comments
 (0)