From ecdadc92d23943d774274b253f9867ddc584e841 Mon Sep 17 00:00:00 2001 From: Guillaume Martres Date: Thu, 15 Mar 2018 16:00:43 +0100 Subject: [PATCH] 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. --- compiler/src/dotty/tools/dotc/ast/Desugar.scala | 2 +- compiler/src/dotty/tools/dotc/core/NameKinds.scala | 2 +- compiler/src/dotty/tools/dotc/typer/Namer.scala | 8 +------- compiler/test/dotty/tools/dotc/FromTastyTests.scala | 3 --- scala2-library | 2 +- tests/neg/i831.scala | 4 ++++ tests/pickling/desugar.scala | 2 +- tests/pos/desugar.scala | 2 +- tests/pos/i831.scala | 4 ---- tests/pos/t3612.scala | 6 ------ 10 files changed, 10 insertions(+), 25 deletions(-) create mode 100644 tests/neg/i831.scala delete mode 100644 tests/pos/i831.scala delete mode 100644 tests/pos/t3612.scala diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index 30dad2f44847..4c051163a58b 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -645,7 +645,7 @@ object desugar { .withPos(mdef.pos.startPos) val ValDef(selfName, selfTpt, _) = impl.self val selfMods = impl.self.mods - if (!selfTpt.isEmpty) ctx.error(ObjectMayNotHaveSelfType(mdef), impl.self.pos) + if (!selfTpt.isEmpty || selfName != nme.WILDCARD) ctx.error(ObjectMayNotHaveSelfType(mdef), impl.self.pos) val clsSelf = ValDef(selfName, SingletonTypeTree(Ident(moduleName)), impl.self.rhs) .withMods(selfMods) .withPos(impl.self.pos orElse impl.pos.startPos) diff --git a/compiler/src/dotty/tools/dotc/core/NameKinds.scala b/compiler/src/dotty/tools/dotc/core/NameKinds.scala index bf758ac77300..24cbb051b082 100644 --- a/compiler/src/dotty/tools/dotc/core/NameKinds.scala +++ b/compiler/src/dotty/tools/dotc/core/NameKinds.scala @@ -63,7 +63,7 @@ object NameKinds { def infoString: String } - object SimpleNameKind extends NameKind(UTF8) { self => + object SimpleNameKind extends NameKind(UTF8) { type ThisInfo = Info val info = new Info def mkString(underlying: TermName, info: ThisInfo) = unsupported("mkString") diff --git a/compiler/src/dotty/tools/dotc/typer/Namer.scala b/compiler/src/dotty/tools/dotc/typer/Namer.scala index 69836719c7d3..c7217ef7cd3f 100644 --- a/compiler/src/dotty/tools/dotc/typer/Namer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Namer.scala @@ -954,13 +954,7 @@ class Namer { typer: Typer => val selfInfo = if (self.isEmpty) NoType - else if (cls.is(Module)) { - val moduleType = cls.owner.thisType select sourceModule - if (self.name == nme.WILDCARD) moduleType - else recordSym( - ctx.newSymbol(cls, self.name, self.mods.flags, moduleType, coord = self.pos), - self) - } + else if (cls.is(Module)) cls.owner.thisType.select(sourceModule) else createSymbol(self) // pre-set info, so that parent types can refer to type params diff --git a/compiler/test/dotty/tools/dotc/FromTastyTests.scala b/compiler/test/dotty/tools/dotc/FromTastyTests.scala index ff9ff9431a5c..0e0aa9d0edd2 100644 --- a/compiler/test/dotty/tools/dotc/FromTastyTests.scala +++ b/compiler/test/dotty/tools/dotc/FromTastyTests.scala @@ -56,9 +56,6 @@ class FromTastyTests extends ParallelTesting { "spec-super.scala", "spec-sparsearray-old.scala", "collections_1.scala", - - // Infinite compilation - "t3612.scala", ) ) step1.checkCompile() // Compile all files to generate the class files with tasty diff --git a/scala2-library b/scala2-library index e588767817e4..723f22266db0 160000 --- a/scala2-library +++ b/scala2-library @@ -1 +1 @@ -Subproject commit e588767817e4985852f6547fc7cf211a8a7df3fa +Subproject commit 723f22266db0a72e3ac19358586e3426757ac201 diff --git a/tests/neg/i831.scala b/tests/neg/i831.scala new file mode 100644 index 000000000000..53e4185ed9a8 --- /dev/null +++ b/tests/neg/i831.scala @@ -0,0 +1,4 @@ +object Test { self => // error: objects must not have a self type + def a = 5 + self.a // error: not found: self +} diff --git a/tests/pickling/desugar.scala b/tests/pickling/desugar.scala index 0d3b6d8ca624..720f46d9c3e7 100644 --- a/tests/pickling/desugar.scala +++ b/tests/pickling/desugar.scala @@ -11,7 +11,7 @@ object desugar { def foo1(first: Int, second: Int = 2)(third: Int = 3) = first + second def foo2(first: Int)(second: Int = 2)(third: Int = 3) = first + second - object caseClasses { self => + object caseClasses { trait List[+T] { def head: T def tail: List[T] diff --git a/tests/pos/desugar.scala b/tests/pos/desugar.scala index cc679498578f..d5e13fb1d8c8 100644 --- a/tests/pos/desugar.scala +++ b/tests/pos/desugar.scala @@ -11,7 +11,7 @@ object desugar { def foo1(first: Int, second: Int = 2)(third: Int = 3) = first + second def foo2(first: Int)(second: Int = 2)(third: Int = 3) = first + second - object caseClasses { self => + object caseClasses { trait List[+T] { def head: T def tail: List[T] diff --git a/tests/pos/i831.scala b/tests/pos/i831.scala deleted file mode 100644 index 629853b9ca6b..000000000000 --- a/tests/pos/i831.scala +++ /dev/null @@ -1,4 +0,0 @@ -object Test { self => - def a = 5 - self.a -} diff --git a/tests/pos/t3612.scala b/tests/pos/t3612.scala deleted file mode 100644 index a9d063998ca1..000000000000 --- a/tests/pos/t3612.scala +++ /dev/null @@ -1,6 +0,0 @@ -trait C - -class Outer { - object O0 extends C {} - object O extends C { self => } -}