Skip to content

Commit 05a7ae3

Browse files
committed
Fix #4342: When calling a Scala 2.x method with an outer parameter, adapt to the old behavior in picking the outer parameter's type, i.e., prefer the enclosing class's self type, rather than the enclosing class itself.
1 parent 3821b16 commit 05a7ae3

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,12 +336,26 @@ object ExplicitOuter {
336336
class OuterOps(val ictx: Context) extends AnyVal {
337337
private implicit def ctx: Context = ictx
338338

339+
/** Scala 2.x and Dotty don't always agree on what should be the type of the outer parameter,
340+
* so we replicate the old behavior when passing arguments to methods coming from Scala 2.x.
341+
*/
342+
private def outerType(cls: ClassSymbol): Type = {
343+
val encl = cls.owner.enclosingClass
344+
if (cls.is(Scala2x))
345+
encl.asClass.classInfo.selfInfo match {
346+
case tp: TypeRef => tp.classSymbol.typeRef
347+
case self: Symbol => self.typeRef
348+
case _ => encl.typeRef
349+
}
350+
else encl.typeRef
351+
}
352+
339353
/** If `cls` has an outer parameter add one to the method type `tp`. */
340354
def addParam(cls: ClassSymbol, tp: Type): Type =
341355
if (hasOuterParam(cls)) {
342356
val mt @ MethodTpe(pnames, ptypes, restpe) = tp
343357
mt.derivedLambdaType(
344-
nme.OUTER :: pnames, cls.owner.enclosingClass.typeRef :: ptypes, restpe)
358+
nme.OUTER :: pnames, outerType(cls) :: ptypes, restpe)
345359
} else tp
346360

347361
/** If function in an apply node is a constructor that needs to be passed an

0 commit comments

Comments
 (0)