Skip to content

Disallow interpretation of arbitrary calls in parameters #10103

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
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 1 addition & 7 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,12 @@ object Splicer {
case Apply(Select(Apply(fn, quoted :: Nil), nme.apply), _) if fn.symbol == defn.InternalQuoted_exprQuote =>
// OK

case TypeApply(fn, quoted :: Nil) if fn.symbol == defn.QuotedTypeModule_apply =>
case Apply(Select(TypeApply(fn, List(quoted)), nme.apply), _)if fn.symbol == defn.QuotedTypeModule_apply =>
// OK

case Literal(Constant(value)) =>
// OK

case Call(fn, args)
if (fn.symbol.isConstructor && fn.symbol.owner.owner.is(Package)) ||
fn.symbol.is(Module) || fn.symbol.isStatic ||
(fn.qualifier.symbol.is(Module) && fn.qualifier.symbol.isStatic) =>
args.foreach(_.foreach(checkIfValidArgument))

case NamedArg(_, arg) =>
checkIfValidArgument(arg)

Expand Down
6 changes: 6 additions & 0 deletions tests/neg-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")} } // error
}
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)
Index.succ(null) // error
}
}
5 changes: 3 additions & 2 deletions tests/neg-macros/i4493-b.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import scala.quoted._
class Index[K]
object Index {
inline def succ[K](x: K): Unit = ${
implicit val t: quoted.Type[K] = Type[K] // error
'{new Index[K]}
implicit val t: Type[K] = Type[K] // error
'{new Index[K]} // error
}
}
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]}
'{new Index[K]} // error
}
}
5 changes: 3 additions & 2 deletions tests/neg-macros/i4493.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import scala.quoted._
class Index[K]
object Index {
inline def succ[K]: Unit = ${
implicit val t: quoted.Type[K] = Type[K] // error
'{new Index[K]}
implicit val t: Type[K] = Type[K] // error
'{new Index[K]} // error
}
}
9 changes: 9 additions & 0 deletions tests/neg-macros/i5547.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import scala.quoted._

object scalatest {
inline def assert2(condition: => Boolean): Unit =
${ assertImpl('condition, Expr("")) } // error

def assertImpl(condition: Expr[Boolean], clue: Expr[Any])(using QuoteContext): Expr[Unit] =
'{}
}
2 changes: 1 addition & 1 deletion tests/pos-macros/i4023c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import scala.quoted._
object Macro {
inline def ff[T](x: T): T = ${ impl('x)(Type[T], summon[QuoteContext]) }
inline def ff[T](x: T): T = ${ impl('x) }
def impl[T](x: Expr[T])(implicit t: Type[T], qctx: QuoteContext): Expr[T] = '{ $x: $t }
}
5 changes: 1 addition & 4 deletions tests/pos-macros/i5547.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ import scala.quoted._

object scalatest {
inline def assert1(condition: => Boolean): Unit =
${assertImpl('condition, '{""})}

inline def assert2(condition: => Boolean): Unit =
${ assertImpl('condition, Expr("")) }
${assertImpl('condition, '{""})}

def assertImpl(condition: Expr[Boolean], clue: Expr[Any])(using QuoteContext): Expr[Unit] =
'{}
Expand Down
6 changes: 0 additions & 6 deletions tests/run-macros/i4492/quoted_1.scala

This file was deleted.

15 changes: 12 additions & 3 deletions tests/run-macros/quote-matcher-symantics-2/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import scala.quoted.unsafe._

object Macros {

inline def liftString(inline a: DSL): String = ${impl(StringNum, 'a)}
inline def liftString(inline a: DSL): String = ${implStringNum('a)}

inline def liftCompute(inline a: DSL): Int = ${impl(ComputeNum, 'a)}
private def implStringNum(a: Expr[DSL])(using qctx: QuoteContext): Expr[String] =
impl(StringNum, a)

inline def liftAST(inline a: DSL): ASTNum = ${impl(ASTNum, 'a)}
inline def liftCompute(inline a: DSL): Int = ${implComputeNum('a)}

private def implComputeNum(a: Expr[DSL])(using qctx: QuoteContext): Expr[Int] =
impl(ComputeNum, a)

inline def liftAST(inline a: DSL): ASTNum = ${implASTNum('a)}

private def implASTNum(a: Expr[DSL])(using qctx: QuoteContext): Expr[ASTNum] =
impl(ASTNum, a)

private def impl[T: Type](sym: Symantics[T], a: Expr[DSL])(using qctx: QuoteContext): Expr[T] = {

Expand Down