Skip to content

Commit dd29629

Browse files
committed
Fix _root_ in patterns
Also eliminate allowRoot and rawTermIdent, and fix 12508 reference.
1 parent 8be509f commit dd29629

File tree

3 files changed

+26
-23
lines changed

3 files changed

+26
-23
lines changed

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

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1072,12 +1072,8 @@ object Parsers {
10721072
}
10731073

10741074
/** Accept identifier and return Ident with its name as a term name. */
1075-
def rawTermIdent(): Ident =
1076-
makeIdent(in.token, in.offset, ident())
1077-
1078-
/** Call `rawTermIdent`, and check it isn't a root package name. */
10791075
def termIdent(): Ident =
1080-
val ident = rawTermIdent()
1076+
val ident = makeIdent(in.token, in.offset, ident())
10811077
if ident.name == nme.ROOTPKG then
10821078
syntaxError(em"Illegal use of root package name.")
10831079
ident
@@ -1119,7 +1115,7 @@ object Parsers {
11191115
* | [id ‘.’] ‘this’
11201116
* | [id ‘.’] ‘super’ [ClassQualifier] ‘.’ id
11211117
*/
1122-
def simpleRef(allowRoot: Boolean = true): Tree =
1118+
def simpleRef(): Tree =
11231119
val start = in.offset
11241120

11251121
def handleThis(qual: Ident) =
@@ -1135,19 +1131,21 @@ object Parsers {
11351131

11361132
if in.token == THIS then handleThis(EmptyTypeIdent)
11371133
else if in.token == SUPER then handleSuper(EmptyTypeIdent)
1138-
else
1139-
val t = if allowRoot then rawTermIdent() else termIdent()
1140-
if in.token == DOT then
1141-
def qual = cpy.Ident(t)(t.name.toTypeName)
1142-
in.lookahead.token match
1143-
case THIS =>
1144-
in.nextToken()
1145-
handleThis(qual)
1146-
case SUPER =>
1147-
in.nextToken()
1148-
handleSuper(qual)
1149-
case _ => t
1150-
else t
1134+
else if in.lookahead.token == DOT then
1135+
val tok = in.token
1136+
val offset = in.offset
1137+
val name = ident()
1138+
def qual = makeIdent(tok, offset, name.toTypeName)
1139+
in.lookahead.token match
1140+
case THIS =>
1141+
in.nextToken()
1142+
handleThis(qual)
1143+
case SUPER =>
1144+
in.nextToken()
1145+
handleSuper(qual)
1146+
case _ =>
1147+
makeIdent(tok, offset, name)
1148+
else termIdent()
11511149
end simpleRef
11521150

11531151
/** MixinQualifier ::= `[' id `]'
@@ -2972,7 +2970,7 @@ object Parsers {
29722970
*/
29732971
def simplePattern(): Tree = in.token match {
29742972
case IDENTIFIER | BACKQUOTED_IDENT | THIS | SUPER =>
2975-
simpleRef(allowRoot = false) match
2973+
simpleRef() match
29762974
case id @ Ident(nme.raw.MINUS) if isNumericLit => literal(startOffset(id))
29772975
case t => simplePatternRest(t)
29782976
case USCORE =>

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,8 +583,8 @@ class Typer(@constructorOnly nestingLevel: Int = 0) extends Namer
583583
if foundUnderScala2.exists && !(foundUnderScala2 =:= found) then
584584
report.migrationWarning(
585585
em"""Name resolution will change.
586-
| currently selected : $foundUnderScala2
587-
| in the future, without -source 3.0-migration: $found""", tree.srcPos)
586+
| currently selected : $foundUnderScala2
587+
| in the future, without -source 3.0-migration: $found""", tree.srcPos)
588588
foundUnderScala2
589589
else found
590590
finally

tests/neg/i18020.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ package p {
4646
}
4747
}
4848

49-
// i12508
49+
// scala/bug#12508
5050
package _root_ { // error
5151
class C {
5252
val _root_ = 42 // error
@@ -55,3 +55,8 @@ package _root_ { // error
5555
package _root_.p { // error
5656
class C
5757
}
58+
59+
// from ScalaPB
60+
def fromScalaPb(x: Option[String]) = x match
61+
case _root_.scala.Some(s) => s
62+
case _ => ""

0 commit comments

Comments
 (0)