Skip to content

Commit e665162

Browse files
committed
Add defaults to all trait interface methods.
Currently, these forward to the impl method in the impl class. Later (perhaps after bootstrapping dependencies like partest) we ought do away with the impl classes.
1 parent 17e27f8 commit e665162

File tree

4 files changed

+24
-0
lines changed

4 files changed

+24
-0
lines changed

src/compiler/scala/tools/nsc/transform/Mixin.scala

+14
Original file line numberDiff line numberDiff line change
@@ -1202,6 +1202,20 @@ abstract class Mixin extends InfoTransform with ast.TreeDSL {
12021202
} else {
12031203
tree
12041204
}
1205+
case dd @ DefDef(_, _, _, vparamss, _, EmptyTree) if sym.owner.isTrait =>
1206+
val isDeferred = enteringPhase(currentRun.erasurePhase.prev)(sym.isDeferred) // before lateDEFERRED
1207+
// TODO Duplicated with isImplementedStatically, at this point sym.owner is still the trait so the other method returns false.
1208+
val isImplementedStatically = sym.isMethod && !sym.isModule && !sym.hasFlag(ACCESSOR | SUPERACCESSOR)
1209+
if (!isDeferred && isImplementedStatically) {
1210+
val implSym = exitingMixin(implClass(sym.owner).info.member(sym.name))
1211+
sym.setFlag(Flags.DEFAULTMETHOD)
1212+
val tree = Apply(staticRef(implSym), gen.mkAttributedCast(gen.mkAttributedThis(sym.owner), sym.owner.typeOfThis) :: vparamss.head.map(t => gen.mkAttributedRef(t.symbol)))
1213+
val app = exitingMixin(typedPos(tree.pos)(tree))
1214+
copyDefDef(dd)(rhs = app)
1215+
} else {
1216+
tree
1217+
}
1218+
12051219
case Apply(Select(qual, _), args) =>
12061220
/* Changes `qual.m(args)` where m refers to an implementation
12071221
* class method to Q.m(S, args) where Q is the implementation module of
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
trait A {
2+
def a = "a"
3+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
public class B_2 implements A {
2+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
def main(args: Array[String]): Unit = {
3+
assert(new B_2().a == "a")
4+
}
5+
}

0 commit comments

Comments
 (0)