Skip to content

Commit 026be41

Browse files
committed
Fix #9829: Allow as in place of @ for pattern bindings
1 parent 333ab8e commit 026be41

Some content is hidden

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

53 files changed

+76
-72
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2631,7 +2631,10 @@ object Parsers {
26312631
/** Pattern2 ::= [id `@'] InfixPattern
26322632
*/
26332633
val pattern2: () => Tree = () => infixPattern() match {
2634-
case p @ Ident(name) if in.token == AT =>
2634+
case p @ Ident(name) if in.token == AT || in.isIdent(nme.as) =>
2635+
if in.token == AT && sourceVersion.isAtLeast(`3.1`) then
2636+
deprecationWarning(s"`@` bindings have been deprecated; use `as` instead", in.offset)
2637+
26352638
val offset = in.skipToken()
26362639

26372640
// compatibility for Scala2 `x @ _*` syntax
@@ -2659,7 +2662,8 @@ object Parsers {
26592662
/** InfixPattern ::= SimplePattern {id [nl] SimplePattern}
26602663
*/
26612664
def infixPattern(): Tree =
2662-
infixOps(simplePattern(), in.canStartExprTokens, simplePattern, isOperator = in.name != nme.raw.BAR)
2665+
infixOps(simplePattern(), in.canStartExprTokens, simplePattern,
2666+
isOperator = in.name != nme.raw.BAR && in.name != nme.as)
26632667

26642668
/** SimplePattern ::= PatVar
26652669
* | Literal

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
}

tests/neg/i3200b.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
3-
val a: Nil.type = (Vector(): Any) match { case n @ Nil => n } // error
4-
val b: Nil.type = (Vector(): Any) match { case n @ (m @ Nil) => n } // error
5-
val c: Int = (1.0: Any) match { case n @ 1 => n } // error
6-
val d: Int = (1.0: Any) match { case n @ (m @ 1) => n } // error
3+
val a: Nil.type = (Vector(): Any) match { case n as Nil => n } // error
4+
val b: Nil.type = (Vector(): Any) match { case n as (m as Nil) => n } // error
5+
val c: Int = (1.0: Any) match { case n as 1 => n } // error
6+
val d: Int = (1.0: Any) match { case n as (m as 1) => n } // error
77
}
88
}

0 commit comments

Comments
 (0)