diff --git a/library/src/scala/tasty/Reflection.scala b/library/src/scala/tasty/Reflection.scala index e955e0d6f803..c6957a291c58 100644 --- a/library/src/scala/tasty/Reflection.scala +++ b/library/src/scala/tasty/Reflection.scala @@ -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 */ diff --git a/library/src/scala/tasty/reflect/TypeOrBoundsOps.scala b/library/src/scala/tasty/reflect/TypeOrBoundsOps.scala index 5e1a320b4f86..a8e97b7eba02 100644 --- a/library/src/scala/tasty/reflect/TypeOrBoundsOps.scala +++ b/library/src/scala/tasty/reflect/TypeOrBoundsOps.scala @@ -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 diff --git a/tests/run-separate-compilation/tasty-typeof.check b/tests/run-separate-compilation/tasty-typeof.check new file mode 100644 index 000000000000..86fa6752a39e --- /dev/null +++ b/tests/run-separate-compilation/tasty-typeof.check @@ -0,0 +1,3 @@ +collection.immutable.List[scala.Int] +Macros +Macros diff --git a/tests/run-separate-compilation/tasty-typeof/Macro_1.scala b/tests/run-separate-compilation/tasty-typeof/Macro_1.scala new file mode 100644 index 000000000000..90c716fd6982 --- /dev/null +++ b/tests/run-separate-compilation/tasty-typeof/Macro_1.scala @@ -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 diff --git a/tests/run-separate-compilation/tasty-typeof/Test_2.scala b/tests/run-separate-compilation/tasty-typeof/Test_2.scala new file mode 100644 index 000000000000..8f9ccb7117bc --- /dev/null +++ b/tests/run-separate-compilation/tasty-typeof/Test_2.scala @@ -0,0 +1,8 @@ + +object Test { + + def main(args: Array[String]): Unit = { + Macros.testTypeOf() + } + +}