From 25435cde739e8f0740168c8a02214df63315e255 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 24 Jan 2019 15:15:39 +0100 Subject: [PATCH 1/2] Add a more complex macro to test more APIs --- tests/run/reflect-select-copy2/assert_1.scala | 48 +++++++++++++++++++ tests/run/reflect-select-copy2/test_2.scala | 24 ++++++++++ 2 files changed, 72 insertions(+) create mode 100644 tests/run/reflect-select-copy2/assert_1.scala create mode 100644 tests/run/reflect-select-copy2/test_2.scala diff --git a/tests/run/reflect-select-copy2/assert_1.scala b/tests/run/reflect-select-copy2/assert_1.scala new file mode 100644 index 000000000000..64da10a7bca2 --- /dev/null +++ b/tests/run/reflect-select-copy2/assert_1.scala @@ -0,0 +1,48 @@ +import scala.quoted._ +import scala.tasty._ + +object scalatest { + + inline def assert(condition: => Boolean): Unit = ~assertImpl('(condition), '("")) + + def assertImpl(cond: Expr[Boolean], clue: Expr[Any])(implicit refl: Reflection): Expr[Unit] = { + import refl._ + import util._ + import quoted.Toolbox.Default._ + + def isImplicitMethodType(tp: Type): Boolean = + Type.IsMethodType.unapply(tp).flatMap(tp => if tp.isImplicit then Some(true) else None).nonEmpty + + cond.unseal.underlyingArgument match { + case Term.Apply(sel @ Term.Select(lhs, op), rhs :: Nil) => + let(lhs) { left => + let(rhs) { right => + let(Term.Apply(Term.Select.copy(sel)(left, op), right :: Nil)) { result => + val l = left.seal[Any] + val r = right.seal[Any] + val b = result.seal[Boolean] + val code = '{ scala.Predef.assert(~b) } + code.unseal + } + } + }.seal[Unit] + case Term.Apply(f @ Term.Apply(Term.IsSelect(sel @ Term.Select(Term.Apply(qual, lhs :: Nil), op)), rhs :: Nil), implicits) + if isImplicitMethodType(f.tpe) => + let(lhs) { left => + let(rhs) { right => + let(Term.Apply(Term.Apply(Term.Select.copy(sel)(Term.Apply(qual, left :: Nil), op), right :: Nil), implicits)) { result => + val l = left.seal[Any] + val r = right.seal[Any] + val b = result.seal[Boolean] + val code = '{ scala.Predef.assert(~b) } + code.unseal + } + } + }.seal[Unit] + + case _ => + '{ scala.Predef.assert(~cond) } + } + } + +} diff --git a/tests/run/reflect-select-copy2/test_2.scala b/tests/run/reflect-select-copy2/test_2.scala new file mode 100644 index 000000000000..a2361f72184e --- /dev/null +++ b/tests/run/reflect-select-copy2/test_2.scala @@ -0,0 +1,24 @@ +object Test { + import scalatest._ + + case class Box[T](v: T) { + def >(that: Box[T]): Boolean = this == that + } + + trait EqInt + implicit val eq: EqInt = new EqInt {} + + implicit class AnyOps[T](x: T) { + def === (y: T)(implicit c: EqInt) = x == y + } + + def main(args: Array[String]): Unit = { + val a = Box(Some(10)) + assert(a.v === Some(10)) + assert(10 > 5) + assert(4.0 < 5) + assert(6.0 > 5L) + assert(Box(6) > Box(6)) + assert(Box("h") > Box("h")) + } +} From 516639311a5077030f04334c855c849b21c980f2 Mon Sep 17 00:00:00 2001 From: Liu Fengyun Date: Thu, 24 Jan 2019 15:32:56 +0100 Subject: [PATCH 2/2] Remove default case to ensure match --- .../reflect-select-copy}/assert_1.scala | 3 --- .../reflect-select-copy}/test_2.scala | 11 ++++++++--- 2 files changed, 8 insertions(+), 6 deletions(-) rename tests/{run/reflect-select-copy2 => run-with-compiler/reflect-select-copy}/assert_1.scala (96%) rename tests/{run/reflect-select-copy2 => run-with-compiler/reflect-select-copy}/test_2.scala (70%) diff --git a/tests/run/reflect-select-copy2/assert_1.scala b/tests/run-with-compiler/reflect-select-copy/assert_1.scala similarity index 96% rename from tests/run/reflect-select-copy2/assert_1.scala rename to tests/run-with-compiler/reflect-select-copy/assert_1.scala index 64da10a7bca2..3537676ff1e2 100644 --- a/tests/run/reflect-select-copy2/assert_1.scala +++ b/tests/run-with-compiler/reflect-select-copy/assert_1.scala @@ -39,9 +39,6 @@ object scalatest { } } }.seal[Unit] - - case _ => - '{ scala.Predef.assert(~cond) } } } diff --git a/tests/run/reflect-select-copy2/test_2.scala b/tests/run-with-compiler/reflect-select-copy/test_2.scala similarity index 70% rename from tests/run/reflect-select-copy2/test_2.scala rename to tests/run-with-compiler/reflect-select-copy/test_2.scala index a2361f72184e..1525360a71f7 100644 --- a/tests/run/reflect-select-copy2/test_2.scala +++ b/tests/run-with-compiler/reflect-select-copy/test_2.scala @@ -14,10 +14,15 @@ object Test { def main(args: Array[String]): Unit = { val a = Box(Some(10)) + val five: Float = 5.0f + val six: Double = 6.0 + val ten: Int = 10 assert(a.v === Some(10)) - assert(10 > 5) - assert(4.0 < 5) - assert(6.0 > 5L) + assert(five < six) + assert(five > 4) + assert(ten > 5) + assert(six < 7) + assert(six > 5L) assert(Box(6) > Box(6)) assert(Box("h") > Box("h")) }