Skip to content

Commit

Permalink
Merge pull request #9584 from smarter/scala3-varargs
Browse files Browse the repository at this point in the history
Support Scala 3 vararg splice syntax under -Xsource:3
  • Loading branch information
lrytz committed Apr 21, 2021
2 parents 81c02a0 + 24bd2d5 commit 777f07a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
26 changes: 23 additions & 3 deletions src/compiler/scala/tools/nsc/ast/parser/Parsers.scala
Expand Up @@ -941,6 +941,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 @@ -1735,7 +1745,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 @@ -1746,7 +1756,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 @@ -2105,7 +2120,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)
}
}

0 comments on commit 777f07a

Please sign in to comment.