-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #6849: support irrefutable sequence match #6850
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
Changes from all commits
f824f25
bce5daf
7dd000a
6e85212
0b7cf57
87a6391
a2038ae
38873e0
3ed9147
8ceb525
a601d9d
435f02f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,3 +24,5 @@ macros-in-same-project1 | |
i5257.scala | ||
i7868.scala | ||
enum-java | ||
zero-arity-case-class.scala | ||
tuple-ops.scala |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
8: Pattern Match Exhaustivity: Foo() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
sealed class Foo(val value: Int) | ||
|
||
object Foo { | ||
def unapplySeq(foo: Foo): Seq[Int] = List(foo.value) | ||
} | ||
|
||
def foo(x: Foo): Unit = | ||
x match { | ||
case Foo(x, _: _*) => assert(x == 3) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
22: Pattern Match Exhaustivity: _: Base | ||
65: Pattern Match Exhaustivity: _: M |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
sealed trait Base | ||
sealed class A(s: String) extends Base | ||
sealed class B(val n: Int) extends Base | ||
sealed case class C(val s: String, val n: Int) extends Base | ||
|
||
// boolean | ||
object ExTrue { | ||
def unapply(b: Base): true = b match { | ||
case _ => true | ||
} | ||
|
||
def test(b: Base) = b match { | ||
case ExTrue() => | ||
} | ||
} | ||
|
||
object ExFalse { | ||
def unapply(b: Base): Boolean = b match { | ||
case _ => true | ||
} | ||
|
||
def test(b: Base) = b match { // warning | ||
case ExFalse() => | ||
} | ||
} | ||
|
||
// product | ||
|
||
object ExProduct { | ||
def unapply(b: B): (Int, Int) = (b.n, b.n * b.n) | ||
def test(b: B) = b match { | ||
case ExProduct(x, xx) => | ||
} | ||
} | ||
|
||
// isEmpty/get | ||
|
||
trait Res { | ||
def isEmpty: false = false | ||
def get: Int | ||
} | ||
|
||
object ExName { | ||
def unapply(b: B): Res = new Res { def get: Int = b.n } | ||
|
||
def test(b: B) = b match { | ||
case ExName(x) => | ||
} | ||
} | ||
|
||
sealed class M(n: Int, s: String) extends Product { | ||
def _1: Int = n | ||
def _2: String = s | ||
def isEmpty: Boolean = s.size > n | ||
def get: M = this | ||
|
||
def canEqual(that: Any): Boolean = true | ||
def productArity: Int = 2 | ||
def productElement(n: Int): Any = ??? | ||
} | ||
|
||
object ExM { | ||
def unapply(m: M): M = m | ||
|
||
def test(m: M) = m match { // warning | ||
case ExM(s) => | ||
} | ||
} | ||
|
||
// some | ||
object ExSome { | ||
def unapply(b: B): Some[Int] = Some(b.n) | ||
|
||
def test(b: B) = b match { | ||
case ExSome(x) => | ||
} | ||
} | ||
|
||
object ExSome2 { | ||
def unapply(c: C): Some[C] = Some(c) | ||
|
||
def test(c: C) = c match { | ||
case ExSome2(s, n) => | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
13: Pattern Match Exhaustivity: Node2() | ||
17: Pattern Match Exhaustivity: Node1(Foo(List(_, _: _*))), Node1(Foo(Nil)), Node2() | ||
21: Pattern Match Exhaustivity: Node1(Foo(Nil)), Node2() | ||
17: Pattern Match Exhaustivity: Node1(Foo(_, _: _*)), Node1(Foo()), Node2() | ||
21: Pattern Match Exhaustivity: Node1(Foo()), Node2() |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
final class Foo(val value: Int) | ||
|
||
object Foo { | ||
def unapplySeq(foo: Foo): Seq[Int] = List(foo.value) | ||
} | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
(new Foo(3)) match { | ||
case Foo(x, _: _*) => | ||
assert(x == 3) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
object A { | ||
def unapplySeq(a: Any): Seq[_] = "" | ||
} | ||
|
||
def unapply(x: Any) = x match { case A() => } |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
final class Foo(val value: Int) | ||
|
||
object Foo { | ||
def unapplySeq(foo: Foo): Seq[Int] = List(foo.value) | ||
} | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
(new Foo(3)) match { | ||
case Foo(x, _: _*) => | ||
assert(x == 3) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
final class Foo(val value: Int) | ||
|
||
object Foo { | ||
def unapplySeq(foo: Foo): (Int, Seq[Int]) = (foo.value, Nil) | ||
} | ||
|
||
object Test { | ||
def main(args: Array[String]): Unit = { | ||
(new Foo(3)) match { | ||
case Foo(x, _: _*) => | ||
assert(x == 3) | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
final class StringExtract(val s: String) extends AnyVal { | ||
def isEmpty = (s eq null) || (s == "") | ||
def get = this | ||
def length = s.length | ||
def lengthCompare(n: Int) = s.length compare n | ||
def apply(idx: Int): Char = s charAt idx | ||
|
@@ -13,7 +11,6 @@ final class StringExtract(val s: String) extends AnyVal { | |
} | ||
|
||
final class ThreeStringExtract(val s: String) extends AnyVal { | ||
def isEmpty = (s eq null) || (s == "") | ||
def get: (List[Int], Double, Seq[Char]) = ((s.length :: Nil, s.length.toDouble, toSeq)) | ||
def length = s.length | ||
def lengthCompare(n: Int) = s.length compare n | ||
|
@@ -28,10 +25,14 @@ final class ThreeStringExtract(val s: String) extends AnyVal { | |
|
||
|
||
object Bippy { | ||
def unapplySeq(x: Any): StringExtract = new StringExtract("" + x) | ||
def unapplySeq(x: Any): Option[StringExtract] = | ||
if ((x == null) || (x == "")) None | ||
else Some(new StringExtract("" + x)) | ||
} | ||
object TripleBippy { | ||
def unapplySeq(x: Any): ThreeStringExtract = new ThreeStringExtract("" + x) | ||
def unapplySeq(x: Any): Option[(List[Int], Double, Seq[Char])] = | ||
if ((x == null) || (x == "")) then None | ||
else Some(new ThreeStringExtract("" + x).get) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should add extra tests rather than modify the old ones There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We added extra tests in other files. This test has to be adapted, as it does not conform to the protocol: the result type conforms to sequence match, thus it is irrefutable. |
||
} | ||
|
||
object Test { | ||
|
Uh oh!
There was an error while loading. Please reload this page.