Skip to content

Commit b58799e

Browse files
authored
Merge pull request #3922 from dotty-staging/fix-#1771
Fix #1771: Harden namer in the presence of double definitions
2 parents 764d3fe + 74a9e10 commit b58799e

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -590,8 +590,10 @@ class Namer { typer: Typer =>
590590
def createLinks(classTree: TypeDef, moduleTree: TypeDef)(implicit ctx: Context) = {
591591
val claz = ctx.effectiveScope.lookup(classTree.name)
592592
val modl = ctx.effectiveScope.lookup(moduleTree.name)
593-
ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, claz, modl).entered
594-
ctx.synthesizeCompanionMethod(nme.COMPANION_MODULE_METHOD, modl, claz).entered
593+
if (claz.isClass && modl.isClass) {
594+
ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, claz, modl).entered
595+
ctx.synthesizeCompanionMethod(nme.COMPANION_MODULE_METHOD, modl, claz).entered
596+
}
595597
}
596598

597599
def createCompanionLinks(implicit ctx: Context): Unit = {

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,20 +1141,19 @@ class Typer extends Namer
11411141
bindings1, expansion1)
11421142
}
11431143

1144-
def typedTypeTree(tree: untpd.TypeTree, pt: Type)(implicit ctx: Context): TypeTree = track("typedTypeTree") {
1144+
def typedTypeTree(tree: untpd.TypeTree, pt: Type)(implicit ctx: Context): Tree = track("typedTypeTree") {
11451145
tree match {
11461146
case tree: untpd.DerivedTypeTree =>
11471147
tree.ensureCompletions
1148-
try
1149-
TypeTree(tree.derivedType(tree.attachment(untpd.OriginalSymbol))) withPos tree.pos
1150-
// btw, no need to remove the attachment. The typed
1151-
// tree is different from the untyped one, so the
1152-
// untyped tree is no longer accessed after all
1153-
// accesses with typedTypeTree are done.
1154-
catch {
1155-
case ex: NoSuchElementException =>
1156-
println(s"missing OriginalSymbol for ${ctx.owner.ownersIterator.toList}")
1157-
throw ex
1148+
tree.getAttachment(untpd.OriginalSymbol) match {
1149+
case Some(origSym) =>
1150+
TypeTree(tree.derivedType(origSym)).withPos(tree.pos)
1151+
// btw, no need to remove the attachment. The typed
1152+
// tree is different from the untyped one, so the
1153+
// untyped tree is no longer accessed after all
1154+
// accesses with typedTypeTree are done.
1155+
case None =>
1156+
errorTree(tree, "Something's wrong: missing original symbol for type tree")
11581157
}
11591158
case _ =>
11601159
tree.withType(

tests/neg/i1771.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class GBTree[B] {
2+
class Tree[A, B]; class Node[A, B](value: Node[A, B]) extends Tree[A, B]
3+
case class B[A, B]() extends Tree[A, B] // error: double definition
4+
}

0 commit comments

Comments
 (0)