Skip to content
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

Support Scala 3 vararg splice syntax under -Xsource:3 #9584

Merged
merged 1 commit into from Apr 21, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 23 additions & 3 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -922,6 +922,16 @@ self =>
mkApply(Ident(op.encode), stripParens(left) :: arguments)
}

/** Is current ident a `*`, and is it followed by a `)` or `, )`? */
def followingIsScala3Vararg(): Boolean =
currentRun.isScala3 && isRawStar && lookingAhead {
in.token == RPAREN ||
in.token == COMMA && {
in.nextToken()
in.token == RPAREN
}
}

/* --------- OPERAND/OPERATOR STACK --------------------------------------- */

/** Modes for infix types. */
Expand Down Expand Up @@ -1716,7 +1726,7 @@ self =>
val base = opstack

@tailrec
def loop(top: Tree): Tree = if (!isIdent) top else {
def loop(top: Tree): Tree = if (!isIdent || followingIsScala3Vararg()) top else {
pushOpInfo(reduceExprStack(base, top))
newLineOptWhenFollowing(isExprIntroToken)
if (isExprIntro)
Expand All @@ -1727,7 +1737,12 @@ self =>
else finishPostfixOp(start, base, popOpInfo())
}

reduceExprStack(base, loop(prefixExpr()))
val expr = reduceExprStack(base, loop(prefixExpr()))
if (followingIsScala3Vararg())
atPos(expr.pos.start) {
Typed(expr, atPos(in.skipToken()) { Ident(tpnme.WILDCARD_STAR) })
}
else expr
}

/** {{{
Expand Down Expand Up @@ -2080,7 +2095,12 @@ self =>
if (isCloseDelim) atPos(top.pos.start, in.prev.offset)(Star(stripParens(top)))
else EmptyTree
)
case _ => EmptyTree
case Ident(name) if isSequenceOK && followingIsScala3Vararg() =>
atPos(top.pos.start) {
Bind(name, atPos(in.skipToken()) { Star(Ident(nme.WILDCARD)) })
}
case _ =>
EmptyTree
}
@tailrec
def loop(top: Tree): Tree = reducePatternStack(base, top) match {
Expand Down
22 changes: 22 additions & 0 deletions test/files/pos/varargs-future.scala
@@ -0,0 +1,22 @@
// scalac: -Xsource:3
//

class Test {
def foo(xs: Int*): Seq[Int] = xs

val s: Seq[Int] = Seq(1, 2, 3)
foo(s*)

// not very useful, but supported by Scala 3 (and matches what works with `: _*` syntax)
foo(
s*,
)

s match {
case Seq(elems*) => println(elems)
}

s match {
case Seq(x, rest*) => println(rest)
}
}