Skip to content

Commit dd0db48

Browse files
authored
Merge pull request #1722 from dotty-staging/topic/fix#1708
Fix #1708: duplicate symbols in package
2 parents 156a9d9 + 040379e commit dd0db48

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed

src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -790,7 +790,8 @@ object messages {
790790
val explanation = ""
791791
}
792792

793-
case class SeqWildcardPatternPos()(implicit ctx: Context) extends Message(31) {
793+
case class SeqWildcardPatternPos()(implicit ctx: Context)
794+
extends Message(31) {
794795
val kind = "Syntax"
795796
val msg = "`_*' can be used only for last argument"
796797
val explanation = {
@@ -891,4 +892,11 @@ object messages {
891892
|"""
892893
}
893894
}
895+
896+
case class PkgDuplicateSymbol(existing: Symbol)(implicit ctx: Context)
897+
extends Message(33) {
898+
val kind = "Duplicate Symbol"
899+
val msg = hl"trying to define package with same name as `$existing`"
900+
val explanation = ""
901+
}
894902
}

src/dotty/tools/dotc/typer/FrontEnd.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class FrontEnd extends Phase {
3535
else new Parser(unit.source).parse()
3636
val printer = if (ctx.settings.Xprint.value.contains("parser")) default else typr
3737
printer.println("parsed:\n" + unit.untpdTree.show)
38-
if (Config.checkPositions)
38+
if (Config.checkPositions)
3939
unit.untpdTree.checkPos(nonOverlapping = !unit.isJava && !ctx.reporter.hasErrors)
4040
}
4141

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Inferencing._
2121
import transform.ValueClasses._
2222
import TypeApplications._
2323
import language.implicitConversions
24+
import reporting.diagnostic.messages._
2425

2526
trait NamerContextOps { this: Context =>
2627

@@ -264,7 +265,7 @@ class Namer { typer: Typer =>
264265
def preExisting = ctx.effectiveScope.lookup(name)
265266
if (ctx.owner is PackageClass)
266267
if (preExisting.isDefinedInCurrentRun)
267-
errorName(s"${preExisting.showLocated} is compiled twice")
268+
errorName(s"${preExisting.showLocated} has already been compiled\nonce during this run")
268269
else name
269270
else
270271
if ((!ctx.owner.isClass || name.isTypeName) && preExisting.exists)
@@ -343,8 +344,17 @@ class Namer { typer: Typer =>
343344
case Select(qual: RefTree, _) => createPackageSymbol(qual).moduleClass
344345
}
345346
val existing = pkgOwner.info.decls.lookup(pid.name)
347+
346348
if ((existing is Package) && (pkgOwner eq existing.owner)) existing
347-
else ctx.newCompletePackageSymbol(pkgOwner, pid.name.asTermName).entered
349+
else {
350+
/** If there's already an existing type, then the package is a dup of this type */
351+
val existingType = pkgOwner.info.decls.lookup(pid.name.toTypeName)
352+
if (existingType.exists) {
353+
ctx.error(PkgDuplicateSymbol(existingType), pid.pos)
354+
ctx.newCompletePackageSymbol(pkgOwner, (pid.name ++ "$_error_").toTermName).entered
355+
}
356+
else ctx.newCompletePackageSymbol(pkgOwner, pid.name.asTermName).entered
357+
}
348358
}
349359

350360
/** Expand tree and store in `expandedTree` */
@@ -377,9 +387,10 @@ class Namer { typer: Typer =>
377387
localCtx
378388
}
379389

380-
/** For all class definitions `stat` in `xstats`: If the companion class if not also defined
381-
* in `xstats`, invalidate it by setting its info to NoType.
382-
*/
390+
/** For all class definitions `stat` in `xstats`: If the companion class if
391+
* not also defined in `xstats`, invalidate it by setting its info to
392+
* NoType.
393+
*/
383394
def invalidateCompanions(pkg: Symbol, xstats: List[untpd.Tree])(implicit ctx: Context): Unit = {
384395
val definedNames = xstats collect { case stat: NameTree => stat.name }
385396
def invalidate(name: TypeName) =

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

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,14 +1341,19 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
13411341
def typedPackageDef(tree: untpd.PackageDef)(implicit ctx: Context): Tree = track("typedPackageDef") {
13421342
val pid1 = typedExpr(tree.pid, AnySelectionProto)(ctx.addMode(Mode.InPackageClauseName))
13431343
val pkg = pid1.symbol
1344-
val packageContext =
1345-
if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree)
1346-
else {
1347-
ctx.error(em"$pkg is already defined, cannot be a package", tree.pos)
1348-
ctx
1349-
}
1350-
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext)
1351-
cpy.PackageDef(tree)(pid1.asInstanceOf[RefTree], stats1) withType pkg.valRef
1344+
1345+
// Package will not exist if a duplicate type has already been entered, see
1346+
// `tests/neg/1708.scala`, else branch's error message should be supressed
1347+
if (pkg.exists) {
1348+
val packageContext =
1349+
if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree)
1350+
else {
1351+
ctx.error(em"$pkg is already defined, cannot be a package", tree.pos)
1352+
ctx
1353+
}
1354+
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext)
1355+
cpy.PackageDef(tree)(pid1.asInstanceOf[RefTree], stats1) withType pkg.valRef
1356+
} else errorTree(tree, i"package ${tree.pid.name} does not exist")
13521357
}
13531358

13541359
def typedAnnotated(tree: untpd.Annotated, pt: Type)(implicit ctx: Context): Tree = track("typedAnnotated") {

tests/neg/i1708.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
package foo { trait identifier }
2+
package foo { class identifier } // error

tests/neg/i1708b.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package foo {
2+
trait id {
3+
def bar: Int
4+
}
5+
}
6+
package foo {
7+
package id { // error
8+
class Bar
9+
}
10+
}

0 commit comments

Comments
 (0)