Skip to content

Commit c3bd95a

Browse files
committed
Harden typedPackageDef
1 parent 5ad42d1 commit c3bd95a

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1626,16 +1626,16 @@ class Typer extends Namer
16261626
def typedPackageDef(tree: untpd.PackageDef)(implicit ctx: Context): Tree = track("typedPackageDef") {
16271627
val pid1 = typedExpr(tree.pid, AnySelectionProto)(ctx.addMode(Mode.InPackageClauseName))
16281628
val pkg = pid1.symbol
1629-
1630-
// Package will not exist if a duplicate type has already been entered, see
1631-
// `tests/neg/1708.scala`, else branch's error message should be supressed
1632-
if (pkg.exists) {
1633-
if (!pkg.is(Package)) ctx.error(PackageNameAlreadyDefined(pkg), tree.pos)
1634-
val packageCtx = ctx.packageContext(tree, pkg)
1635-
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageCtx)
1636-
cpy.PackageDef(tree)(pid1.asInstanceOf[RefTree], stats1) withType pkg.termRef
1629+
pid1 match {
1630+
case pid1: RefTree if pkg.exists =>
1631+
if (!pkg.is(Package)) ctx.error(PackageNameAlreadyDefined(pkg), tree.pos)
1632+
val packageCtx = ctx.packageContext(tree, pkg)
1633+
val stats1 = typedStats(tree.stats, pkg.moduleClass)(packageCtx)
1634+
cpy.PackageDef(tree)(pid1, stats1).withType(pkg.termRef)
1635+
case _ =>
1636+
// Package will not exist if a duplicate type has already been entered, see `tests/neg/1708.scala`
1637+
errorTree(tree, i"package ${tree.pid.name} does not exist")
16371638
}
1638-
else errorTree(tree, i"package ${tree.pid.name} does not exist")
16391639
}
16401640

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

tests/neg/parser-stability-22.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package x0.x0 {}
2+
object x0 {
3+
def x0 (implicit // error // error

0 commit comments

Comments
 (0)