Skip to content

Commit 5f9717d

Browse files
committed
Switch to postfix as for patterns
1 parent 8fb1920 commit 5f9717d

File tree

90 files changed

+143
-134
lines changed

Some content is hidden

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

90 files changed

+143
-134
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: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2657,12 +2657,12 @@ object Parsers {
26572657
ascription(p, location)
26582658
else p
26592659

2660-
/** Pattern2 ::= [id `as'] InfixPattern
2660+
/** Pattern2 ::= InfixPattern [`as' id]
26612661
*/
2662-
val pattern2: () => Tree = () => infixPattern() match {
2663-
case p @ Ident(name) if in.token == AT || in.isIdent(nme.as) =>
2664-
if in.token == AT && sourceVersion.isAtLeast(`3.1`) then
2665-
deprecationWarning(s"`@` bindings have been deprecated; use `as` instead", in.offset)
2662+
val pattern2: () => Tree = () => infixPattern() match
2663+
case p @ Ident(name) if in.token == AT =>
2664+
if sourceVersion.isAtLeast(`3.1`) then
2665+
deprecationWarning(s"`<name> @ <pattern>` bindings have been deprecated; use `<apttern> as <name>` instead", in.offset)
26662666

26672667
val offset = in.skipToken()
26682668
infixPattern() match {
@@ -2678,8 +2678,18 @@ object Parsers {
26782678
warnMigration(p)
26792679
atSpan(startOffset(p)) { Typed(Ident(nme.WILDCARD), p) }
26802680
case p =>
2681-
p
2682-
}
2681+
if in.isIdent(nme.as) then
2682+
in.nextToken()
2683+
atSpan(startOffset(p), in.offset) {
2684+
p match
2685+
case p @ Bind(nme.WILDCARD, pt: Typed) if p.mods.is(Given) =>
2686+
Bind(ident(), pt).withMods(p.mods)
2687+
case _ =>
2688+
Bind(ident(), p)
2689+
}
2690+
else
2691+
p
2692+
end pattern2
26832693

26842694
private def warnMigration(p: Tree) =
26852695
if sourceVersion.isAtLeast(`3.1`) then

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 & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,7 @@ TypeCaseClauses ::= TypeCaseClause { TypeCaseClause }
268268
TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]
269269
270270
Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
271-
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
272-
Pattern2 ::= [id ‘as’] InfixPattern Bind(name, pat)
271+
Pattern1 ::= InfixPattern [‘as’ id] [‘:’ RefinedType] Bind(name, pat), Bind(name, Typed(Ident(wildcard), tpe))
273272
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
274273
SimplePattern ::= PatVar Ident(wildcard)
275274
| Literal Bind(name, Ident(wildcard))

docs/docs/reference/contextual/given-imports.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ For instance, assuming the object
6161
```scala
6262
object Instances {
6363
given Ordering[Int] as intOrd
64-
given listOrd[T: Ordering] as Ordering[List[T]]
65-
given ec as ExecutionContext = ...
66-
given im as Monoid[Int]
64+
given [T: Ordering] => Ordering[List[T]] as listOrd
65+
given ExecutionContext as ec = ...
66+
given Monoid[Int] as im
6767
}
6868
```
6969

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) =>

scala3doc-testcases/src/tests/implicitConversions2.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class OuterClass //unexpected
2929
= ???
3030
}
3131

32-
given conversionFromVal as Conversion[ClassWithConversionFromVal, Methods] //unexpected
32+
given Conversion[ClassWithConversionFromVal, Methods] as conversionFromVal //unexpected
3333
{
3434
def apply(a: ClassWithConversionFromVal): Methods //unexpected
3535
= ???

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 ((x, y) as p <- 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 ((x, y) as p <- 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 ArrayIterator(elems2, len2) as thatIterator = 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 List(1) as l => // 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 Some[_] as x => // 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 Bob as x => 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 Nil as n => n } // error
4+
val b: Nil.type = (Vector(): Any) match { case (Nil as m) as n => n } // error
5+
val c: Int = (1.0: Any) match { case 1 as n => n } // error
6+
val d: Int = (1.0: Any) match { case (1 as m) as n => 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 Some(Z1.v) as a => () } // 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 Some(Z2.v) as a => () } // 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, xs2 as xs1: _*) => 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((ys: _*) as xs) => 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 this as foo = 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 List() as B, 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(x) as d => d.s = new Integer(1) // error
1414
}
1515

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

tests/patmat/i7186.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ object printMips {
118118
s"$indent.globl $id$endl"
119119
case Label(Scoped(Identifier(i),-1)) =>
120120
s"$i:$endl"
121-
case Label(s @ Scoped(Identifier(i), id)) =>
121+
case Label(Scoped(Identifier(i), id) as s) =>
122122
s"${getScopedLabel(s)}: #debug: $i~$id$endl"
123123
case ControlLabel(id) =>
124124
s"${evalLabels(id)}:$endl"

tests/pos-macros/i6535/Macro_1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ object scalatest {
1010
import ValDef.let
1111

1212
Term.of(cond).underlyingArgument match {
13-
case t @ Apply(Select(lhs, op), rhs :: Nil) =>
13+
case Apply(Select(lhs, op), rhs :: Nil) as t =>
1414
let(Symbol.spliceOwner, lhs) { left =>
1515
let(Symbol.spliceOwner, rhs) { right =>
1616
val app = Select.overloaded(left, op, Nil, right :: Nil)

tests/pos-macros/quoted-pattern-type.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,39 +4,39 @@ object Lib {
44

55
def impl[T: Type](arg: Expr[T])(using Quotes): Expr[T] = {
66
arg match {
7-
case e @ '{ $x: Boolean } =>
7+
case '{ $x: Boolean } as e =>
88
e: Expr[T & Boolean]
99
x: Expr[T & Boolean]
1010
e
1111

12-
case e @ '{ println("hello"); $x } =>
12+
case '{ println("hello"); $x } as e =>
1313
e: Expr[T]
1414
x: Expr[T]
1515
e
1616

17-
case e @ '{ println("hello"); $x: T } =>
17+
case '{ println("hello"); $x: T } as e =>
1818
e: Expr[T]
1919
x: Expr[T]
2020
e
2121

22-
case e @ '{ Some($x: Int) } =>
22+
case '{ Some($x: Int) } as e =>
2323
e: Expr[T & Some[Int]]
2424
x: Expr[Int]
2525
e
2626

27-
case e @ '{ if ($x) ($y: Boolean) else ($z: Int) } =>
27+
case '{ if ($x) ($y: Boolean) else ($z: Int) } as e =>
2828
e: Expr[T & (Boolean | Int)]
2929
y: Expr[T & Boolean]
3030
z: Expr[T & Int]
3131
e
3232

33-
case e @ '{ if ($x) $y else $z } =>
33+
case '{ if ($x) $y else $z } as e =>
3434
e: Expr[T]
3535
y: Expr[T]
3636
z: Expr[T]
3737
e
3838

39-
case e @ '{ if ($x) $y else ($z: Int) } =>
39+
case '{ if ($x) $y else ($z: Int) } as e =>
4040
e: Expr[T & (T | Int)]
4141
y: Expr[T]
4242
z: Expr[T & Int]

tests/pos-special/strawman-collections/CollectionStrawMan1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ object CollectionStrawMan1 {
190190
Array.copy(fst.elems, fst.start, elems, 0, fst.remaining)
191191
Array.copy(snd.elems, snd.start, elems, fst.remaining, snd.remaining)
192192
new ArrayBuffer(elems, elems.length)
193-
case it @ Iterator.Partition(underlying, _, buf, _) =>
193+
case Iterator.Partition(underlying, _, buf, _) as it =>
194194
while (underlying.hasNext) it.distribute()
195195
buf.asInstanceOf[ArrayBuffer[B]]
196196
case it if it.remaining >= 0 =>

tests/pos-special/strawman-collections/CollectionStrawMan4.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ object CollectionStrawMan4 {
216216

217217
object ListBuffer extends IterableFactory[ListBuffer] {
218218
def fromIterable[B](coll: Iterable[B]): ListBuffer[B] = coll match {
219-
case pd @ View.Partitioned(partition: View.Partition[B] @unchecked) =>
219+
case View.Partitioned(partition: View.Partition[B] @unchecked) as pd =>
220220
partition.distribute(new ListBuffer[B]())
221221
new ListBuffer[B] ++= pd.forced.get
222222
case _ =>
@@ -267,7 +267,7 @@ object CollectionStrawMan4 {
267267
Array.copy(fst.elems, fst.start, elems, 0, fst.length)
268268
Array.copy(snd.elems, snd.start, elems, fst.length, snd.length)
269269
new ArrayBuffer(elems, elems.length)
270-
case pd @ View.Partitioned(partition: View.Partition[B] @unchecked) =>
270+
case View.Partitioned(partition: View.Partition[B] @unchecked) as pd =>
271271
partition.distribute(new ArrayBuffer[B]())
272272
pd.forced.get.asInstanceOf[ArrayBuffer[B]]
273273
case c if c.knownLength >= 0 =>

tests/pos-with-compiler/Patterns.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object Patterns {
1313
}
1414
d match {
1515
case WildcardType(bounds: TypeBounds) => bounds.lo
16-
case a @ Assign(Ident(id), rhs) => id
16+
case Assign(Ident(id), rhs) as a => id
1717
case a: Object => a
1818
}
1919

@@ -42,7 +42,7 @@ object Patterns {
4242
case List(x, z) => x
4343
case List(x) => x
4444
case List() => ""
45-
case x @ _ => "wildcard"
45+
case _ as x => "wildcard"
4646
}
4747
val yy: String = y
4848
}

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 ArrayIterator(elems2, len2) as thatIterator = fromIterator(that.iterator)
197197
if (len == 0) thatIterator
198198
else if (len2 == 0) this
199199
else {

tests/pos/given-pattern.scala

Lines changed: 3 additions & 3 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]
9+
case given Ordering[T] as ord => new TreeSet[T]
1010
case given Ordering[T] => new TreeSet[T]
1111
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 (given Ordering[T] as y, _) => 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 given Ordering[T] as y <- xs
2020
yield new TreeSet[T]
2121
}
2222
def f3[T](x: Ordering[T]) = (x, x) match {

0 commit comments

Comments
 (0)