Skip to content

Commit 9177c15

Browse files
committed
Refactor parser code that rejects forbidden wildcards
The new API is required for all use cases in this PR.
1 parent 5b3d81b commit 9177c15

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -717,15 +717,7 @@ object Parsers {
717717
/** Same as [[typ]], but if this results in a wildcard it emits a syntax error and
718718
* returns a tree for type `Any` instead.
719719
*/
720-
def toplevelTyp(): Tree = {
721-
val t = typ()
722-
findWildcardType(t) match {
723-
case Some(wildcardPos) =>
724-
syntaxError(UnboundWildcardType(), wildcardPos)
725-
scalaAny
726-
case None => t
727-
}
728-
}
720+
def toplevelTyp(): Tree = checkWildcard(typ())
729721

730722
/** Type ::= [FunArgMods] FunArgTypes `=>' Type
731723
* | HkTypeParamClause `->' Type
@@ -925,7 +917,7 @@ object Parsers {
925917
else Nil
926918
first :: rest
927919
}
928-
def typParser() = if (wildOK) typ() else toplevelTyp()
920+
def typParser() = checkWildcard(typ(), wildOK)
929921
if (namedOK && in.token == IDENTIFIER)
930922
typParser() match {
931923
case Ident(name) if in.token == EQUALS =>
@@ -1020,6 +1012,17 @@ object Parsers {
10201012
case _ => None
10211013
}
10221014

1015+
def rejectWildcard(t: Tree, report: Position => Unit, fallbackTree: Tree): Tree =
1016+
findWildcardType(t) match {
1017+
case Some(wildcardPos) =>
1018+
report(wildcardPos)
1019+
fallbackTree
1020+
case None => t
1021+
}
1022+
1023+
def checkWildcard(t: Tree, wildOK: Boolean = false, fallbackTree: Tree = scalaAny): Tree =
1024+
if (wildOK) t else rejectWildcard(t, syntaxError(UnboundWildcardType(), _), fallbackTree)
1025+
10231026
/* ----------- EXPRESSIONS ------------------------------------------------ */
10241027

10251028
/** EqualsExpr ::= `=' Expr

0 commit comments

Comments
 (0)