Skip to content

Fix #1708: duplicate symbols in package #1722

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 17, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -790,7 +790,8 @@ object messages {
val explanation = ""
}

case class SeqWildcardPatternPos()(implicit ctx: Context) extends Message(31) {
case class SeqWildcardPatternPos()(implicit ctx: Context)
extends Message(31) {
val kind = "Syntax"
val msg = "`_*' can be used only for last argument"
val explanation = {
Expand Down Expand Up @@ -891,4 +892,11 @@ object messages {
|"""
}
}

case class PkgDuplicateSymbol(existing: Symbol)(implicit ctx: Context)
extends Message(33) {
val kind = "Duplicate Symbol"
val msg = hl"trying to define package with same name as `$existing`"
val explanation = ""
}
}
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/typer/FrontEnd.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class FrontEnd extends Phase {
else new Parser(unit.source).parse()
val printer = if (ctx.settings.Xprint.value.contains("parser")) default else typr
printer.println("parsed:\n" + unit.untpdTree.show)
if (Config.checkPositions)
if (Config.checkPositions)
unit.untpdTree.checkPos(nonOverlapping = !unit.isJava && !ctx.reporter.hasErrors)
}

Expand Down
21 changes: 16 additions & 5 deletions src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import Inferencing._
import transform.ValueClasses._
import TypeApplications._
import language.implicitConversions
import reporting.diagnostic.messages._

trait NamerContextOps { this: Context =>

Expand Down Expand Up @@ -264,7 +265,7 @@ class Namer { typer: Typer =>
def preExisting = ctx.effectiveScope.lookup(name)
if (ctx.owner is PackageClass)
if (preExisting.isDefinedInCurrentRun)
errorName(s"${preExisting.showLocated} is compiled twice")
errorName(s"${preExisting.showLocated} has already been compiled\nonce during this run")
else name
else
if ((!ctx.owner.isClass || name.isTypeName) && preExisting.exists)
Expand Down Expand Up @@ -343,8 +344,17 @@ class Namer { typer: Typer =>
case Select(qual: RefTree, _) => createPackageSymbol(qual).moduleClass
}
val existing = pkgOwner.info.decls.lookup(pid.name)

if ((existing is Package) && (pkgOwner eq existing.owner)) existing
else ctx.newCompletePackageSymbol(pkgOwner, pid.name.asTermName).entered
else {
/** If there's already an existing type, then the package is a dup of this type */
val existingType = pkgOwner.info.decls.lookup(pid.name.toTypeName)
if (existingType.exists) {
ctx.error(PkgDuplicateSymbol(existingType), pid.pos)
ctx.newCompletePackageSymbol(pkgOwner, (pid.name ++ "$_error_").toTermName).entered
}
else ctx.newCompletePackageSymbol(pkgOwner, pid.name.asTermName).entered
}
}

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

/** For all class definitions `stat` in `xstats`: If the companion class if not also defined
* in `xstats`, invalidate it by setting its info to NoType.
*/
/** For all class definitions `stat` in `xstats`: If the companion class if
* not also defined in `xstats`, invalidate it by setting its info to
* NoType.
*/
def invalidateCompanions(pkg: Symbol, xstats: List[untpd.Tree])(implicit ctx: Context): Unit = {
val definedNames = xstats collect { case stat: NameTree => stat.name }
def invalidate(name: TypeName) =
Expand Down
21 changes: 13 additions & 8 deletions src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1341,14 +1341,19 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
def typedPackageDef(tree: untpd.PackageDef)(implicit ctx: Context): Tree = track("typedPackageDef") {
val pid1 = typedExpr(tree.pid, AnySelectionProto)(ctx.addMode(Mode.InPackageClauseName))
val pkg = pid1.symbol
val packageContext =
if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree)
else {
ctx.error(em"$pkg is already defined, cannot be a package", tree.pos)
ctx
}
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext)
cpy.PackageDef(tree)(pid1.asInstanceOf[RefTree], stats1) withType pkg.valRef

// Package will not exist if a duplicate type has already been entered, see
// `tests/neg/1708.scala`, else branch's error message should be supressed
if (pkg.exists) {
val packageContext =
if (pkg is Package) ctx.fresh.setOwner(pkg.moduleClass).setTree(tree)
else {
ctx.error(em"$pkg is already defined, cannot be a package", tree.pos)
ctx
}
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageContext)
cpy.PackageDef(tree)(pid1.asInstanceOf[RefTree], stats1) withType pkg.valRef
} else errorTree(tree, i"package ${tree.pid.name} does not exist")
}

def typedAnnotated(tree: untpd.Annotated, pt: Type)(implicit ctx: Context): Tree = track("typedAnnotated") {
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i1643.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trait T extends Array {
trait T extends Array { // error
def t1(as: String*): Array[String] = { varargs1(as: _*) } // error
def t2(as: String*): Array[String] = { super.varargs1(as: _*) } // error
}
Expand Down
2 changes: 2 additions & 0 deletions tests/neg/i1708.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
package foo { trait identifier }
package foo { class identifier } // error
10 changes: 10 additions & 0 deletions tests/neg/i1708b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package foo {
trait id {
def bar: Int
}
}
package foo {
package id { // error
class Bar
}
}