Skip to content

Commit e73397f

Browse files
committed
Fix #1708: return ErrorTree in typedPackageDef if pkg has no symbol
1 parent 17ef1d8 commit e73397f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ class Namer { typer: Typer =>
351351
val existingTpe = pkgOwner.info.decls.lookup(pid.name.toTypeName)
352352
if (existingTpe != NoSymbol) {
353353
ctx.error(PkgDuplicateSymbol(existingTpe), pid.pos)
354-
ctx.newCompletePackageSymbol(pkgOwner, (pid.name.asTermName.show + "$$package").toTermName).entered
354+
ctx.newCompletePackageSymbol(pkgOwner, (pid.name ++ "$termDup").toTermName).entered
355355
}
356356
else ctx.newCompletePackageSymbol(pkgOwner, pid.name.asTermName).entered
357357
}

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") {

0 commit comments

Comments
 (0)