summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEugene Burmako <xeno.by@gmail.com>2012-09-12 12:24:08 +0200
committerEugene Burmako <xeno.by@gmail.com>2012-09-14 13:29:48 +0200
commit524c90d09f2fb4687b312f2c7597393978d50b6a (patch)
tree1badec2cf5d0cf4ce3769d69af225005b7bc550e
parent0eabb63c06f4041077860875c9ea71216adf4faa (diff)
downloadscala-524c90d09f2fb4687b312f2c7597393978d50b6a.tar.gz
scala-524c90d09f2fb4687b312f2c7597393978d50b6a.tar.bz2
scala-524c90d09f2fb4687b312f2c7597393978d50b6a.zip
SI-6323 outlaws free types from TypeTag
Free types are no longer acceptable in normal type tags. Like type parameters or abstract type members they don't map on any real type, therefore I think this is a justified change. The main reason for doing is this is to prohibit people from using `typeOf` on local classes. Sure, the guard introduced in the previous commit will raise runtime errors about that, but this commit provides static checking. Those especially persistent might use `absTypeOf` and then try to play around with the weak type it returns, but that's advanced usage scenario, and I don't worry much about it. Bottom line: `typeOf` should just work. Things that work with additional effort should be explicitly marked as such.
-rw-r--r--src/compiler/scala/reflect/reify/Errors.scala4
-rw-r--r--src/compiler/scala/reflect/reify/States.scala8
-rw-r--r--src/compiler/scala/reflect/reify/codegen/GenSymbols.scala1
-rw-r--r--src/compiler/scala/reflect/reify/utils/SymbolTables.scala1
-rw-r--r--test/files/neg/t6323a.check9
-rw-r--r--test/files/neg/t6323a.flags1
-rw-r--r--test/files/neg/t6323a.scala (renamed from test/files/run/t6323.scala)2
-rw-r--r--test/files/run/t6323b.check (renamed from test/files/run/t6323.check)0
-rw-r--r--test/files/run/t6323b.scala21
9 files changed, 41 insertions, 6 deletions
diff --git a/src/compiler/scala/reflect/reify/Errors.scala b/src/compiler/scala/reflect/reify/Errors.scala
index 5e15c5ad3a..73c13901b6 100644
--- a/src/compiler/scala/reflect/reify/Errors.scala
+++ b/src/compiler/scala/reflect/reify/Errors.scala
@@ -27,8 +27,8 @@ trait Errors {
throw new ReificationError(defaultErrorPosition, msg)
}
- def CannotReifyTypeTagHavingUnresolvedTypeParameters(tpe: Type) = {
- val msg = "cannot reify TypeTag having unresolved type parameter %s".format(tpe)
+ def CannotReifyWeakType(details: Any) = {
+ val msg = "cannot create a TypeTag" + details
throw new ReificationError(defaultErrorPosition, msg)
}
diff --git a/src/compiler/scala/reflect/reify/States.scala b/src/compiler/scala/reflect/reify/States.scala
index a01cfe5d74..58455c9f3c 100644
--- a/src/compiler/scala/reflect/reify/States.scala
+++ b/src/compiler/scala/reflect/reify/States.scala
@@ -34,9 +34,11 @@ trait States {
def reificationIsConcrete_=(value: Boolean): Unit = {
_reificationIsConcrete = value
if (!value && concrete) {
- assert(current.isInstanceOf[Type], current)
- val offender = current.asInstanceOf[Type]
- CannotReifyTypeTagHavingUnresolvedTypeParameters(offender)
+ current match {
+ case tpe: Type => CannotReifyWeakType(s" having unresolved type parameter $tpe")
+ case sym: Symbol => CannotReifyWeakType(s" referring to local ${sym.kindString} ${sym.fullName}")
+ case _ => CannotReifyWeakType("")
+ }
}
}
var reifyStack = reifee :: Nil
diff --git a/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala b/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala
index be138c7bfa..c4b674955a 100644
--- a/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala
+++ b/src/compiler/scala/reflect/reify/codegen/GenSymbols.scala
@@ -112,6 +112,7 @@ trait GenSymbols {
def reifyFreeType(binding: Tree): Tree =
reifyIntoSymtab(binding.symbol) { sym =>
if (reifyDebug) println("Free type: %s (%s)".format(sym, sym.accurateKindString))
+ state.reificationIsConcrete = false
val name = newTermName(nme.REIFY_FREE_PREFIX + sym.name)
Reification(name, binding, mirrorBuildCall(nme.newFreeType, reify(sym.name.toString), mirrorBuildCall(nme.flagsFromBits, reify(sym.flags)), reify(origin(sym))))
}
diff --git a/src/compiler/scala/reflect/reify/utils/SymbolTables.scala b/src/compiler/scala/reflect/reify/utils/SymbolTables.scala
index 30ec6e8d52..2e17558f54 100644
--- a/src/compiler/scala/reflect/reify/utils/SymbolTables.scala
+++ b/src/compiler/scala/reflect/reify/utils/SymbolTables.scala
@@ -17,6 +17,7 @@ trait SymbolTables {
private[SymbolTable] val original: Option[List[Tree]] = None) {
def syms: List[Symbol] = symtab.keys.toList
+ def isConcrete: Boolean = symtab.values forall (sym => !FreeTypeDef.unapply(sym).isDefined)
// def aliases: Map[Symbol, List[TermName]] = aliases.distinct groupBy (_._1) mapValues (_ map (_._2))
diff --git a/test/files/neg/t6323a.check b/test/files/neg/t6323a.check
new file mode 100644
index 0000000000..694c79ca5f
--- /dev/null
+++ b/test/files/neg/t6323a.check
@@ -0,0 +1,9 @@
+t6323a.scala:11: `package`.this.materializeTypeTag[Test](scala.reflect.runtime.`package`.universe) is not a valid implicit value for reflect.runtime.universe.TypeTag[Test] because:
+failed to typecheck the materialized tag:
+cannot create a TypeTag referring to local class Test.Test
+ val value = u.typeOf[Test]
+ ^
+t6323a.scala:11: error: No TypeTag available for Test
+ val value = u.typeOf[Test]
+ ^
+one error found
diff --git a/test/files/neg/t6323a.flags b/test/files/neg/t6323a.flags
new file mode 100644
index 0000000000..4c6cdb71e2
--- /dev/null
+++ b/test/files/neg/t6323a.flags
@@ -0,0 +1 @@
+-Xlog-implicits \ No newline at end of file
diff --git a/test/files/run/t6323.scala b/test/files/neg/t6323a.scala
index 625cfaae20..a203167f3c 100644
--- a/test/files/run/t6323.scala
+++ b/test/files/neg/t6323a.scala
@@ -15,7 +15,7 @@ object Test extends App {
val thisShouldBeA = aAccessor.apply()
println(thisShouldBeA)
} catch {
- case ScalaReflectionException(msg) => println(msg)
+ case ScalaReflectionException(msg) => println(msg)
}
}
} \ No newline at end of file
diff --git a/test/files/run/t6323.check b/test/files/run/t6323b.check
index 2219278a16..2219278a16 100644
--- a/test/files/run/t6323.check
+++ b/test/files/run/t6323b.check
diff --git a/test/files/run/t6323b.scala b/test/files/run/t6323b.scala
new file mode 100644
index 0000000000..25a2329d94
--- /dev/null
+++ b/test/files/run/t6323b.scala
@@ -0,0 +1,21 @@
+import scala.reflect.runtime.universe._
+import scala.reflect.runtime.{currentMirror => m}
+import scala.reflect.runtime.{universe => u}
+
+object Test extends App {
+ locally {
+ try {
+ case class Test(a:String,b:List[Int])
+
+ val lookAtMe = m.reflect(Test("a",List(5)))
+ val value = u.absTypeOf[Test]
+ val members = value.members
+ val member = value.members.filter(_.name.encoded == "a")
+ val aAccessor = lookAtMe.reflectMethod(member.head.asMethod)
+ val thisShouldBeA = aAccessor.apply()
+ println(thisShouldBeA)
+ } catch {
+ case ScalaReflectionException(msg) => println(msg)
+ }
+ }
+} \ No newline at end of file