Skip to content

Commit 959cab4

Browse files
authored
Merge pull request #9837 from dotty-staging/fix-#9829
Fix #9829: Allow `as` in place of `@` for pattern bindings
2 parents 246fc89 + c9caa7e commit 959cab4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+77
-73
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2618,7 +2618,10 @@ object Parsers {
26182618
/** Pattern2 ::= [id `@'] InfixPattern
26192619
*/
26202620
val pattern2: () => Tree = () => infixPattern() match {
2621-
case p @ Ident(name) if in.token == AT =>
2621+
case p @ Ident(name) if in.token == AT || in.isIdent(nme.as) =>
2622+
if in.token == AT && sourceVersion.isAtLeast(`3.1`) then
2623+
deprecationWarning(s"`@` bindings have been deprecated; use `as` instead", in.offset)
2624+
26222625
val offset = in.skipToken()
26232626

26242627
// compatibility for Scala2 `x @ _*` syntax
@@ -2646,7 +2649,8 @@ object Parsers {
26462649
/** InfixPattern ::= SimplePattern {id [nl] SimplePattern}
26472650
*/
26482651
def infixPattern(): Tree =
2649-
infixOps(simplePattern(), in.canStartExprTokens, simplePattern, isOperator = in.name != nme.raw.BAR)
2652+
infixOps(simplePattern(), in.canStartExprTokens, simplePattern,
2653+
isOperator = in.name != nme.raw.BAR && in.name != nme.as)
26502654

26512655
/** SimplePattern ::= PatVar
26522656
* | Literal

docs/docs/internals/syntax.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
273273
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
274274
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
275275
| ‘given’ PatVar ‘:’ RefinedType
276-
Pattern2 ::= [id ‘@’] InfixPattern Bind(name, pat)
276+
Pattern2 ::= [id ‘as’] InfixPattern Bind(name, pat)
277277
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
278278
SimplePattern ::= PatVar Ident(wildcard)
279279
| Literal Bind(name, Ident(wildcard))

library/src-bootstrapped/dotty/internal/StringContextMacro.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ object StringContextMacro {
6262
val sourceFile = strCtxExpr.unseal.pos.sourceFile
6363

6464
val (partsExpr, parts) = strCtxExpr match {
65-
case Expr.StringContext(p1 @ Consts(p2)) => (p1.toList, p2.toList)
65+
case Expr.StringContext(p1 as Consts(p2)) => (p1.toList, p2.toList)
6666
case _ => report.throwError("Expected statically known String Context", strCtxExpr)
6767
}
6868

library/src-bootstrapped/scala/internal/quoted/Matcher.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ object Matcher {
244244

245245
/* Term hole */
246246
// Match a scala.internal.Quoted.patternHole typed as a repeated argument and return the scrutinee tree
247-
case (scrutinee @ Typed(s, tpt1), Typed(TypeApply(patternHole, tpt :: Nil), tpt2))
247+
case (scrutinee as Typed(s, tpt1), Typed(TypeApply(patternHole, tpt :: Nil), tpt2))
248248
if patternHole.symbol == qctx.tasty.Definitions_InternalQuotedMatcher_patternHole &&
249249
s.tpe <:< tpt.tpe &&
250250
tpt2.tpe.derivesFrom(defn.RepeatedParamClass) =>
@@ -259,7 +259,7 @@ object Matcher {
259259

260260
/* Higher order term hole */
261261
// Matches an open term and wraps it into a lambda that provides the free variables
262-
case (scrutinee, pattern @ Apply(TypeApply(Ident("higherOrderHole"), List(Inferred())), Repeated(args, _) :: Nil))
262+
case (scrutinee, pattern as Apply(TypeApply(Ident("higherOrderHole"), List(Inferred())), Repeated(args, _) :: Nil))
263263
if pattern.symbol == qctx.tasty.Definitions_InternalQuotedMatcher_higherOrderHole =>
264264

265265
def bodyFn(lambdaArgs: List[Tree]): Tree = {

library/src-bootstrapped/scala/quoted/util/ExprMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ trait ExprMap {
5151
tree
5252
case Super(qual, mix) =>
5353
tree
54-
case tree @ Apply(fun, args) =>
54+
case tree as Apply(fun, args) =>
5555
val MethodType(_, tpes, _) = fun.tpe.widen
5656
Apply.copy(tree)(transformTerm(fun, Type.of[Any]), transformTerms(args, tpes))
5757
case TypeApply(fun, args) =>

tests/init/crash/fors.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Test extends App {
2626
var n = 0
2727
for (_ <- xs) n += 1; println(n)
2828
for ((x, y) <- xs zip ys) print(x + " "); println()
29-
for (p @ (x, y) <- xs zip ys) print(p._1 + " "); println()
29+
for (p as (x, y) <- xs zip ys) print(p._1 + " "); println()
3030

3131
// iterators
3232
for (x <- it) print(x + " "); println()
@@ -53,7 +53,7 @@ object Test extends App {
5353
var n = 0
5454
for (_ <- xs) n += 1; println(n)
5555
for ((x, y) <- xs zip ys) print(x + " "); println()
56-
for (p @ (x, y) <- xs zip ys) print(p._1 + " "); println()
56+
for (p as (x, y) <- xs zip ys) print(p._1 + " "); println()
5757

5858
// iterators
5959
for (x <- it) print(x + " "); println()

tests/neg/Iter3.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ object Iter2 {
147147
flatten(map(f(_).buildIterator))
148148

149149
override def ++[B >: A](that: IterableOnce[B]): ArrayIterator[B] = {
150-
val thatIterator @ ArrayIterator(elems2, len2) = fromIterator(that.iterator)
150+
val thatIterator as ArrayIterator(elems2, len2) = fromIterator(that.iterator)
151151
if (len == 0) thatIterator
152152
else if (len2 == 0) this.asInstanceOf[ArrayIterator[B]]
153153
else {

tests/neg/ensureReported.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object AnonymousF {
22
val f = {
3-
case l @ List(1) => // error: missing parameter type
3+
case l as List(1) => // error: missing parameter type
44
Some(l)
55
}
66
}

tests/neg/i1716.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object Fail {
22
def f(m: Option[Int]): Unit = {
33
m match {
4-
case x @ Some[_] => // error: unbound wildcard type
4+
case x as Some[_] => // error: unbound wildcard type
55
case _ =>
66
}
77
}

tests/neg/i3200.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test {
22
case object Bob { override def equals(other: Any) = true }
33
def main(args: Array[String]): Unit = {
4-
val m : Bob.type = (5: Any) match { case x @ Bob => x } // error
4+
val m : Bob.type = (5: Any) match { case x as Bob => x } // error
55
}
66
}

0 commit comments

Comments
 (0)