Skip to content

support xs @ _* and _* in Scala2 mode #1179

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 34 additions & 5 deletions src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,19 @@ object Parsers {
} finally inFunReturnType = saved
}

private val isScala2Mode =
ctx.settings.language.value.contains(nme.Scala2.toString)

def migrationWarningOrError(msg: String, offset: Int = in.offset) =
if (isScala2Mode)
ctx.migrationWarning(msg, source atPos Position(offset))
else
syntaxError(msg, offset)

/** Cannot use ctx.featureEnabled because accessing the context would force too much */
private def testScala2Mode(msg: String, pos: Position = Position(in.offset)) = {
val s2 = ctx.settings.language.value.contains(nme.Scala2.toString)
if (s2) ctx.migrationWarning(msg, source atPos pos)
s2
if (isScala2Mode) ctx.migrationWarning(msg, source atPos pos)
isScala2Mode
}

/* ---------- TREE CONSTRUCTION ------------------------------------------- */
Expand Down Expand Up @@ -1309,7 +1317,20 @@ object Parsers {
*/
val pattern2 = () => infixPattern() match {
case p @ Ident(name) if isVarPattern(p) && in.token == AT =>
atPos(p.pos.start, in.skipToken()) { Bind(name, infixPattern()) }
val pos = in.skipToken()

// compatibility for Scala2 `x @ _*` syntax
infixPattern() match {
case pt @ Ident(tpnme.WILDCARD_STAR) =>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(3) Then this code can be written shorter like this:

    migrationWarningOrError("The syntax `x @ _*' is no longer supported; use `x : _*' instead", p.pos)

migrationWarningOrError("The syntax `x @ _*' is no longer supported; use `x : _*' instead", p.pos.start)
atPos(p.pos.start, pos) { Typed(p, pt) }
case p =>
atPos(p.pos.start, pos) { Bind(name, p) }
}
case p @ Ident(tpnme.WILDCARD_STAR) =>
// compatibility for Scala2 `_*` syntax
migrationWarningOrError("The syntax `_*' is no longer supported; use `x : _*' instead", p.pos.start)
atPos(p.pos.start) { Typed(Ident(nme.WILDCARD), p) }
case p =>
p
}
Expand Down Expand Up @@ -1337,7 +1358,15 @@ object Parsers {
case t => simplePatternRest(t)
}
case USCORE =>
wildcardIdent()
val wildIndent = wildcardIdent()

// compatibility for Scala2 `x @ _*` and `_*` syntax
// `x: _*' is parsed in `ascription'
if (isIdent(nme.raw.STAR)) {
in.nextToken()
if (in.token != RPAREN) syntaxError("`_*' can be used only for last argument", wildIndent.pos)
atPos(wildIndent.pos) { Ident(tpnme.WILDCARD_STAR) }
} else wildIndent
case LPAREN =>
atPos(in.offset) { makeTupleOrParens(inParens(patternsOpt())) }
case LBRACE =>
Expand Down
10 changes: 10 additions & 0 deletions tests/neg/i1059.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Repeated {
val list = List(1, 2, 3)

list match {
case List(_, _, _, _ @ _*) => 0 // error: only allowed in Scala2 mode
case List(_, _, _*) => 1 // error: only allowed in Scala2 mode
case List(_, _: _*) => 2
case Nil => 3
}
}
10 changes: 10 additions & 0 deletions tests/pos-scala2/i1059.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Repeated {
val list = List(1, 2, 3)

list match {
case List(_, _, _, _ @ _*) => 0
case List(_, _, _*) => 1
case List(_, _: _*) => 2
case Nil => 3
}
}