Skip to content

Commit ecdadc9

Browse files
committed
Do not allow named self in objects
`object A { foo: Bla => }` was already prohibited, we extend this restriction to `object A { foo => }`, the latter is not really useful and it currently causes a MatchError in SymDenotation#sourceModule when unpickling.
1 parent a7c6ff7 commit ecdadc9

File tree

10 files changed

+10
-25
lines changed

10 files changed

+10
-25
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ object desugar {
645645
.withPos(mdef.pos.startPos)
646646
val ValDef(selfName, selfTpt, _) = impl.self
647647
val selfMods = impl.self.mods
648-
if (!selfTpt.isEmpty) ctx.error(ObjectMayNotHaveSelfType(mdef), impl.self.pos)
648+
if (!selfTpt.isEmpty || selfName != nme.WILDCARD) ctx.error(ObjectMayNotHaveSelfType(mdef), impl.self.pos)
649649
val clsSelf = ValDef(selfName, SingletonTypeTree(Ident(moduleName)), impl.self.rhs)
650650
.withMods(selfMods)
651651
.withPos(impl.self.pos orElse impl.pos.startPos)

compiler/src/dotty/tools/dotc/core/NameKinds.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ object NameKinds {
6363
def infoString: String
6464
}
6565

66-
object SimpleNameKind extends NameKind(UTF8) { self =>
66+
object SimpleNameKind extends NameKind(UTF8) {
6767
type ThisInfo = Info
6868
val info = new Info
6969
def mkString(underlying: TermName, info: ThisInfo) = unsupported("mkString")

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -954,13 +954,7 @@ class Namer { typer: Typer =>
954954

955955
val selfInfo =
956956
if (self.isEmpty) NoType
957-
else if (cls.is(Module)) {
958-
val moduleType = cls.owner.thisType select sourceModule
959-
if (self.name == nme.WILDCARD) moduleType
960-
else recordSym(
961-
ctx.newSymbol(cls, self.name, self.mods.flags, moduleType, coord = self.pos),
962-
self)
963-
}
957+
else if (cls.is(Module)) cls.owner.thisType.select(sourceModule)
964958
else createSymbol(self)
965959

966960
// pre-set info, so that parent types can refer to type params

compiler/test/dotty/tools/dotc/FromTastyTests.scala

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ class FromTastyTests extends ParallelTesting {
5656
"spec-super.scala",
5757
"spec-sparsearray-old.scala",
5858
"collections_1.scala",
59-
60-
// Infinite compilation
61-
"t3612.scala",
6259
)
6360
)
6461
step1.checkCompile() // Compile all files to generate the class files with tasty

tests/neg/i831.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test { self => // error: objects must not have a self type
2+
def a = 5
3+
self.a // error: not found: self
4+
}

tests/pickling/desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object desugar {
1111
def foo1(first: Int, second: Int = 2)(third: Int = 3) = first + second
1212
def foo2(first: Int)(second: Int = 2)(third: Int = 3) = first + second
1313

14-
object caseClasses { self =>
14+
object caseClasses {
1515
trait List[+T] {
1616
def head: T
1717
def tail: List[T]

tests/pos/desugar.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object desugar {
1111
def foo1(first: Int, second: Int = 2)(third: Int = 3) = first + second
1212
def foo2(first: Int)(second: Int = 2)(third: Int = 3) = first + second
1313

14-
object caseClasses { self =>
14+
object caseClasses {
1515
trait List[+T] {
1616
def head: T
1717
def tail: List[T]

tests/pos/i831.scala

Lines changed: 0 additions & 4 deletions
This file was deleted.

tests/pos/t3612.scala

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)