Skip to content

Add typeOf to TASTy reflection #5569

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
Dec 5, 2018
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
6 changes: 5 additions & 1 deletion library/src/scala/tasty/Reflection.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ abstract class Reflection
with TreeOps
with TreeUtils
with TypeOrBoundsTreeOps
with TypeOrBoundsOps
with TypeOrBoundsOps {

def typeOf[T: scala.quoted.Type]: Type =
implicitly[scala.quoted.Type[T]].unseal.tpe
}

object Reflection {
/** Compiler tasty context available in a top level ~ of an inline macro */
Expand Down
2 changes: 2 additions & 0 deletions library/src/scala/tasty/reflect/TypeOrBoundsOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ trait TypeOrBoundsOps extends Core {

// ----- Types ----------------------------------------------------

def typeOf[T: scala.quoted.Type]: Type

trait TypeAPI {
def =:=(other: Type)(implicit ctx: Context): Boolean
def <:<(other: Type)(implicit ctx: Context): Boolean
Expand Down
3 changes: 3 additions & 0 deletions tests/run-separate-compilation/tasty-typeof.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
collection.immutable.List[scala.Int]
Macros
Macros
35 changes: 35 additions & 0 deletions tests/run-separate-compilation/tasty-typeof/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import scala.quoted._
import scala.tasty._

object Macros {

inline def testTypeOf(): Unit = ~testTypeOfImpl

private def testTypeOfImpl(implicit reflect: Reflection): Expr[Unit] = {
import reflect._
'{
assert(~(typeOf[Unit] =:= definitions.UnitType).toExpr, "Unit")
assert(~(typeOf[Byte] =:= definitions.ByteType).toExpr, "Byte")
assert(~(typeOf[Short] =:= definitions.ShortType).toExpr, "Short")
assert(~(typeOf[Int] =:= definitions.IntType).toExpr, "Int")
assert(~(typeOf[Long] =:= definitions.LongType).toExpr, "Long")
assert(~(typeOf[Float] =:= definitions.FloatType).toExpr, "Float")
assert(~(typeOf[Double] =:= definitions.DoubleType).toExpr, "Double")
assert(~(typeOf[Char] =:= definitions.CharType).toExpr, "Char")
assert(~(typeOf[String] =:= definitions.StringType).toExpr, "String")

assert(~(typeOf[Any] =:= definitions.AnyType).toExpr, "Any")
assert(~(typeOf[AnyRef] =:= definitions.AnyRefType).toExpr, "AnyRef")
assert(~(typeOf[AnyVal] =:= definitions.AnyValType).toExpr, "AnyVal")
assert(~(typeOf[Object] =:= definitions.ObjectType).toExpr, "Object")
assert(~(typeOf[Nothing] =:= definitions.NothingType).toExpr, "Nothing")

println(~(typeOf[List[Int]].showCode.toExpr))
println(~(typeOf[Macros].showCode.toExpr))
println(~(typeOf[Macros.type].showCode.toExpr))
}
}

}

class Macros
8 changes: 8 additions & 0 deletions tests/run-separate-compilation/tasty-typeof/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

object Test {

def main(args: Array[String]): Unit = {
Macros.testTypeOf()
}

}