Skip to content

Commit aa96c6e

Browse files
committed
Work around scala/scala3#19019: patch the SelfDef of inner module classes.
Re-create the correct self type which is a term reference to the accompanying lazy val.
1 parent 2c65b9e commit aa96c6e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

tasty-query/shared/src/main/scala/tastyquery/reader/tasties/TreeUnpickler.scala

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,17 @@ private[tasties] class TreeUnpickler private (
685685
case _ => readTypeTree
686686
}
687687
}
688-
val self = readSelf
688+
689+
val self0 = readSelf
690+
val self = self0 match
691+
case Some(orig @ SelfDef(name, tpt)) if cls.name.isObjectClassTypeName && cls.owner.isClass =>
692+
// Work around https://github.com/lampepfl/dotty/issues/19019: replace with a correct SELFDEF
693+
val owner = cls.owner.asClass
694+
val fixedTpt = TypeWrapper(TermRef(owner.thisType, cls.name.sourceObjectName))(orig.pos)
695+
Some(SelfDef(name, fixedTpt)(orig.pos))
696+
case _ =>
697+
self0
698+
689699
cls.withGivenSelfType(self.map(_.tpt.toType))
690700
// The first entry is the constructor
691701
val cstr = readStat.asInstanceOf[DefDef]
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package simple_trees
2+
3+
object ObjectWithSelf:
4+
object StaticObjectNoSelf:
5+
def foo: Any = this
6+
def bar: Any = this.foo
7+
end StaticObjectNoSelf
8+
9+
object StaticObjectWithSelf:
10+
self =>
11+
12+
def foo: Any = self
13+
def bar: Any = self.foo
14+
end StaticObjectWithSelf
15+
16+
class Container:
17+
object NonStaticObjectNoSelf:
18+
def foo: Any = this
19+
def bar: Any = this.foo
20+
end NonStaticObjectNoSelf
21+
22+
object NonStaticObjectWithSelf:
23+
self =>
24+
25+
def foo: Any = self
26+
def bar: Any = self.foo // used to cause StackOverflow while resolving this in WholeClasspathSuite tests
27+
end NonStaticObjectWithSelf
28+
end Container
29+
30+
def methodOwner(): Unit =
31+
object LocalObjectNoSelf:
32+
def foo: Any = this
33+
def bar: Any = this.foo
34+
end LocalObjectNoSelf
35+
36+
object LocalObjectWithSelf:
37+
self =>
38+
39+
def foo: Any = self
40+
def bar: Any = self.foo
41+
end LocalObjectWithSelf
42+
end methodOwner
43+
end ObjectWithSelf

0 commit comments

Comments
 (0)