Skip to content

Commit 78211ae

Browse files
committed
Do not allow calls to inline metthods directly in macro splice
Also properly support canceled macro splices. These will warn the user and compile as if they where a normal inline method. Fixes #11688
1 parent 02cee1f commit 78211ae

File tree

10 files changed

+26
-16
lines changed

10 files changed

+26
-16
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 ||

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)