Skip to content

Commit 0821625

Browse files
oderskyfelixmulder
authored andcommitted
Fix 2.12 self types in mixin translation
2.12 seems to always assume the enclsing mixin class as the type of the "$this" self parameter of a mixin implementation method whereas 2.11 used the self type of this class instead. This commit compensates or the difference.
1 parent cd7250c commit 0821625

File tree

3 files changed

+15
-6
lines changed

3 files changed

+15
-6
lines changed

compiler/src/dotty/tools/dotc/transform/LinkScala2Impls.scala

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,18 @@ class LinkScala2Impls extends MiniPhase with IdentityDenotTransformer { thisTran
5050

5151
/** Copy definitions from implementation class to trait itself */
5252
private def augmentScala_2_12_Trait(mixin: ClassSymbol)(implicit ctx: Context): Unit = {
53+
def info_2_12(sym: Symbol) = sym.info match {
54+
case mt @ MethodType(paramNames @ nme.SELF :: _) =>
55+
// 2.12 seems to always assume the enclsing mixin class as self type parameter,
56+
// whereas 2.11 used the self type of this class instead.
57+
val selfType :: otherParamTypes = mt.paramInfos
58+
MethodType(paramNames, mixin.typeRef :: otherParamTypes, mt.resType)
59+
case info => info
60+
}
5361
def newImpl(sym: TermSymbol): Symbol = sym.copy(
5462
owner = mixin,
55-
name = if (sym.isConstructor) sym.name else ImplMethName(sym.name)
63+
name = if (sym.isConstructor) sym.name else ImplMethName(sym.name),
64+
info = info_2_12(sym)
5665
)
5766
for (sym <- mixin.implClass.info.decls)
5867
newImpl(sym.asTerm).enteredAfter(thisTransform)

tests/run/mixins1/A_1.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
trait A {
2-
1+
trait A[This <: A[This] with B] { self: B =>
32
var x = 3
43
println("hi")
54
val y = x * x
65

76
def f: Int = x + y
87

98
def f(z: Int): Int = f + z
10-
119
}
10+
11+
trait B extends A[B]

tests/run/mixins1/C_2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Intended to be compiled with either 2.11 or 2.12
2-
class C extends A {
2+
class C extends A with B {
33
x = 4
44

55
override def f: Int = super.f
@@ -8,5 +8,5 @@ class C extends A {
88
}
99

1010
object Test extends App {
11-
new C().f
11+
println(new C().f)
1212
}

0 commit comments

Comments
 (0)