Skip to content

Commit 68b7b75

Browse files
committed
Check package names too
1 parent 052afd8 commit 68b7b75

File tree

3 files changed

+10
-3
lines changed

3 files changed

+10
-3
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,11 @@ trait Checking {
968968
em"Implementation restriction: ${path.tpe.classSymbol} is not a valid prefix for a wildcard export, as it is a package",
969969
path.srcPos)
970970

971+
/** Check that the definition name isn't root. */
972+
def checkNonRootName(name: Name, nameSpan: Span)(using Context): Unit =
973+
if name == nme.ROOTPKG then
974+
report.error(em"Illegal use of root package name.", ctx.source.atSpan(nameSpan))
975+
971976
/** Check that module `sym` does not clash with a class of the same name
972977
* that is concurrently compiled in another source file.
973978
*/

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
24722472

24732473
def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree = {
24742474
val ValDef(name, tpt, _) = vdef
2475-
if name == nme.ROOTPKG then report.error(em"Illegal use of root package name.", vdef)
2475+
checkNonRootName(vdef.name, vdef.nameSpan)
24762476
completeAnnotations(vdef, sym)
24772477
if (sym.isOneOf(GivenOrImplicit)) checkImplicitConversionDefOK(sym)
24782478
if sym.is(Module) then checkNoModuleClash(sym)
@@ -2506,7 +2506,7 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
25062506
// hence we special case it until `erased` is no longer experimental.
25072507
sym.setFlag(Erased)
25082508
val DefDef(name, paramss, tpt, _) = ddef
2509-
if name == nme.ROOTPKG then report.error(em"Illegal use of root package name.", ddef)
2509+
checkNonRootName(ddef.name, ddef.nameSpan)
25102510
completeAnnotations(ddef, sym)
25112511
val paramss1 = paramss.nestedMapConserve(typed(_)).asInstanceOf[List[ParamClause]]
25122512
for case ValDefs(vparams) <- paramss1 do
@@ -2855,6 +2855,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
28552855
val pkg = pid1.symbol
28562856
pid1 match
28572857
case pid1: RefTree if pkg.is(Package) =>
2858+
if ctx.owner != defn.EmptyPackageClass then
2859+
checkNonRootName(pid1.name, pid1.span)
28582860
inContext(ctx.packageContext(tree, pkg)) {
28592861
// If it exists, complete the class containing the top-level definitions
28602862
// before typing any statement in the package to avoid cycles as in i13669.scala

tests/neg/i18020.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ def barVal: Unit =
4141

4242
// i18050
4343
package p {
44-
package _root_ { // not-reported
44+
package _root_ { // error
4545
object X // error
4646
}
4747
}

0 commit comments

Comments
 (0)