Skip to content

Add reflect API: Select.apply with a symbol #6615

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
Jun 6, 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
3 changes: 3 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/KernelImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ class KernelImpl(val rootContext: core.Contexts.Context, val rootPosition: util.
if (self.symbol.signature == core.Signature.NotAMethod) None
else Some(self.symbol.signature)

def Select_apply(qualifier: Term, symbol: Symbol)(implicit ctx: Context): Select =
withDefaultPos(implicit ctx => tpd.Select(qualifier, Types.TermRef(qualifier.tpe, symbol)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implicit function type will break the scala 2.12 build

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which is only tested after the merge

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have it in the code above?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then it fine


def Select_unique(qualifier: Term, name: String)(implicit ctx: Context): Select = {
val denot = qualifier.tpe.member(name.toTermName)
assert(!denot.isOverloaded, s"The symbol `$name` is overloaded. The method Select.unique can only be used for non-overloaded symbols.")
Expand Down
1 change: 1 addition & 0 deletions library/src/scala/tasty/reflect/Kernel.scala
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ trait Kernel {
def Select_name(self: Select)(implicit ctx: Context): String
def Select_signature(self: Select)(implicit ctx: Context): Option[Signature]

def Select_apply(qualifier: Term, symbol: Symbol)(implicit ctx: Context): Select
def Select_unique(qualifier: Term, name: String)(implicit ctx: Context): Select
// TODO rename, this returns an Apply and not a Select
def Select_overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term])(implicit ctx: Context): Apply
Expand Down
4 changes: 4 additions & 0 deletions library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,10 @@ trait TreeOps extends Core {

/** Scala term selection */
object Select {
/** Select a term member by symbol */
def apply(qualifier: Term, symbol: Symbol)(implicit ctx: Context): Select =
kernel.Select_apply(qualifier, symbol)

/** Select a field or a non-overloaded method by name
*
* @note The method will produce an assertion error if the selected
Expand Down
46 changes: 46 additions & 0 deletions tests/run-macros/reflect-select-symbol-constructor/assert_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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._

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 t @ Apply(sel @ Select(lhs, op), rhs :: Nil) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Apply(Select(left, sel.symbol), right :: Nil)
let(app) { result =>
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal.cast[Unit]
case Apply(f @ Apply(sel @ Select(Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Apply(Select(Apply(qual, left :: Nil), sel.symbol), right :: Nil)
let(Apply(app, implicits)) { result =>
val l = left.seal
val r = right.seal
val b = result.seal.cast[Boolean]
val code = '{ scala.Predef.assert($b) }
code.unseal
}
}
}.seal.cast[Unit]
}
}

}
29 changes: 29 additions & 0 deletions tests/run-macros/reflect-select-symbol-constructor/test_2.scala
Original file line number Diff line number Diff line change
@@ -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"))
}
}