Skip to content

Commit

Permalink
Merge pull request #395 from sjrd/fix-object-self-type
Browse files Browse the repository at this point in the history
Work around scala/scala3#19019: patch the SelfDef of inner module classes.
  • Loading branch information
sjrd authored Nov 23, 2023
2 parents 2c65b9e + aa96c6e commit 68a0413
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,17 @@ private[tasties] class TreeUnpickler private (
case _ => readTypeTree
}
}
val self = readSelf

val self0 = readSelf
val self = self0 match
case Some(orig @ SelfDef(name, tpt)) if cls.name.isObjectClassTypeName && cls.owner.isClass =>
// Work around https://github.com/lampepfl/dotty/issues/19019: replace with a correct SELFDEF
val owner = cls.owner.asClass
val fixedTpt = TypeWrapper(TermRef(owner.thisType, cls.name.sourceObjectName))(orig.pos)
Some(SelfDef(name, fixedTpt)(orig.pos))
case _ =>
self0

cls.withGivenSelfType(self.map(_.tpt.toType))
// The first entry is the constructor
val cstr = readStat.asInstanceOf[DefDef]
Expand Down
43 changes: 43 additions & 0 deletions test-sources/src/main/scala/simple_trees/ObjectWithSelf.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package simple_trees

object ObjectWithSelf:
object StaticObjectNoSelf:
def foo: Any = this
def bar: Any = this.foo
end StaticObjectNoSelf

object StaticObjectWithSelf:
self =>

def foo: Any = self
def bar: Any = self.foo
end StaticObjectWithSelf

class Container:
object NonStaticObjectNoSelf:
def foo: Any = this
def bar: Any = this.foo
end NonStaticObjectNoSelf

object NonStaticObjectWithSelf:
self =>

def foo: Any = self
def bar: Any = self.foo // used to cause StackOverflow while resolving this in WholeClasspathSuite tests
end NonStaticObjectWithSelf
end Container

def methodOwner(): Unit =
object LocalObjectNoSelf:
def foo: Any = this
def bar: Any = this.foo
end LocalObjectNoSelf

object LocalObjectWithSelf:
self =>

def foo: Any = self
def bar: Any = self.foo
end LocalObjectWithSelf
end methodOwner
end ObjectWithSelf

0 comments on commit 68a0413

Please sign in to comment.