Skip to content

Commit

Permalink
Merge pull request #11746 from dotty-staging/fix-#11688
Browse files Browse the repository at this point in the history
Do not allow calls to inline methods directly in macro splice
  • Loading branch information
nicolasstucki authored Mar 17, 2021
2 parents 2caf209 + 07b4de1 commit da7aae2
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 18 deletions.
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ object Splicer {
case SeqLiteral(elems, _) =>
elems.foreach(checkIfValidArgument)

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

case _ =>
Expand All @@ -186,6 +186,9 @@ object Splicer {
case Typed(expr, _) =>
checkIfValidStaticCall(expr)

case Apply(Select(Apply(fn, quoted :: Nil), nme.apply), _) if fn.symbol == defn.QuotedRuntime_exprQuote =>
// OK, canceled and warning emitted

case Call(fn, args)
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package)) ||
fn.symbol.is(Module) || fn.symbol.isStatic ||
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -287,10 +287,10 @@ object PrepareInlineable {
"""Malformed macro.
|
|Expected the splice ${...} to be at the top of the RHS:
| inline def foo(inline x: X, ..., y: Y): Int = ${impl(x, ... '{y}})
| inline def foo(inline x: X, ..., y: Y): Int = ${ impl('x, ... 'y) }
|
| * The contents of the splice must call a static method
| * All arguments must be quoted or inline
| * All arguments must be quoted
""".stripMargin, inlined.srcPos)
}
checkMacro(body)
Expand Down
6 changes: 6 additions & 0 deletions tests/neg-macros/i11688.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.quoted._

def myMacro(a: Int)(using Quotes) = Expr(a)
def baz = 42
inline def bar = baz
inline def foo = ${myMacro(bar)} // error
5 changes: 0 additions & 5 deletions tests/neg-macros/i4431.scala

This file was deleted.

6 changes: 0 additions & 6 deletions tests/neg-macros/i4492/quoted_1.scala

This file was deleted.

2 changes: 1 addition & 1 deletion tests/neg-macros/i4493-b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Index[K]
object Index {
inline def succ[K](x: K): Unit = ${
implicit val t: Type[K] = Type.of[K] // error
'{new Index[K]} // error
'{new Index[K]}
}
}
2 changes: 1 addition & 1 deletion tests/neg-macros/i4493.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class Index[K]
object Index {
inline def succ[K]: Unit = ${
implicit val t: Type[K] = Type.of[K] // error
'{new Index[K]} // error
'{new Index[K]}
}
}
6 changes: 6 additions & 0 deletions tests/pos-macros/i4431.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import scala.quoted.*

object Macros {
inline def h(f: => Int => String): String = ${'{f(42)}}
val a = h(_.toString)
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Index[K]
object Index {
inline def succ[K]: Unit = ${
'{new Index[K]} // error
'{new Index[K]}
}
}
6 changes: 6 additions & 0 deletions tests/run-macros/i4492/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

trait Index

object Index {
inline def succ(prev: Index): Unit = ${ '{println("Ok")} }
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

object Test {
def main(args: Array[String]): Unit = {
Index.succ(null) // error
Index.succ(null)
}
}

0 comments on commit da7aae2

Please sign in to comment.