diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2015-06-07 12:51:51 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2015-08-11 14:28:05 +1000 |
commit | 658b62a4425ff7b774cd95e3323d9627b5af3c72 (patch) | |
tree | 63cc1a49a574b4f2489171bd0c4e55a169e05b18 /test/junit | |
parent | 2bde3928833ae194fc7e2094b8955112b70fd31f (diff) | |
download | scala-658b62a4425ff7b774cd95e3323d9627b5af3c72.tar.gz scala-658b62a4425ff7b774cd95e3323d9627b5af3c72.tar.bz2 scala-658b62a4425ff7b774cd95e3323d9627b5af3c72.zip |
Allow BCode to emit default interface methods
A DefDef owned by a trait may now have have a method body.
Such a method must be emitted without ACC_ABSTRACT, and
with the code attribute.
Tested by intercepting the compile pipeline and adding the
DEFAULTMETHOD flag and a method body before running
the backend.
Diffstat (limited to 'test/junit')
-rw-r--r-- | test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala | 17 | ||||
-rw-r--r-- | test/junit/scala/tools/nsc/backend/jvm/DefaultMethodTest.scala | 43 |
2 files changed, 60 insertions, 0 deletions
diff --git a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala index ee9580c1c3..769236ae49 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala @@ -93,6 +93,23 @@ object CodeGenTools { getGeneratedClassfiles(compiler.settings.outputDirs.getSingleOutput.get) } + def compileTransformed(compiler: Global)(scalaCode: String, javaCode: List[(String, String)] = Nil, beforeBackend: compiler.Tree => compiler.Tree): List[(String, Array[Byte])] = { + compiler.settings.stopBefore.value = "jvm" :: Nil + val run = newRun(compiler) + import compiler._ + val scalaUnit = newCompilationUnit(scalaCode, "unitTestSource.scala") + val javaUnits = javaCode.map(p => newCompilationUnit(p._1, p._2)) + val units = scalaUnit :: javaUnits + run.compileUnits(units, run.parserPhase) + compiler.settings.stopBefore.value = Nil + scalaUnit.body = beforeBackend(scalaUnit.body) + checkReport(compiler, _ => false) + val run1 = newRun(compiler) + run1.compileUnits(units, run1.phaseNamed("jvm")) + checkReport(compiler, _ => false) + getGeneratedClassfiles(compiler.settings.outputDirs.getSingleOutput.get) + } + /** * Compile multiple Scala files separately into a single output directory. * diff --git a/test/junit/scala/tools/nsc/backend/jvm/DefaultMethodTest.scala b/test/junit/scala/tools/nsc/backend/jvm/DefaultMethodTest.scala new file mode 100644 index 0000000000..f9a55bb26e --- /dev/null +++ b/test/junit/scala/tools/nsc/backend/jvm/DefaultMethodTest.scala @@ -0,0 +1,43 @@ +package scala.tools.nsc.backend.jvm + +import org.junit.Assert._ +import org.junit.Test + +import scala.collection.JavaConverters +import scala.tools.asm.Opcodes +import scala.tools.asm.tree.ClassNode +import scala.tools.nsc.backend.jvm.CodeGenTools._ +import JavaConverters._ +import scala.tools.testing.ClearAfterClass + +object DefaultMethodTest extends ClearAfterClass.Clearable { + var compiler = newCompiler(extraArgs = "-Ybackend:GenBCode") + def clear(): Unit = { compiler = null } +} + +class DefaultMethodTest extends ClearAfterClass { + ClearAfterClass.stateToClear = DirectCompileTest + val compiler = DirectCompileTest.compiler + + @Test + def defaultMethodsViaGenBCode(): Unit = { + import compiler._ + val code = "package pack { trait T { def foo: Int }}" + object makeFooDefaultMethod extends Transformer { + val Foo = TermName("foo") + /** Transforms a single tree. */ + override def transform(tree: compiler.Tree): compiler.Tree = tree match { + case dd @ DefDef(_, Foo, _, _, _, _) => + dd.symbol.setFlag(reflect.internal.Flags.JAVA_DEFAULTMETHOD) + copyDefDef(dd)(rhs = Literal(Constant(1)).setType(definitions.IntTpe)) + case _ => super.transform(tree) + } + } + val asmClasses: List[ClassNode] = readAsmClasses(compileTransformed(compiler)(code, Nil, makeFooDefaultMethod.transform(_))) + val foo = asmClasses.head.methods.iterator.asScala.toList.last + assertTrue("default method should not be abstract", (foo.access & Opcodes.ACC_ABSTRACT) == 0) + assertTrue("default method body emitted", foo.instructions.size() > 0) + } + + +} |