diff --git a/tests/run-with-compiler/reflect-select-copy/assert_1.scala b/tests/run-with-compiler/reflect-select-copy/assert_1.scala new file mode 100644 index 000000000000..3537676ff1e2 --- /dev/null +++ b/tests/run-with-compiler/reflect-select-copy/assert_1.scala @@ -0,0 +1,45 @@ +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] + } + } + +} diff --git a/tests/run-with-compiler/reflect-select-copy/test_2.scala b/tests/run-with-compiler/reflect-select-copy/test_2.scala new file mode 100644 index 000000000000..1525360a71f7 --- /dev/null +++ b/tests/run-with-compiler/reflect-select-copy/test_2.scala @@ -0,0 +1,29 @@ +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)) + val five: Float = 5.0f + val six: Double = 6.0 + val ten: Int = 10 + assert(a.v === Some(10)) + 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")) + } +}