Skip to content

Commit e4ddb5b

Browse files
committed
Fix #4151: Make sure the definition of the macro is correcty formed
1 parent c5e40dc commit e4ddb5b

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,26 @@ class ReifyQuotes extends MacroTransformWithImplicits {
508508
case _: Import =>
509509
tree
510510
case tree: DefDef if tree.symbol.is(Macro) && level == 0 =>
511-
markDef(tree)
512-
nested(isQuote = true).transform(tree)
513-
// check macro code as it if appeared in a quoted context
514-
cpy.DefDef(tree)(rhs = EmptyTree)
511+
tree.rhs match {
512+
case InlineSplice(_) =>
513+
markDef(tree)
514+
nested(isQuote = true).transform(tree)
515+
// check macro code as it if appeared in a quoted context
516+
cpy.DefDef(tree)(rhs = EmptyTree)
517+
case _ =>
518+
ctx.error(
519+
"""Malformed inline macro.
520+
|
521+
|Expected the ~ to be at the top of the RHS:
522+
| inline def foo(...): Int = ~impl(...)
523+
|or
524+
| inline def foo(...): Int = ~{
525+
| val x = 1
526+
| impl(... x ...)
527+
| }
528+
""".stripMargin, tree.rhs.pos)
529+
EmptyTree
530+
}
515531
case _ =>
516532
markDef(tree)
517533
checkLevel(mapOverTree(enteredSyms))

tests/neg/quote-macro-splice.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import scala.quoted._
2+
3+
object Test {
4+
5+
inline def foo1: Int = { // error
6+
println()
7+
~impl(1.toExpr)
8+
}
9+
10+
inline def foo2: Int = { // error
11+
~impl(1.toExpr)
12+
~impl(2.toExpr)
13+
}
14+
15+
inline def foo3: Int = { // error
16+
val a = 1
17+
~impl('(a))
18+
}
19+
20+
inline def foo4: Int = { // error
21+
~impl('(1))
22+
1
23+
}
24+
25+
def impl(n: Expr[Int]): Expr[Int] = ???
26+
27+
}

0 commit comments

Comments
 (0)