Skip to content

Commit

Permalink
Fix selecting terms using _root_
Browse files Browse the repository at this point in the history
Switch to only reporting val and def names
  • Loading branch information
dwijnand committed Aug 7, 2023
1 parent fa38cb8 commit 990d112
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
18 changes: 13 additions & 5 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1071,12 +1071,13 @@ object Parsers {
nme.ERROR
}

def checkNotRoot(name: Name): name.type =
if name == nme.ROOTPKG then syntaxError(em"Illegal use of root package name.")
name

/** Accept identifier and return Ident with its name as a term name. */
def termIdent(): Ident =
val t = makeIdent(in.token, in.offset, ident())
if t.name == nme.ROOTPKG then
syntaxError(em"Illegal use of root package name.")
t
makeIdent(in.token, in.offset, ident())

/** Accept identifier and return Ident with its name as a type name. */
def typeIdent(): Ident =
Expand Down Expand Up @@ -3601,6 +3602,13 @@ object Parsers {
case _ =>
first :: Nil
}

def checkForRoot(trees: List[Tree]): Unit = for tree <- trees do tree match
case IdPattern(id, _) => checkNotRoot(id.name)
case Tuple(trees) => checkForRoot(trees)
case _ =>
checkForRoot(lhs)

val tpt = typedOpt()
val rhs =
if tpt.isEmpty || in.token == EQUALS then
Expand Down Expand Up @@ -3681,7 +3689,7 @@ object Parsers {
else {
val mods1 = addFlag(mods, Method)
val ident = termIdent()
var name = ident.name.asTermName
var name = checkNotRoot(ident.name).asTermName
val paramss =
if in.featureEnabled(Feature.clauseInterleaving) then
// If you are making interleaving stable manually, please refer to the PR introducing it instead, section "How to make non-experimental"
Expand Down
6 changes: 3 additions & 3 deletions tests/neg/i18020.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,18 @@ def barVal: Unit =

// i18050
package p {
package _root_ { // error
package _root_ { // not-reported
object X // error
}
}

// scala/bug#12508
package _root_ { // error
package _root_ { // ok
class C {
val _root_ = 42 // error
}
}
package _root_.p { // error
package _root_.p { // ok
class C
}

Expand Down
8 changes: 8 additions & 0 deletions tests/pos/i18275.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package foo

enum MyEnum derives _root_.foo.Eq:
case One

trait Eq[T]
object Eq:
inline def derived[T](using m: scala.deriving.Mirror.Of[T]): Eq[T] = ???

0 comments on commit 990d112

Please sign in to comment.