Skip to content

Drop as in patterns #10565

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
}
}
compareTypeLambda
case tp2 as OrType(tp21, tp22) =>
case tp2 @ OrType(tp21, tp22) =>
compareAtoms(tp1, tp2) match
case Some(b) => return b
case _ =>
Expand Down Expand Up @@ -970,7 +970,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling
* corresponding arguments are subtypes relative to their variance (see `isSubArgs`).
*/
def isMatchingApply(tp1: Type): Boolean = tp1 match {
case tp1 as AppliedType(tycon1, args1) =>
case tp1 @ AppliedType(tycon1, args1) =>
// We intentionally do not automatically dealias `tycon1` or `tycon2` here.
// `TypeApplications#appliedTo` already takes care of dealiasing type
// constructors when this can be done without affecting type
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ object TypeOps:
tp.derivedAlias(simplify(tp.alias, theMap))
case AndType(l, r) if !ctx.mode.is(Mode.Type) =>
simplify(l, theMap) & simplify(r, theMap)
case tp as OrType(l, r)
case tp @ OrType(l, r)
if !ctx.mode.is(Mode.Type)
&& (tp.isSoft || l.isBottomType || r.isBottomType) =>
// Normalize A | Null and Null | A to A even if the union is hard (i.e.
Expand Down
8 changes: 2 additions & 6 deletions compiler/src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2608,10 +2608,7 @@ object Parsers {
/** Pattern2 ::= [id `as'] InfixPattern
*/
val pattern2: () => Tree = () => infixPattern() match {
case p @ Ident(name) if in.token == AT || in.isIdent(nme.as) =>
if in.token == AT && sourceVersion.isAtLeast(`3.1`) then
deprecationWarning(s"`@` bindings have been deprecated; use `as` instead", in.offset)

case p @ Ident(name) if in.token == AT =>
val offset = in.skipToken()
infixPattern() match {
case pt @ Ident(tpnme.WILDCARD_STAR) => // compatibility for Scala2 `x @ _*` syntax
Expand All @@ -2638,8 +2635,7 @@ object Parsers {
/** InfixPattern ::= SimplePattern {id [nl] SimplePattern}
*/
def infixPattern(): Tree =
infixOps(simplePattern(), in.canStartExprTokens, simplePattern,
isOperator = in.name != nme.raw.BAR && in.name != nme.as)
infixOps(simplePattern(), in.canStartExprTokens, simplePattern, isOperator = in.name != nme.raw.BAR)

/** SimplePattern ::= PatVar
* | Literal
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ class RefinedPrinter(_ctx: Context) extends PlainPrinter(_ctx) {
// of AndType and OrType to account for associativity
case AndType(tp1, tp2) =>
toTextInfixType(tpnme.raw.AMP, tp1, tp2) { toText(tpnme.raw.AMP) }
case tp as OrType(tp1, tp2) =>
case tp @ OrType(tp1, tp2) =>
toTextInfixType(tpnme.raw.BAR, tp1, tp2) {
if tp.isSoft && printDebug then toText(tpnme.ZOR) else toText(tpnme.raw.BAR)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ class SpaceEngine(using Context) extends SpaceLogic {
else args.map(arg => erase(arg, inArray = false))
tp.derivedAppliedType(erase(tycon, inArray), args2)

case tp as OrType(tp1, tp2) =>
case tp @ OrType(tp1, tp2) =>
OrType(erase(tp1, inArray), erase(tp2, inArray), tp.isSoft)

case AndType(tp1, tp2) =>
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ trait Applications extends Compatibility {
args match {
case arg :: Nil if isVarArg(arg) =>
addTyped(arg, formal)
case (arg as Typed(Literal(Constant(null)), _)) :: Nil if ctx.isAfterTyper =>
case (arg @ Typed(Literal(Constant(null)), _)) :: Nil if ctx.isAfterTyper =>
addTyped(arg, formal)
case _ =>
val elemFormal = formal.widenExpr.argTypesLo.head
Expand Down
2 changes: 1 addition & 1 deletion docs/docs/internals/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ TypeCaseClause ::= ‘case’ InfixType ‘=>’ Type [nl]

Pattern ::= Pattern1 { ‘|’ Pattern1 } Alternative(pats)
Pattern1 ::= Pattern2 [‘:’ RefinedType] Bind(name, Typed(Ident(wildcard), tpe))
Pattern2 ::= [id ‘as’] InfixPattern Bind(name, pat)
Pattern2 ::= [id ‘@’] InfixPattern Bind(name, pat)
InfixPattern ::= SimplePattern { id [nl] SimplePattern } InfixOp(pat, op, pat)
SimplePattern ::= PatVar Ident(wildcard)
| Literal Bind(name, Ident(wildcard))
Expand Down
4 changes: 2 additions & 2 deletions docs/docs/reference/contextual/givens.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,13 @@ Since `mkAnnotations` is `transparent`, the type of an application is the type o

## Pattern-Bound Given Instances

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

```scala
for given Context <- applicationContexts do

pair match
case (ctx as given Context, y) => ...
case (ctx @ given Context, y) => ...
```
In the first fragment above, anonymous given instances for class `Context` are established by enumerating over `applicationContexts`. In the second fragment, a given `Context`
instance named `ctx` is established by matching against the first half of the `pair` selector.
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/quoted/ExprMap.scala
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ trait ExprMap:
tree
case Super(qual, mix) =>
tree
case tree as Apply(fun, args) =>
case tree @ Apply(fun, args) =>
val MethodType(_, tpes, _) = fun.tpe.widen
Apply.copy(tree)(transformTerm(fun, TypeRepr.of[Any])(owner), transformTerms(args, tpes)(owner))
case TypeApply(fun, args) =>
Expand Down
4 changes: 2 additions & 2 deletions tests/init/crash/fors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object Test extends App {
var n = 0
for (_ <- xs) n += 1; println(n)
for ((x, y) <- xs zip ys) print(x + " "); println()
for (p as (x, y) <- xs zip ys) print(p._1 + " "); println()
for (p @ (x, y) <- xs zip ys) print(p._1 + " "); println()

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

// iterators
for (x <- it) print(x + " "); println()
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/Iter3.scala
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ object Iter2 {
flatten(map(f(_).buildIterator))

override def ++[B >: A](that: IterableOnce[B]): ArrayIterator[B] = {
val thatIterator as ArrayIterator(elems2, len2) = fromIterator(that.iterator)
val thatIterator @ ArrayIterator(elems2, len2) = fromIterator(that.iterator)
if (len == 0) thatIterator
else if (len2 == 0) this.asInstanceOf[ArrayIterator[B]]
else {
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/ensureReported.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object AnonymousF {
val f = {
case l as List(1) => // error: missing parameter type
case l @ List(1) => // error: missing parameter type
Some(l)
}
}
2 changes: 1 addition & 1 deletion tests/neg/i1716.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Fail {
def f(m: Option[Int]): Unit = {
m match {
case x as Some[_] => // error: unbound wildcard type
case x @ Some[_] => // error: unbound wildcard type
case _ =>
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/neg/i3200.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object Test {
case object Bob { override def equals(other: Any) = true }
def main(args: Array[String]): Unit = {
val m : Bob.type = (5: Any) match { case x as Bob => x } // error
val m : Bob.type = (5: Any) match { case x @ Bob => x } // error
}
}
8 changes: 4 additions & 4 deletions tests/neg/i3200b.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
object Test {
def main(args: Array[String]): Unit = {
val a: Nil.type = (Vector(): Any) match { case n as Nil => n } // error
val b: Nil.type = (Vector(): Any) match { case n as (m as Nil) => n } // error
val c: Int = (1.0: Any) match { case n as 1 => n } // error
val d: Int = (1.0: Any) match { case n as (m as 1) => n } // error
val a: Nil.type = (Vector(): Any) match { case n @ Nil => n } // error
val b: Nil.type = (Vector(): Any) match { case n @ (m @ Nil) => n } // error
val c: Int = (1.0: Any) match { case n @ 1 => n } // error
val d: Int = (1.0: Any) match { case n @ (m @ 1) => n } // error
}
}
2 changes: 1 addition & 1 deletion tests/neg/i3332.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ object Test {
case _ => println("nope")
}
def test(x: Any) = x match {
case _: String | _ as A() => 1
case _: String | _ @ A() => 1
}
}
4 changes: 2 additions & 2 deletions tests/neg/i3812b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ object Test {
Some(x) match { case Some(Z2.v) => () } // ok

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

Some(x) match { case Some(4) | Some(Z2.v) => () } // ok
Some(x) match { case a as Some(Z2.v) => () } // ok
Some(x) match { case a @ Some(Z2.v) => () } // ok
}
}
2 changes: 1 addition & 1 deletion tests/neg/i8407.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
object Test:
val xs = List(1, 2, 3, 4, 5)
xs match {
case List(1, 2, xs1 as xs2: _*) => println(xs2) // error // error
case List(1, 2, xs1 @ xs2: _*) => println(xs2) // error // error
case _ => ()
}
2 changes: 1 addition & 1 deletion tests/neg/i8715.scala
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
@main
def Test = List(42) match { case List(xs as (ys: _*)) => xs } // error
def Test = List(42) match { case List(xs @ (ys: _*)) => xs } // error
2 changes: 1 addition & 1 deletion tests/neg/i9310.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//AE-d101cfe6d25117a51897609e673f6c8e74d31e6e
val foo as this = 0
val foo @ this = 0
class Foo {
foo { } // error
}
2 changes: 1 addition & 1 deletion tests/neg/multi-patterns.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
object Test {
val (a :: as), bs = List(1, 2, 3) // error
val B as List(), C: List[Int] = List() // error
val B @ List(), C: List[Int] = List() // error
}
2 changes: 1 addition & 1 deletion tests/neg/patternUnsoundness.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object patternUnsoundness extends App {
val y: C[Object] = x

y match {
case d as D(x) => d.s = new Integer(1) // error
case d @ D(x) => d.s = new Integer(1) // error
}

val z: String = x.s // used to throw ClassCast exception
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/Iter2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ object Iter2 {
flatten(map(f(_).buildIterator))

override def ++[B >: A](that: IterableOnce[B]): ArrayIterator[B] = {
val thatIterator as ArrayIterator(elems2, len2) = fromIterator(that.iterator)
val thatIterator @ ArrayIterator(elems2, len2) = fromIterator(that.iterator)
if (len == 0) thatIterator
else if (len2 == 0) this
else {
Expand Down
10 changes: 5 additions & 5 deletions tests/pos/given-pattern.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ class Test {
transparent inline def trySummon[S, T](f: PartialFunction[S, T]): T = ???

inline def setFor[T]: Set[T] = trySummon {
case ord as given Ordering[T] => new TreeSet[T]
case given Ordering[T] => new TreeSet[T]
case _ => new HashSet[T]
case ord @ given Ordering[T] => new TreeSet[T]
case given Ordering[T] => new TreeSet[T]
case _ => new HashSet[T]
}

def f1[T](x: Ordering[T]) = (x, x) match {
case (y as given Ordering[T], _) => new TreeSet[T]
case (y @ given Ordering[T], _) => new TreeSet[T]
}
def f2[T](x: Ordering[T]) = {
val xs = List(x, x, x)
for y as given Ordering[T] <- xs
for y @ given Ordering[T] <- xs
yield new TreeSet[T]
}
def f3[T](x: Ordering[T]) = (x, x) match {
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i10087.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ def f4[T](x: Ordering[T]) = {
val xs = List(x, x, x)
for given Ordering[T] <- xs
yield new TreeSet[T]
for x as given Ordering[T] <- xs
for x @ given Ordering[T] <- xs
yield new TreeSet[T]
}
2 changes: 1 addition & 1 deletion tests/pos/i1540.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Casey1 { def unapply(a: Casey1) = a }

object Test {
def main(args: Array[String]): Unit = {
val c as Casey1(x) = new Casey1(0)
val c @ Casey1(x) = new Casey1(0)
assert(x == c.get)
}
}
2 changes: 1 addition & 1 deletion tests/pos/i1540b.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object Casey1 { def unapply[T](a: Casey1[T]) = a }

object Test {
def main(args: Array[String]): Unit = {
val c as Casey1(x) = new Casey1(0)
val c @ Casey1(x) = new Casey1(0)
assert(x == c.get)
}
}
2 changes: 1 addition & 1 deletion tests/pos/i3412.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
class Test {
val A as List() = List()
val A @ List() = List()
}
2 changes: 1 addition & 1 deletion tests/pos/i4177.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Test {
val a: PartialFunction[Int, String] = { case Foo(x) => x }
val b: PartialFunction[Int, String] = { case x => x.toString }

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

Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i4564.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object ClashNoSig { // ok
def unapply(x: ClashNoSig) = x

ClashNoSig(2) match {
case c as ClashNoSig(y) => c.copy(y + c._1)
case c @ ClashNoSig(y) => c.copy(y + c._1)
}
}
case class ClashNoSig private (x: Int) {
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/i5402.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ object Main {
case 1 => 1
case 0 | 0 => 0
case 2 | 2 | 2 | 3 | 2 | 3 => 0
case 4 | (_ as 4) => 0
case 4 | (_ @ 4) => 0
case _ => -1
}

Expand Down
4 changes: 2 additions & 2 deletions tests/pos/reference/delegate-match.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ class Test extends App:
import scala.compiletime.summonFrom

transparent inline def setFor[T]: Set[T] = summonFrom {
case ord as given Ordering[T] => new TreeSet[T]
case _ => new HashSet[T]
case ord @ given Ordering[T] => new TreeSet[T]
case _ => new HashSet[T]
}

summon[Ordering[String]]
Expand Down
4 changes: 2 additions & 2 deletions tests/pos/simpleExtractors-2.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class Foo {
def bar(x: Any): Unit = x match {
case Some(Some(i: Int)) => println(i)
case Some(s as Some(i)) => println(s)
case s as Some(r as Some(i)) => println(s)
case Some(s @ Some(i)) => println(s)
case s @ Some(r @ Some(i)) => println(s)
}
}
2 changes: 1 addition & 1 deletion tests/pos/t10533.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object Foo {
val b as Bar(_) = Bar(1)(2)(3)
val b @ Bar(_) = Bar(1)(2)(3)
}

case class Bar(a: Int)(b: Int)(c: Int)
2 changes: 1 addition & 1 deletion tests/pos/t3136.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ object NullaryMethodType {
object Test {
def TEST(tp: Type): String =
tp match {
case PolyType(ps1, PolyType(ps2, res as PolyType(a, b))) => "1" + tp // couldn't find a simpler version that still crashes
case PolyType(ps1, PolyType(ps2, res @ PolyType(a, b))) => "1" + tp // couldn't find a simpler version that still crashes
case NullaryMethodType(meh) => "2" + meh
}
}
2 changes: 1 addition & 1 deletion tests/pos/t6675.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ object LeftOrRight {

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

(Left((0, 0)): Either[(Int, Int), (Int, Int)]) match {
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/trailingCommas/trailingCommas.scala
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ trait SimplePattern {

// test '@' syntax in patterns
Some(1) match {
case Some(x as 1,
case Some(x @ 1,
) => x
}

Expand Down
2 changes: 1 addition & 1 deletion tests/pos/virtpatmat_alts_subst.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
case class Foo(s: String) {
def appliedType(tycon: Any) =
tycon match {
case Foo(sym as ("NothingClass" | "AnyClass")) => println(sym)
case Foo(sym @ ("NothingClass" | "AnyClass")) => println(sym)
}
}
2 changes: 1 addition & 1 deletion tests/run/3179.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
object Test {
def main(args: Array[String]): Unit = {
("": Any) match {
case a as Test => 1
case a @ Test => 1
case _ => 2
}
}
Expand Down
Loading