Skip to content

Commit 990d112

Browse files
committed
Fix selecting terms using _root_
Switch to only reporting val and def names
1 parent fa38cb8 commit 990d112

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

compiler/src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,12 +1071,13 @@ object Parsers {
10711071
nme.ERROR
10721072
}
10731073

1074+
def checkNotRoot(name: Name): name.type =
1075+
if name == nme.ROOTPKG then syntaxError(em"Illegal use of root package name.")
1076+
name
1077+
10741078
/** Accept identifier and return Ident with its name as a term name. */
10751079
def termIdent(): Ident =
1076-
val t = makeIdent(in.token, in.offset, ident())
1077-
if t.name == nme.ROOTPKG then
1078-
syntaxError(em"Illegal use of root package name.")
1079-
t
1080+
makeIdent(in.token, in.offset, ident())
10801081

10811082
/** Accept identifier and return Ident with its name as a type name. */
10821083
def typeIdent(): Ident =
@@ -3601,6 +3602,13 @@ object Parsers {
36013602
case _ =>
36023603
first :: Nil
36033604
}
3605+
3606+
def checkForRoot(trees: List[Tree]): Unit = for tree <- trees do tree match
3607+
case IdPattern(id, _) => checkNotRoot(id.name)
3608+
case Tuple(trees) => checkForRoot(trees)
3609+
case _ =>
3610+
checkForRoot(lhs)
3611+
36043612
val tpt = typedOpt()
36053613
val rhs =
36063614
if tpt.isEmpty || in.token == EQUALS then
@@ -3681,7 +3689,7 @@ object Parsers {
36813689
else {
36823690
val mods1 = addFlag(mods, Method)
36833691
val ident = termIdent()
3684-
var name = ident.name.asTermName
3692+
var name = checkNotRoot(ident.name).asTermName
36853693
val paramss =
36863694
if in.featureEnabled(Feature.clauseInterleaving) then
36873695
// If you are making interleaving stable manually, please refer to the PR introducing it instead, section "How to make non-experimental"

tests/neg/i18020.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,18 @@ def barVal: Unit =
4141

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

4949
// scala/bug#12508
50-
package _root_ { // error
50+
package _root_ { // ok
5151
class C {
5252
val _root_ = 42 // error
5353
}
5454
}
55-
package _root_.p { // error
55+
package _root_.p { // ok
5656
class C
5757
}
5858

tests/pos/i18275.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package foo
2+
3+
enum MyEnum derives _root_.foo.Eq:
4+
case One
5+
6+
trait Eq[T]
7+
object Eq:
8+
inline def derived[T](using m: scala.deriving.Mirror.Of[T]): Eq[T] = ???

0 commit comments

Comments
 (0)