summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-12-10 00:33:30 +0100
committerEugene Burmako <xeno.by@gmail.com>2014-02-14 14:09:14 +0100
commitb0176294060c9ce8b86d71e7bc8a7a13cef15b1e (patch)
treefb68701eda408edfd8d347d22ca97f36eb70df45 /test
parent51b16e421ddd4e6c7e90ba945addf39ffcb4fa41 (diff)
downloadscala-b0176294060c9ce8b86d71e7bc8a7a13cef15b1e.tar.gz
scala-b0176294060c9ce8b86d71e7bc8a7a13cef15b1e.tar.bz2
scala-b0176294060c9ce8b86d71e7bc8a7a13cef15b1e.zip
SI-6484 adds Universe.typeOf[T](x: T)
This is a nice addition to Universe.typeOf[T], quite frequently useful for inspecting ad-hoc types. As promised by https://github.com/scala/scala/pull/1741, which represented my previous stab at adding this API, I ran into problems when trying to introduce the API naively. def typeOf[T](implicit ttag: TypeTag[T]): Type def typeOf[T: TypeTag](x: T): Type The first problem came from the fact that even though they don't look ambiguous, under certain circumstances, the couple of typeOf overloads can become ambiguous. Concretely, ambiguity happens when T <: TypeTag[T], which makes the compiler uncapable to choose an overload to typecheck `typeOf[T](<materialized>: TypeTag[T])`. Luckily, defining x as a by-name parameter fixes the problem. def typeOf[T](implicit ttag: TypeTag[T]): Type def typeOf[T: TypeTag](x: => T): Type The second problem manifested itself in reification of snippets that contained calls to typeOf. Apparently, materialized tags get rolled back by reify as products of macro expansion, becoming replaced with `implicitly[TypeTag[T]]`. Afterwards, subsequent rollback of TypeTree's strips the replacement of its type argument, producing bare `implicitly`. Back then when typeOf wasn't overloaded, this abomination typechecked and worked correctly, but now, due to some weird reason, it stopped. I have worked around this by performing the rollback on a larger scale - instead of hunting down materialized tags, this commit detects calls to typeOf and removes their implicit argument lists altogether.
Diffstat (limited to 'test')
-rw-r--r--test/files/run/reify_typeof.check10
-rw-r--r--test/files/run/reify_typeof.scala14
-rw-r--r--test/files/run/typetags_typeof_x.check8
-rw-r--r--test/files/run/typetags_typeof_x.scala14
4 files changed, 46 insertions, 0 deletions
diff --git a/test/files/run/reify_typeof.check b/test/files/run/reify_typeof.check
new file mode 100644
index 0000000000..670f76faa4
--- /dev/null
+++ b/test/files/run/reify_typeof.check
@@ -0,0 +1,10 @@
+Expr[Unit]({
+ val ru = `package`.universe;
+ val tpe1: ru.Type = ru.typeOf[`package`.List[Int]];
+ Predef.println(tpe1);
+ val tpe2: ru.Type = ru.typeOf(List.apply(1, 2, 3));
+ Predef.println(tpe2)
+})
+scala.List[Int]
+List[Int]
+()
diff --git a/test/files/run/reify_typeof.scala b/test/files/run/reify_typeof.scala
new file mode 100644
index 0000000000..985c57b9ab
--- /dev/null
+++ b/test/files/run/reify_typeof.scala
@@ -0,0 +1,14 @@
+import scala.reflect.runtime.universe._
+import scala.tools.reflect.Eval
+
+object Test extends App {
+ val reified = reify {
+ val ru = scala.reflect.runtime.universe
+ val tpe1: ru.Type = ru.typeOf[List[Int]]
+ println(tpe1)
+ val tpe2: ru.Type = ru.typeOf(List(1, 2, 3))
+ println(tpe2)
+ }
+ println(reified)
+ println(reified.eval)
+} \ No newline at end of file
diff --git a/test/files/run/typetags_typeof_x.check b/test/files/run/typetags_typeof_x.check
new file mode 100644
index 0000000000..832a8bc63c
--- /dev/null
+++ b/test/files/run/typetags_typeof_x.check
@@ -0,0 +1,8 @@
+List[T]
+C
+Int
+List[Any]
+AnyRef{def x: Int}
+Null
+Nothing
+Null
diff --git a/test/files/run/typetags_typeof_x.scala b/test/files/run/typetags_typeof_x.scala
new file mode 100644
index 0000000000..08be6d4527
--- /dev/null
+++ b/test/files/run/typetags_typeof_x.scala
@@ -0,0 +1,14 @@
+import scala.reflect.runtime.universe._
+
+object Test extends App {
+ def foo[T](x: T) = weakTypeOf(List(x))
+ println(foo(2))
+ locally { class C; println(weakTypeOf(new C)) }
+
+ println(typeOf(2))
+ println(typeOf(List(1, "1")))
+ println(typeOf(new { def x = 2 }))
+ println(typeOf[Null])
+ println(typeOf[Nothing])
+ println(typeOf(null))
+} \ No newline at end of file