Skip to content

Commit e5b3023

Browse files
committed
Better error message when package names conflict with other defs
1 parent 13e1b78 commit e5b3023

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

Diff for: compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -1908,10 +1908,15 @@ object messages {
19081908
}
19091909

19101910
case class PackageNameAlreadyDefined(pkg: Symbol)(implicit ctx: Context) extends Message(PackageNameAlreadyDefinedID) {
1911-
val msg: String = em"${pkg} is already defined, cannot be a ${hl("package")}"
1911+
val (where, or) =
1912+
if pkg.associatedFile == null then ("", "")
1913+
else (s" in ${pkg.associatedFile}", " or delete the containing class file")
1914+
val msg: String = em"""${pkg.name} is the name of $pkg$where.
1915+
|It cannot be used at the same time as the name of a package."""
19121916
val kind: String = "Naming"
19131917
val explanation: String =
1914-
em"An ${hl("object")} cannot have the same name as an existing ${hl("package")}. Rename either one of them."
1918+
em"""An ${hl("object")} or other toplevel definition cannot have the same name as an existing ${hl("package")}.
1919+
|Rename either one of them$or."""
19151920
}
19161921

19171922
case class UnapplyInvalidNumberOfArguments(qual: untpd.Tree, argTypes: List[Type])(implicit ctx: Context)

Diff for: compiler/src/dotty/tools/dotc/typer/Typer.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -1879,16 +1879,17 @@ class Typer extends Namer
18791879
val pid1 = typedExpr(tree.pid, AnySelectionProto)(ctx.addMode(Mode.InPackageClauseName))
18801880
val pkg = pid1.symbol
18811881
pid1 match {
1882-
case pid1: RefTree if pkg.exists =>
1883-
if (!pkg.is(Package)) ctx.error(PackageNameAlreadyDefined(pkg), tree.sourcePos)
1882+
case pid1: RefTree if pkg.is(Package) =>
18841883
val packageCtx = ctx.packageContext(tree, pkg)
18851884
var stats1 = typedStats(tree.stats, pkg.moduleClass)(packageCtx)._1
18861885
if (!ctx.isAfterTyper)
18871886
stats1 = stats1 ++ typedBlockStats(MainProxies.mainProxies(stats1))(packageCtx)._1
18881887
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
18891888
case _ =>
18901889
// Package will not exist if a duplicate type has already been entered, see `tests/neg/1708.scala`
1891-
errorTree(tree, i"package ${tree.pid.name} does not exist")
1890+
errorTree(tree,
1891+
if pkg.exists then PackageNameAlreadyDefined(pkg)
1892+
else i"package ${tree.pid.name} does not exist")
18921893
}
18931894
}
18941895

0 commit comments

Comments
 (0)