Skip to content

Add constructors for Select #5796

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 5 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/tastyreflect/TreeOpsImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ trait TreeOpsImpl extends scala.tasty.reflect.TreeOps with RootPositionImpl with


object Select extends SelectModule {
def 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.")
withDefaultPos(implicit ctx => tpd.Select(qualifier, name.toTermName))
}

def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term])(implicit ctx: Context): Apply =
withDefaultPos(implicit ctx => tpd.applyOverloaded(qualifier, name.toTermName, args, targs, Types.WildcardType).asInstanceOf[Apply])

def copy(original: Tree)(qualifier: Term, name: String)(implicit ctx: Context): Select =
tpd.cpy.Select(original)(qualifier, name.toTermName)

Expand Down
21 changes: 9 additions & 12 deletions library/src-bootstrapped/scala/tasty/reflect/utils/TreeUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,17 @@ trait TreeUtils {
import reflect._

/** Bind the `rhs` to a `val` and use it in `body` */
def let(rhs: Term)(bodyType: Type)(body: Term.Ident => Term): Term = {
// Recover all lost type information
def let(rhs: Term)(body: Term.Ident => Term): Term = {
type T // TODO probably it is better to use the Sealed contruct rather than let the user create their own existential type
type U // TODO probably it is better to use the Sealed contruct rather than let the user create their own existential type
implicit val bodyTpe: quoted.Type[U] = bodyType.seal.asInstanceOf[quoted.Type[U]]
implicit val rhsTpe: quoted.Type[T] = rhs.tpe.seal.asInstanceOf[quoted.Type[T]]
val rhsExpr = rhs.seal[T]
let[T, U](rhsExpr)(x => body(x.unseal.asInstanceOf[Term.Ident]).seal[U]).unseal
val expr = '{
val x = ~rhsExpr
~{
val id = ('(x)).unseal.asInstanceOf[Term.Ident]
body(id).seal[Any]
}
}
expr.unseal
}

/** */
private def let[T: quoted.Type, U: quoted.Type](rhs: Expr[T])(in: Expr[T] => Expr[U]): Expr[U] = '{
val x = ~rhs
~in('(x))
}

}
3 changes: 2 additions & 1 deletion library/src/scala/tasty/reflect/TreeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,9 @@ trait TreeOps extends Core {
/** Scala term selection */
val Select: SelectModule
abstract class SelectModule {
def unique(qualifier: Term, name: String)(implicit ctx: Context): Select

// TODO def apply(qualifier: Term, name: String, signature: Option[Signature])(implicit ctx: Context): Select
def overloaded(qualifier: Term, name: String, targs: List[Type], args: List[Term])(implicit ctx: Context): Apply

def copy(original: Tree)(qualifier: Term, name: String)(implicit ctx: Context): Select

Expand Down
47 changes: 47 additions & 0 deletions tests/run-with-compiler/reflect-select-constructor/assert_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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 t @ Term.Apply(Term.Select(lhs, op), rhs :: Nil) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Term.Select.overloaded(left, op, Nil, right :: Nil)
let(app) { 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.Select(Term.Apply(qual, lhs :: Nil), op), rhs :: Nil), implicits)
if isImplicitMethodType(f.tpe) =>
let(lhs) { left =>
let(rhs) { right =>
val app = Term.Select.overloaded(Term.Apply(qual, left :: Nil), op, Nil, right :: Nil)
let(Term.Apply(app, 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]
}
}

}
29 changes: 29 additions & 0 deletions tests/run-with-compiler/reflect-select-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"))
}
}
12 changes: 6 additions & 6 deletions tests/run-with-compiler/reflect-select-copy/assert_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ object scalatest {

cond.unseal.underlyingArgument match {
case Term.Apply(sel @ Term.Select(lhs, op), rhs :: Nil) =>
let(lhs)(definitions.UnitType) { left =>
let(rhs)(definitions.UnitType) { right =>
let(Term.Apply(Term.Select.copy(sel)(left, op), right :: Nil))(definitions.UnitType) { result =>
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]
Expand All @@ -28,9 +28,9 @@ object scalatest {
}.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)(definitions.UnitType) { left =>
let(rhs)(definitions.UnitType) { right =>
let(Term.Apply(Term.Apply(Term.Select.copy(sel)(Term.Apply(qual, left :: Nil), op), right :: Nil), implicits))(definitions.UnitType) { result =>
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]
Expand Down
2 changes: 1 addition & 1 deletion tests/run-with-compiler/tasty-unsafe-let/quoted_1.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object Macros {
val rhsTerm = rhs.unseal

import reflect.util.{let => letTerm}
letTerm(rhsTerm)(('[Unit]).unseal.tpe) { rhsId =>
letTerm(rhsTerm) { rhsId =>
body(rhsId.seal[Any].asInstanceOf[Expr[T]]).unseal // Dangerous uncheked cast!
}.seal[Unit]
}
Expand Down