Skip to content

Commit da7aae2

Browse files
Merge pull request #11746 from dotty-staging/fix-#11688
Do not allow calls to inline methods directly in macro splice
2 parents 2caf209 + 07b4de1 commit da7aae2

File tree

11 files changed

+28
-18
lines changed

11 files changed

+28
-18
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object Splicer {
161161
case SeqLiteral(elems, _) =>
162162
elems.foreach(checkIfValidArgument)
163163

164-
case tree: Ident if tree.symbol.is(Inline) || summon[Env].contains(tree.symbol) =>
164+
case tree: Ident if summon[Env].contains(tree.symbol) =>
165165
// OK
166166

167167
case _ =>
@@ -186,6 +186,9 @@ object Splicer {
186186
case Typed(expr, _) =>
187187
checkIfValidStaticCall(expr)
188188

189+
case Apply(Select(Apply(fn, quoted :: Nil), nme.apply), _) if fn.symbol == defn.QuotedRuntime_exprQuote =>
190+
// OK, canceled and warning emitted
191+
189192
case Call(fn, args)
190193
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package)) ||
191194
fn.symbol.is(Module) || fn.symbol.isStatic ||

compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,10 @@ object PrepareInlineable {
287287
"""Malformed macro.
288288
|
289289
|Expected the splice ${...} to be at the top of the RHS:
290-
| inline def foo(inline x: X, ..., y: Y): Int = ${impl(x, ... '{y}})
290+
| inline def foo(inline x: X, ..., y: Y): Int = ${ impl('x, ... 'y) }
291291
|
292292
| * The contents of the splice must call a static method
293-
| * All arguments must be quoted or inline
293+
| * All arguments must be quoted
294294
""".stripMargin, inlined.srcPos)
295295
}
296296
checkMacro(body)

tests/neg-macros/i11688.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted._
2+
3+
def myMacro(a: Int)(using Quotes) = Expr(a)
4+
def baz = 42
5+
inline def bar = baz
6+
inline def foo = ${myMacro(bar)} // error

tests/neg-macros/i4431.scala

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

tests/neg-macros/i4492/quoted_1.scala

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

tests/neg-macros/i4493-b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ class Index[K]
33
object Index {
44
inline def succ[K](x: K): Unit = ${
55
implicit val t: Type[K] = Type.of[K] // error
6-
'{new Index[K]} // error
6+
'{new Index[K]}
77
}
88
}

tests/neg-macros/i4493.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ class Index[K]
33
object Index {
44
inline def succ[K]: Unit = ${
55
implicit val t: Type[K] = Type.of[K] // error
6-
'{new Index[K]} // error
6+
'{new Index[K]}
77
}
88
}

tests/pos-macros/i4431.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import scala.quoted.*
2+
3+
object Macros {
4+
inline def h(f: => Int => String): String = ${'{f(42)}}
5+
val a = h(_.toString)
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Index[K]
22
object Index {
33
inline def succ[K]: Unit = ${
4-
'{new Index[K]} // error
4+
'{new Index[K]}
55
}
66
}

tests/run-macros/i4492/quoted_1.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
trait Index
3+
4+
object Index {
5+
inline def succ(prev: Index): Unit = ${ '{println("Ok")} }
6+
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
object Test {
33
def main(args: Array[String]): Unit = {
4-
Index.succ(null) // error
4+
Index.succ(null)
55
}
66
}

0 commit comments

Comments
 (0)