Skip to content

Do not allow calls to inline methods directly in macro splice #11746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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)
}
}