Skip to content

Commit abb6f36

Browse files
authored
Merge pull request #4162 from dotty-staging/revert-self
Fix issues to make new collections compile again
2 parents 571547d + a26d6c7 commit abb6f36

File tree

11 files changed

+47
-21
lines changed

11 files changed

+47
-21
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 || selfName != nme.WILDCARD) ctx.error(ObjectMayNotHaveSelfType(mdef), impl.self.pos)
648+
if (!selfTpt.isEmpty) 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/Denotations.scala

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,20 @@ object Denotations {
332332
}
333333

334334
/** Handle merge conflict by throwing a `MergeError` exception */
335-
private def mergeConflict(tp1: Type, tp2: Type)(implicit ctx: Context): Type = {
335+
private def mergeConflict(tp1: Type, tp2: Type, that: Denotation)(implicit ctx: Context): Type = {
336+
def showSymbol(sym: Symbol): String = if (sym.exists) sym.showLocated else "[unknown]"
336337
def showType(tp: Type) = tp match {
337338
case ClassInfo(_, cls, _, _, _) => cls.showLocated
338339
case bounds: TypeBounds => i"type bounds $bounds"
339340
case _ => tp.show
340341
}
341-
if (true) throw new MergeError(s"cannot merge ${showType(tp1)} with ${showType(tp2)}", tp1, tp2)
342-
else throw new Error(s"cannot merge ${showType(tp1)} with ${showType(tp2)}") // flip condition for debugging
342+
val msg =
343+
s"""cannot merge
344+
| ${showSymbol(this.symbol)} of type ${showType(tp1)} and
345+
| ${showSymbol(that.symbol)} of type ${showType(tp2)}
346+
"""
347+
if (true) throw new MergeError(msg, tp1, tp2)
348+
else throw new Error(msg) // flip condition for debugging
343349
}
344350

345351
/** Merge parameter names of lambda types. If names in corresponding positions match, keep them,
@@ -389,13 +395,13 @@ object Denotations {
389395
tp2 match {
390396
case tp2: TypeBounds => if (safeIntersection) tp1 safe_& tp2 else tp1 & tp2
391397
case tp2: ClassInfo if tp1 contains tp2 => tp2
392-
case _ => mergeConflict(tp1, tp2)
398+
case _ => mergeConflict(tp1, tp2, that)
393399
}
394400
case tp1: ClassInfo =>
395401
tp2 match {
396402
case tp2: ClassInfo if tp1.cls eq tp2.cls => tp1.derivedClassInfo(tp1.prefix & tp2.prefix)
397403
case tp2: TypeBounds if tp2 contains tp1 => tp1
398-
case _ => mergeConflict(tp1, tp2)
404+
case _ => mergeConflict(tp1, tp2, that)
399405
}
400406
case tp1: MethodOrPoly =>
401407
tp2 match {
@@ -420,9 +426,9 @@ object Denotations {
420426
tp1.derivedLambdaType(
421427
mergeParamNames(tp1, tp2), tp1.paramInfos,
422428
infoMeet(tp1.resultType, tp2.resultType.subst(tp2, tp1)))
423-
else mergeConflict(tp1, tp2)
429+
else mergeConflict(tp1, tp2, that)
424430
case _ =>
425-
mergeConflict(tp1, tp2)
431+
mergeConflict(tp1, tp2, that)
426432
}
427433
case _ =>
428434
tp1 & tp2
@@ -523,7 +529,12 @@ object Denotations {
523529
try infoMeet(info1, info2)
524530
catch {
525531
case ex: MergeError =>
526-
if (pre.widen.classSymbol.is(Scala2x) || ctx.scala2Mode)
532+
// TODO: this picks one type over the other whereas it might be better
533+
// to return a MultiDenotation instead. But doing so would affect lots of
534+
// things, starting with the return type of this method.
535+
if (preferSym(sym2, sym1)) info2
536+
else if (preferSym(sym1, sym2)) info1
537+
else if (pre.widen.classSymbol.is(Scala2x) || ctx.scala2Mode)
527538
info1 // follow Scala2 linearization -
528539
// compare with way merge is performed in SymDenotation#computeMembersNamed
529540
else
@@ -560,13 +571,13 @@ object Denotations {
560571
tp2 match {
561572
case tp2: TypeBounds => tp1 | tp2
562573
case tp2: ClassInfo if tp1 contains tp2 => tp1
563-
case _ => mergeConflict(tp1, tp2)
574+
case _ => mergeConflict(tp1, tp2, that)
564575
}
565576
case tp1: ClassInfo =>
566577
tp2 match {
567578
case tp2: ClassInfo if tp1.cls eq tp2.cls => tp1.derivedClassInfo(tp1.prefix | tp2.prefix)
568579
case tp2: TypeBounds if tp2 contains tp1 => tp2
569-
case _ => mergeConflict(tp1, tp2)
580+
case _ => mergeConflict(tp1, tp2, that)
570581
}
571582
case tp1: MethodOrPoly =>
572583
tp2 match {
@@ -577,7 +588,7 @@ object Denotations {
577588
mergeParamNames(tp1, tp2), tp1.paramInfos,
578589
tp1.resultType | tp2.resultType.subst(tp2, tp1))
579590
case _ =>
580-
mergeConflict(tp1, tp2)
591+
mergeConflict(tp1, tp2, that)
581592
}
582593
case _ =>
583594
tp1 | tp2

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) {
66+
object SimpleNameKind extends NameKind(UTF8) { self =>
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: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,13 @@ class Namer { typer: Typer =>
954954

955955
val selfInfo =
956956
if (self.isEmpty) NoType
957-
else if (cls.is(Module)) cls.owner.thisType.select(sourceModule)
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+
}
958964
else createSymbol(self)
959965

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class FromTastyTests extends ParallelTesting {
5959
"spec-super.scala",
6060
"spec-sparsearray-old.scala",
6161
"collections_1.scala",
62+
63+
// Infinite compilation
64+
"t3612.scala",
6265
)
6366
)
6467
step1.checkCompile() // Compile all files to generate the class files with tasty

tests/neg/i831.scala

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

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 {
14+
object caseClasses { self =>
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 {
14+
object caseClasses { self =>
1515
trait List[+T] {
1616
def head: T
1717
def tail: List[T]

tests/pos/i831.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Test { self =>
2+
def a = 5
3+
self.a
4+
}

tests/pos/t3612.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait C
2+
3+
class Outer {
4+
object O0 extends C {}
5+
object O extends C { self => }
6+
}

0 commit comments

Comments
 (0)