From c8e6050c3c190dd064642b6b77fc179f27b0495d Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Wed, 16 Mar 2016 16:14:27 +1000 Subject: New trait encoding: use default methods, jettison impl classes Until now, concrete methods in traits were encoded with "trait implementation classes". - Such a trait would compile to two class files - the trait interface, a Java interface, and - the implementation class, containing "trait implementation methods" - trait implementation methods are static methods has an explicit self parameter. - some methods don't require addition of an interface method, such as private methods. Calls to these directly call the implementation method - classes that mixin a trait install "trait forwarders", which implement the abstract method in the interface by forwarding to the trait implementation method. The new encoding: - no longer emits trait implementation classes or trait implementation methods. - instead, concrete methods are simply retained in the interface, as JVM 8 default interface methods (the JVM spec changes in [JSR-335](http://download.oracle.com/otndocs/jcp/lambda-0_9_3-fr-eval-spec/index.html) pave the way) - use `invokespecial` to call private or particular super implementations of a method (rather `invokestatic`) - in cases when we `invokespecial` to a method in an indirect ancestor, we add that ancestor redundantly as a direct parent. We are investigating alternatives approaches here. - we still emit trait fowrarders, although we are [investigating](https://github.com/scala/scala-dev/issues/98) ways to only do this when the JVM would be unable to resolve the correct method using its rules for default method resolution. Here's an example: ``` trait T { println("T") def m1 = m2 private def m2 = "m2" } trait U extends T { println("T") override def m1 = super[T].m1 } class C extends U { println("C") def test = m1 } ``` The old and new encodings are displayed and diffed here: https://gist.github.com/retronym/f174d23f859f0e053580 Some notes in the implementation: - No need to filter members from class decls at all in AddInterfaces (although we do have to trigger side effecting info transformers) - We can now emit an EnclosingMethod attribute for classes nested in private trait methods - Created a factory method for an AST shape that is used in a number of places to symbolically bind to a particular super method without needed to specify the qualifier of the `Super` tree (which is too limiting, as it only allows you to refer to direct parents.) - I also found a similar tree shape created in Delambdafy, that is better expressed with an existing tree creation factory method, mkSuperInit. --- test/files/pos/t3234.scala | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'test/files/pos') diff --git a/test/files/pos/t3234.scala b/test/files/pos/t3234.scala index c3b7366db8..8c588e5aa9 100644 --- a/test/files/pos/t3234.scala +++ b/test/files/pos/t3234.scala @@ -1,17 +1,17 @@ trait Trait1 { - @inline def foo2(n: Int) = n*n + @inline final def foo2(n: Int) = n*n } trait Trait2 { - @inline def foo3(n: Int) = 1 + @inline final def foo3(n: Int) = 1 } class Base extends Trait1 { - @inline def foo(n: Int) = n + @inline final def foo(n: Int) = n } object Test extends Base with Trait2 { def main(args: Array[String]) = { println(foo(42) + foo2(11) + foo3(2)) } -} \ No newline at end of file +} -- cgit v1.2.3