Skip to content

Commit a00ccd3

Browse files
authored
Merge pull request #10565 from dotty-staging/drop-pattern-as
Drop `as` in patterns
2 parents 31751b9 + 2c2bc02 commit a00ccd3

Some content is hidden

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

61 files changed

+86
-90
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -630,7 +630,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
630630
}
631631
}
632632
compareTypeLambda
633-
case tp2 as OrType(tp21, tp22) =>
633+
case tp2 @ OrType(tp21, tp22) =>
634634
compareAtoms(tp1, tp2) match
635635
case Some(b) => return b
636636
case _ =>
@@ -970,7 +970,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
970970
* corresponding arguments are subtypes relative to their variance (see `isSubArgs`).
971971
*/
972972
def isMatchingApply(tp1: Type): Boolean = tp1 match {
973-
case tp1 as AppliedType(tycon1, args1) =>
973+
case tp1 @ AppliedType(tycon1, args1) =>
974974
// We intentionally do not automatically dealias `tycon1` or `tycon2` here.
975975
// `TypeApplications#appliedTo` already takes care of dealiasing type
976976
// constructors when this can be done without affecting type

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ object TypeOps:
150150
tp.derivedAlias(simplify(tp.alias, theMap))
151151
case AndType(l, r) if !ctx.mode.is(Mode.Type) =>
152152
simplify(l, theMap) & simplify(r, theMap)
153-
case tp as OrType(l, r)
153+
case tp @ OrType(l, r)
154154
if !ctx.mode.is(Mode.Type)
155155
&& (tp.isSoft || l.isBottomType || r.isBottomType) =>
156156
// Normalize A | Null and Null | A to A even if the union is hard (i.e.

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2608,10 +2608,7 @@ object Parsers {
26082608
/** Pattern2 ::= [id `as'] InfixPattern
26092609
*/
26102610
val pattern2: () => Tree = () => infixPattern() match {
2611-
case p @ Ident(name) if in.token == AT || in.isIdent(nme.as) =>
2612-
if in.token == AT && sourceVersion.isAtLeast(`3.1`) then
2613-
deprecationWarning(s"`@` bindings have been deprecated; use `as` instead", in.offset)
2614-
2611+
case p @ Ident(name) if in.token == AT =>
26152612
val offset = in.skipToken()
26162613
infixPattern() match {
26172614
case pt @ Ident(tpnme.WILDCARD_STAR) => // compatibility for Scala2 `x @ _*` syntax
@@ -2638,8 +2635,7 @@ object Parsers {
26382635
/** InfixPattern ::= SimplePattern {id [nl] SimplePattern}
26392636
*/
26402637
def infixPattern(): Tree =
2641-
infixOps(simplePattern(), in.canStartExprTokens, simplePattern,
2642-
isOperator = in.name != nme.raw.BAR && in.name != nme.as)
2638+
infixOps(simplePattern(), in.canStartExprTokens, simplePattern, isOperator = in.name != nme.raw.BAR)
26432639

26442640
/** SimplePattern ::= PatVar
26452641
* | Literal

compiler/src/dotty/tools/dotc/printing/RefinedPrinter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
214214
// of AndType and OrType to account for associativity
215215
case AndType(tp1, tp2) =>
216216
toTextInfixType(tpnme.raw.AMP, tp1, tp2) { toText(tpnme.raw.AMP) }
217-
case tp as OrType(tp1, tp2) =>
217+
case tp @ OrType(tp1, tp2) =>
218218
toTextInfixType(tpnme.raw.BAR, tp1, tp2) {
219219
if tp.isSoft && printDebug then toText(tpnme.ZOR) else toText(tpnme.raw.BAR)
220220
}

compiler/src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
480480
else args.map(arg => erase(arg, inArray = false))
481481
tp.derivedAppliedType(erase(tycon, inArray), args2)
482482

483-
case tp as OrType(tp1, tp2) =>
483+
case tp @ OrType(tp1, tp2) =>
484484
OrType(erase(tp1, inArray), erase(tp2, inArray), tp.isSoft)
485485

486486
case AndType(tp1, tp2) =>

compiler/src/dotty/tools/dotc/typer/Applications.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ trait Applications extends Compatibility {
600600
args match {
601601
case arg :: Nil if isVarArg(arg) =>
602602
addTyped(arg, formal)
603-
case (arg as Typed(Literal(Constant(null)), _)) :: Nil if ctx.isAfterTyper =>
603+
case (arg @ Typed(Literal(Constant(null)), _)) :: Nil if ctx.isAfterTyper =>
604604
addTyped(arg, formal)
605605
case _ =>
606606
val elemFormal = formal.widenExpr.argTypesLo.head

docs/docs/internals/syntax.md

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

docs/docs/reference/contextual/givens.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,13 @@ Since `mkAnnotations` is `transparent`, the type of an application is the type o
9292

9393
## Pattern-Bound Given Instances
9494

95-
Given instances can also appear as pattern bound-variables. Example:
95+
Given instances can also appear in patterns. Example:
9696

9797
```scala
9898
for given Context <- applicationContexts do
9999

100100
pair match
101-
case (ctx as given Context, y) => ...
101+
case (ctx @ given Context, y) => ...
102102
```
103103
In the first fragment above, anonymous given instances for class `Context` are established by enumerating over `applicationContexts`. In the second fragment, a given `Context`
104104
instance named `ctx` is established by matching against the first half of the `pair` selector.

library/src/scala/quoted/ExprMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ trait ExprMap:
4747
tree
4848
case Super(qual, mix) =>
4949
tree
50-
case tree as Apply(fun, args) =>
50+
case tree @ Apply(fun, args) =>
5151
val MethodType(_, tpes, _) = fun.tpe.widen
5252
Apply.copy(tree)(transformTerm(fun, TypeRepr.of[Any])(owner), transformTerms(args, tpes)(owner))
5353
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 as (x, y) <- xs zip ys) print(p._1 + " "); println()
29+
for (p @ (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 as (x, y) <- xs zip ys) print(p._1 + " "); println()
56+
for (p @ (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 as ArrayIterator(elems2, len2) = fromIterator(that.iterator)
150+
val thatIterator @ 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 as List(1) => // error: missing parameter type
3+
case l @ 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 as Some[_] => // error: unbound wildcard type
4+
case x @ 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 as Bob => x } // error
4+
val m : Bob.type = (5: Any) match { case x @ 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 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
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
77
}
88
}

tests/neg/i3332.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ object Test {
1616
case _ => println("nope")
1717
}
1818
def test(x: Any) = x match {
19-
case _: String | _ as A() => 1
19+
case _: String | _ @ A() => 1
2020
}
2121
}

tests/neg/i3812b.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ object Test {
1919
Some(x) match { case Some(Z2.v) => () } // ok
2020

2121
Some(x) match { case Some(4) | Some(Z1.v) => () } // error
22-
Some(x) match { case a as Some(Z1.v) => () } // error
22+
Some(x) match { case a @ Some(Z1.v) => () } // error
2323

2424
Some(x) match { case Some(4) | Some(Z2.v) => () } // ok
25-
Some(x) match { case a as Some(Z2.v) => () } // ok
25+
Some(x) match { case a @ Some(Z2.v) => () } // ok
2626
}
2727
}

tests/neg/i8407.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
object Test:
22
val xs = List(1, 2, 3, 4, 5)
33
xs match {
4-
case List(1, 2, xs1 as xs2: _*) => println(xs2) // error // error
4+
case List(1, 2, xs1 @ xs2: _*) => println(xs2) // error // error
55
case _ => ()
66
}

tests/neg/i8715.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
@main
2-
def Test = List(42) match { case List(xs as (ys: _*)) => xs } // error
2+
def Test = List(42) match { case List(xs @ (ys: _*)) => xs } // error

tests/neg/i9310.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//AE-d101cfe6d25117a51897609e673f6c8e74d31e6e
2-
val foo as this = 0
2+
val foo @ this = 0
33
class Foo {
44
foo { } // error
55
}

tests/neg/multi-patterns.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
object Test {
22
val (a :: as), bs = List(1, 2, 3) // error
3-
val B as List(), C: List[Int] = List() // error
3+
val B @ List(), C: List[Int] = List() // error
44
}

tests/neg/patternUnsoundness.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object patternUnsoundness extends App {
1010
val y: C[Object] = x
1111

1212
y match {
13-
case d as D(x) => d.s = new Integer(1) // error
13+
case d @ D(x) => d.s = new Integer(1) // error
1414
}
1515

1616
val z: String = x.s // used to throw ClassCast exception

tests/pos/Iter2.scala

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

195195
override def ++[B >: A](that: IterableOnce[B]): ArrayIterator[B] = {
196-
val thatIterator as ArrayIterator(elems2, len2) = fromIterator(that.iterator)
196+
val thatIterator @ ArrayIterator(elems2, len2) = fromIterator(that.iterator)
197197
if (len == 0) thatIterator
198198
else if (len2 == 0) this
199199
else {

tests/pos/given-pattern.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@ class Test {
66
transparent inline def trySummon[S, T](f: PartialFunction[S, T]): T = ???
77

88
inline def setFor[T]: Set[T] = trySummon {
9-
case ord as given Ordering[T] => new TreeSet[T]
10-
case given Ordering[T] => new TreeSet[T]
11-
case _ => new HashSet[T]
9+
case ord @ given Ordering[T] => new TreeSet[T]
10+
case given Ordering[T] => new TreeSet[T]
11+
case _ => new HashSet[T]
1212
}
1313

1414
def f1[T](x: Ordering[T]) = (x, x) match {
15-
case (y as given Ordering[T], _) => new TreeSet[T]
15+
case (y @ given Ordering[T], _) => new TreeSet[T]
1616
}
1717
def f2[T](x: Ordering[T]) = {
1818
val xs = List(x, x, x)
19-
for y as given Ordering[T] <- xs
19+
for y @ given Ordering[T] <- xs
2020
yield new TreeSet[T]
2121
}
2222
def f3[T](x: Ordering[T]) = (x, x) match {

tests/pos/i10087.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ def f4[T](x: Ordering[T]) = {
88
val xs = List(x, x, x)
99
for given Ordering[T] <- xs
1010
yield new TreeSet[T]
11-
for x as given Ordering[T] <- xs
11+
for x @ given Ordering[T] <- xs
1212
yield new TreeSet[T]
1313
}

tests/pos/i1540.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Casey1 { def unapply(a: Casey1) = a }
88

99
object Test {
1010
def main(args: Array[String]): Unit = {
11-
val c as Casey1(x) = new Casey1(0)
11+
val c @ Casey1(x) = new Casey1(0)
1212
assert(x == c.get)
1313
}
1414
}

tests/pos/i1540b.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ object Casey1 { def unapply[T](a: Casey1[T]) = a }
88

99
object Test {
1010
def main(args: Array[String]): Unit = {
11-
val c as Casey1(x) = new Casey1(0)
11+
val c @ Casey1(x) = new Casey1(0)
1212
assert(x == c.get)
1313
}
1414
}

tests/pos/i3412.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
class Test {
2-
val A as List() = List()
2+
val A @ List() = List()
33
}

tests/pos/i4177.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ class Test {
66
val a: PartialFunction[Int, String] = { case Foo(x) => x }
77
val b: PartialFunction[Int, String] = { case x => x.toString }
88

9-
val e: PartialFunction[String, String] = { case x as "abc" => x }
9+
val e: PartialFunction[String, String] = { case x @ "abc" => x }
1010
val f: PartialFunction[String, String] = x => x match { case "abc" => x }
1111
val g: PartialFunction[String, String] = x => x match { case "abc" if x.isEmpty => x }
1212

tests/pos/i4564.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object ClashNoSig { // ok
1010
def unapply(x: ClashNoSig) = x
1111

1212
ClashNoSig(2) match {
13-
case c as ClashNoSig(y) => c.copy(y + c._1)
13+
case c @ ClashNoSig(y) => c.copy(y + c._1)
1414
}
1515
}
1616
case class ClashNoSig private (x: Int) {

tests/pos/i5402.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ object Main {
99
case 1 => 1
1010
case 0 | 0 => 0
1111
case 2 | 2 | 2 | 3 | 2 | 3 => 0
12-
case 4 | (_ as 4) => 0
12+
case 4 | (_ @ 4) => 0
1313
case _ => -1
1414
}
1515

tests/pos/reference/delegate-match.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ class Test extends App:
55
import scala.compiletime.summonFrom
66

77
transparent inline def setFor[T]: Set[T] = summonFrom {
8-
case ord as given Ordering[T] => new TreeSet[T]
9-
case _ => new HashSet[T]
8+
case ord @ given Ordering[T] => new TreeSet[T]
9+
case _ => new HashSet[T]
1010
}
1111

1212
summon[Ordering[String]]

tests/pos/simpleExtractors-2.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
class Foo {
22
def bar(x: Any): Unit = x match {
33
case Some(Some(i: Int)) => println(i)
4-
case Some(s as Some(i)) => println(s)
5-
case s as Some(r as Some(i)) => println(s)
4+
case Some(s @ Some(i)) => println(s)
5+
case s @ Some(r @ Some(i)) => println(s)
66
}
77
}

tests/pos/t10533.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
object Foo {
2-
val b as Bar(_) = Bar(1)(2)(3)
2+
val b @ Bar(_) = Bar(1)(2)(3)
33
}
44

55
case class Bar(a: Int)(b: Int)(c: Int)

tests/pos/t3136.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object NullaryMethodType {
1313
object Test {
1414
def TEST(tp: Type): String =
1515
tp match {
16-
case PolyType(ps1, PolyType(ps2, res as PolyType(a, b))) => "1" + tp // couldn't find a simpler version that still crashes
16+
case PolyType(ps1, PolyType(ps2, res @ PolyType(a, b))) => "1" + tp // couldn't find a simpler version that still crashes
1717
case NullaryMethodType(meh) => "2" + meh
1818
}
1919
}

tests/pos/t6675.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ object LeftOrRight {
77

88
object Test {
99
(Left((0, 0)): Either[(Int, Int), (Int, Int)]) match {
10-
case LeftOrRight(pair as (a, b)) => a // false -Xlint warning: "extractor pattern binds a single value to a Product2 of type (Int, Int)"
10+
case LeftOrRight(pair @ (a, b)) => a // false -Xlint warning: "extractor pattern binds a single value to a Product2 of type (Int, Int)"
1111
}
1212

1313
(Left((0, 0)): Either[(Int, Int), (Int, Int)]) match {

tests/pos/trailingCommas/trailingCommas.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ trait SimplePattern {
114114

115115
// test '@' syntax in patterns
116116
Some(1) match {
117-
case Some(x as 1,
117+
case Some(x @ 1,
118118
) => x
119119
}
120120

tests/pos/virtpatmat_alts_subst.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
case class Foo(s: String) {
22
def appliedType(tycon: Any) =
33
tycon match {
4-
case Foo(sym as ("NothingClass" | "AnyClass")) => println(sym)
4+
case Foo(sym @ ("NothingClass" | "AnyClass")) => println(sym)
55
}
66
}

tests/run/3179.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
object Test {
22
def main(args: Array[String]): Unit = {
33
("": Any) match {
4-
case a as Test => 1
4+
case a @ Test => 1
55
case _ => 2
66
}
77
}

0 commit comments

Comments
 (0)