Skip to content

Add regression test #6478

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 1 commit into from
May 9, 2019
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import scala.quoted._
import scala.quoted.autolift._
import scala.quoted.matching._
import scala.tasty.Reflection

import scala.language.implicitConversions

object Foo {
implicit object StringContextOps {
inline def (ctx: => StringContext) foo (args: => Any*): String = ${ Macro.foo('ctx, 'args) }
}
}


object TestFooErrors { // Defined in tests
implicit object StringContextOps {
inline def (ctx: => StringContext) foo (args: => Any*): List[(Int, Int, Int, String)] = ${ Macro.fooErrors('ctx, 'args) }
}
}

object Macro {

def foo(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]]) given (reflect: Reflection): Expr[String] = {
(sc, argsExpr) match {
case ('{ StringContext(${ExprSeq(parts)}: _*) }, ExprSeq(args)) =>
val reporter = new Reporter {
def errorOnPart(msg: String, partIdx: Int): Unit = {
import reflect._
error(msg, parts(partIdx).unseal.pos)
}
}
fooCore(parts, args, reporter)
}
}

def fooErrors(sc: Expr[StringContext], argsExpr: Expr[Seq[Any]]) given (reflect: Reflection): Expr[List[(Int, Int, Int, String)]] = {
(sc, argsExpr) match {
case ('{ StringContext(${ExprSeq(parts)}: _*) }, ExprSeq(args)) =>
val errors = List.newBuilder[Expr[(Int, Int, Int, String)]]
val reporter = new Reporter {
def errorOnPart(msg: String, partIdx: Int): Unit = {
import reflect._
val pos = parts(partIdx).unseal.pos
errors += '{ Tuple4($partIdx, ${pos.start}, ${pos.end}, $msg) }
}
}
fooCore(parts, args, reporter) // Discard result
errors.result().toExprOfList
}


}


private def fooCore(parts: Seq[Expr[String]], args: Seq[Expr[Any]], reporter: Reporter) given Reflection: Expr[String] = {
for ((part, idx) <- parts.zipWithIndex) {
val Const(v: String) = part
if (v.contains("#"))
reporter.errorOnPart("Cannot use #", idx)
}

'{ StringContext(${parts.toList.toExprOfList}: _*).s(${args.toList.toExprOfList}: _*) }
}


trait Reporter {
def errorOnPart(msg: String, partIdx: Int): Unit
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

object Test {

def main(args: Array[String]): Unit = {
posTests()
negTests()
}

def posTests() = {
import Foo._
assertEquals(foo"abc${"123"}def", "abc123def")
}

def negTests() = {
import TestFooErrors._
assertEquals(foo"a#c${"123"}def", List((0, 256, 259, "Cannot use #")))
assertEquals(foo"abc${"123"}#ef", List((1, 342, 345, "Cannot use #")))
assertEquals(foo"a#c${"123"}#ef", List((0, 406, 409, "Cannot use #"), (1, 417, 420, "Cannot use #")))
}

def assertEquals(actual: Any, expected: Any): Unit = {
assert(actual == expected, s"actual: $actual\nbut expected: $expected")
}
}