Skip to content

Commit 17fcc6d

Browse files
committed
Make sure inline macros are static methods
1 parent e4ddb5b commit 17fcc6d

File tree

8 files changed

+23
-57
lines changed

8 files changed

+23
-57
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,8 @@ class ReifyQuotes extends MacroTransformWithImplicits {
510510
case tree: DefDef if tree.symbol.is(Macro) && level == 0 =>
511511
tree.rhs match {
512512
case InlineSplice(_) =>
513+
if (!tree.symbol.isStatic)
514+
ctx.error("Inline macro method must be a static method.", tree.pos)
513515
markDef(tree)
514516
nested(isQuote = true).transform(tree)
515517
// check macro code as it if appeared in a quoted context

tests/neg/quote-MacroOverride.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ object Test {
55
inline def g(): Unit = ()
66
}
77

8-
class B extends A {
8+
object B extends A {
99
inline def f() = ~('()) // error: may not override
1010
override def g() = () // error: may not override
1111
}

tests/pos/quote-interpolator-core/quoted_1.scala renamed to tests/neg/quote-interpolator-core-old.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import scala.quoted._
55
object FInterpolation {
66

77
implicit class FInterpolatorHelper(val sc: StringContext) extends AnyVal {
8-
inline def ff(arg1: Any): String = ~fInterpolation(sc, Seq('(arg1)))
9-
inline def ff(arg1: Any, arg2: Any): String = ~fInterpolation(sc, Seq('(arg1), '(arg2)))
10-
inline def ff(arg1: Any, arg2: Any, arg3: Any): String = ~fInterpolation(sc, Seq('(arg1), '(arg2), '(arg3)))
8+
inline def ff(arg1: Any): String = ~fInterpolation(sc, Seq('(arg1))) // error: Inline macro method must be a static method
9+
inline def ff(arg1: Any, arg2: Any): String = ~fInterpolation(sc, Seq('(arg1), '(arg2))) // error: Inline macro method must be a static method
10+
inline def ff(arg1: Any, arg2: Any, arg3: Any): String = ~fInterpolation(sc, Seq('(arg1), '(arg2), '(arg3))) // error: Inline macro method must be a static method
1111
// ...
1212
}
1313

@@ -24,4 +24,4 @@ object FInterpolation {
2424

2525
def hello = "hello"
2626

27-
}
27+
}

tests/pos/i3916/Macro_1.scala

Lines changed: 0 additions & 21 deletions
This file was deleted.

tests/pos/i3916/Test_2.scala

Lines changed: 0 additions & 5 deletions
This file was deleted.

tests/pos/quote-0.scala

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,27 @@ import scala.quoted._
22

33
import dotty.tools.dotc.quoted.Toolbox._
44

5-
class Test {
65

7-
object Macros {
6+
object Macros {
87

9-
inline def assert(expr: => Boolean): Unit =
10-
~ assertImpl('(expr))
8+
inline def assert(expr: => Boolean): Unit =
9+
~ assertImpl('(expr))
1110

12-
def assertImpl(expr: Expr[Boolean]) =
13-
'{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~showExpr(expr)}") }
11+
def assertImpl(expr: Expr[Boolean]) =
12+
'{ if !(~expr) then throw new AssertionError(s"failed assertion: ${~showExpr(expr)}") }
1413

15-
def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString.toExpr
14+
def showExpr[T](expr: Expr[T]): Expr[String] = expr.toString.toExpr
1615

17-
inline def power(inline n: Int, x: Double) = ~powerCode(n, '(x))
16+
inline def power(inline n: Int, x: Double) = ~powerCode(n, '(x))
1817

19-
def powerCode(n: Int, x: Expr[Double]): Expr[Double] =
20-
if (n == 0) '(1.0)
21-
else if (n == 1) x
22-
else if (n % 2 == 0) '{ { val y = ~x * ~x; ~powerCode(n / 2, '(y)) } }
23-
else '{ ~x * ~powerCode(n - 1, x) }
24-
}
18+
def powerCode(n: Int, x: Expr[Double]): Expr[Double] =
19+
if (n == 0) '(1.0)
20+
else if (n == 1) x
21+
else if (n % 2 == 0) '{ { val y = ~x * ~x; ~powerCode(n / 2, '(y)) } }
22+
else '{ ~x * ~powerCode(n - 1, x) }
23+
}
24+
25+
class Test {
2526

2627
val program = '{
2728
import Macros._

tests/pos/quote-assert/quoted_2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import dotty.tools.dotc.quoted.Toolbox._
22
import scala.quoted._
33
import Macros._
44

5-
class Test {
5+
object Test {
66

77
inline def assert(expr: => Boolean): Unit =
88
~ assertImpl('(expr))

tests/pos/quote-interpolator-core/quoted_2.scala

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)