diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-08-06 15:51:50 +0200 |
---|---|---|
committer | Eugene Burmako <xeno.by@gmail.com> | 2013-10-18 17:44:38 +0200 |
commit | 48c28452fcc30838503388e8e7215a1411a826ba (patch) | |
tree | c4fdadfcaace30df524ede41927c3fe8dcc94181 /test/files/run | |
parent | a030784ae0dc53a0cdc65d42ca45fa48943eea0d (diff) | |
download | scala-48c28452fcc30838503388e8e7215a1411a826ba.tar.gz scala-48c28452fcc30838503388e8e7215a1411a826ba.tar.bz2 scala-48c28452fcc30838503388e8e7215a1411a826ba.zip |
eagerly initializes lazy vals and objects in runtime reflection
The code to do so is curated with the help of a generator. Because this
needs to inspect code post-typer, the code generator is run during partest
as a code-validator. We could concievably do the same with a macro, but
this approach might be a better starting point which macros continue to
stabilize.
Removes Definitions.AnyRefModule and an already deprecated alias, as
these have been throwing exceptions for more than a year since 6bb5975289.
They used to be used by AnyRef specialization.
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/t6240-universe-code-gen.scala | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/test/files/run/t6240-universe-code-gen.scala b/test/files/run/t6240-universe-code-gen.scala new file mode 100644 index 0000000000..84691639bd --- /dev/null +++ b/test/files/run/t6240-universe-code-gen.scala @@ -0,0 +1,82 @@ +import scala.tools.partest.nest.FileManager._ + +object Test extends App { + val cm = reflect.runtime.currentMirror + val u = cm.universe + import u._ + + val JavaUniverseTpe = typeOf[reflect.runtime.JavaUniverse] + val DefinitionsModule = JavaUniverseTpe.member(TermName("definitions")) + + def forceCode(prefix: String, tp: Type): String = { + def isLazyAccessorOrObject(sym: Symbol) = ( + (sym.isMethod && sym.asMethod.isLazy) + || sym.isModule + ) + val forcables = tp.members.sorted.filter(isLazyAccessorOrObject) + forcables.map { + sym => + val path = s"$prefix.${sym.name}" + " " + ( + if (sym.isPrivate || sym.isProtected) s"// inaccessible: $path" + else path + ) + }.mkString("\n") + } + + val code = + s"""|// Generated Code, validated by run/t6240-universe-code-gen.scala + |package scala.reflect + |package runtime + | + |trait JavaUniverseForce { self: runtime.JavaUniverse => + | def force() { + | Literal(Constant(42)).duplicate + | nme.flattenedName() + | nme.raw + | WeakTypeTag + | TypeTag + | TypeTag.Byte.tpe + | TypeTag.Short.tpe + | TypeTag.Char.tpe + | TypeTag.Int.tpe + | TypeTag.Long.tpe + | TypeTag.Float.tpe + | TypeTag.Double.tpe + | TypeTag.Boolean.tpe + | TypeTag.Unit.tpe + | TypeTag.Any.tpe + | TypeTag.AnyVal.tpe + | TypeTag.AnyRef.tpe + | TypeTag.Object.tpe + | TypeTag.Nothing.tpe + | TypeTag.Null.tpe + | + |${forceCode("this", JavaUniverseTpe)} + |${forceCode("definitions", DefinitionsModule.typeSignature)} + |${forceCode("refChecks", typeOf[scala.reflect.internal.transform.RefChecks])} + |${forceCode("uncurry", typeOf[scala.reflect.internal.transform.UnCurry])} + |${forceCode("erasure", typeOf[scala.reflect.internal.transform.Erasure])} + | } + |}""".stripMargin + + import java.io.File + val testFile = new File(sys.props("partest.test-path")) + val actualFile = new java.io.File(testFile.getParent + "/../../../src/reflect/scala/reflect/runtime/JavaUniverseForce.scala").getCanonicalFile + val actual = scala.io.Source.fromFile(actualFile) + val actualLines = actual.getLines.toList + val generatedLines = code.lines.toList + if (actualLines != generatedLines) { + val msg = s"""|${actualFile} must be updated. + |=========================================================== + | DIFF: + |=========================================================== + |${compareContents(actualLines, generatedLines)} + |=========================================================== + | NEW CONTENTS: + |=========================================================== + |${code}""".stripMargin + + assert(false, msg) + } +} |