Skip to content
This repository was archived by the owner on Sep 1, 2020. It is now read-only.

Commit 0bc7fa6

Browse files
committed
Merge pull request scala#4537 from som-snytt/issue/xml-unapply
SI-9343 Xlint less strict on pattern sequences
2 parents 70f0b1d + 79436ca commit 0bc7fa6

File tree

3 files changed

+17
-21
lines changed

3 files changed

+17
-21
lines changed

src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,10 @@ trait ScalacPatternExpanders {
112112
arityError("not enough")
113113
else if (elementArity > 0 && !isSeq)
114114
arityError("too many")
115-
else if (settings.warnStarsAlign && isSeq && productArity > 0 && (elementArity > 0 || !isStar))
116-
warn("A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).")
115+
else if (settings.warnStarsAlign && isSeq && productArity > 0 && elementArity > 0) warn {
116+
if (isStar) "Sequence wildcard (_*) does not align with repeated case parameter or extracted sequence; the result may be unexpected."
117+
else "A repeated case parameter or extracted sequence is not matched by a sequence wildcard (_*), and may fail at runtime."
118+
}
117119

118120
aligned
119121
}

test/files/neg/t7623.check

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,15 @@
1-
t7623.scala:19: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
2-
def f = "" match { case X(s) => }
1+
t7623.scala:21: warning: A repeated case parameter or extracted sequence is not matched by a sequence wildcard (_*), and may fail at runtime.
2+
def g = "" match { case X(s, t) => } // warn
33
^
4-
t7623.scala:21: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
5-
def g = "" match { case X(s, t) => }
4+
t7623.scala:23: warning: Sequence wildcard (_*) does not align with repeated case parameter or extracted sequence; the result may be unexpected.
5+
def h = "" match { case X(s, t, u @ _*) => } // warn
66
^
7-
t7623.scala:23: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
8-
def h = "" match { case X(s, t, u @ _*) => }
9-
^
10-
t7623.scala:9: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
11-
def f = C("") match { case C(s) => }
12-
^
13-
t7623.scala:11: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
14-
def g = C("") match { case C(s, t) => }
7+
t7623.scala:11: warning: A repeated case parameter or extracted sequence is not matched by a sequence wildcard (_*), and may fail at runtime.
8+
def g = C("") match { case C(s, t) => } // warn
159
^
16-
t7623.scala:13: warning: A repeated case parameter or extracted sequence should be matched only by a sequence wildcard (_*).
17-
def h = C("") match { case C(s, t, u @ _*) => }
10+
t7623.scala:13: warning: Sequence wildcard (_*) does not align with repeated case parameter or extracted sequence; the result may be unexpected.
11+
def h = C("") match { case C(s, t, u @ _*) => } // warn
1812
^
1913
error: No warnings can be incurred under -Xfatal-warnings.
20-
6 warnings found
14+
four warnings found
2115
one error found

test/files/neg/t7623.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,19 @@ object X { def unapplySeq(a: Any): Option[(String, Seq[Int])] = Some("", List(1,
88
trait Ctest {
99
def f = C("") match { case C(s) => }
1010

11-
def g = C("") match { case C(s, t) => }
11+
def g = C("") match { case C(s, t) => } // warn
1212

13-
def h = C("") match { case C(s, t, u @ _*) => }
13+
def h = C("") match { case C(s, t, u @ _*) => } // warn
1414

1515
def ok = C("") match { case C(s, u @ _*) => }
1616
}
1717
// for extractors that unapplySeq: Option[(Something, Seq[_])], avoid misaligned patterns
1818
trait Xtest {
1919
def f = "" match { case X(s) => }
2020

21-
def g = "" match { case X(s, t) => }
21+
def g = "" match { case X(s, t) => } // warn
2222

23-
def h = "" match { case X(s, t, u @ _*) => }
23+
def h = "" match { case X(s, t, u @ _*) => } // warn
2424

2525
def ok = "" match { case X(s, u @ _*) => }
2626
}

0 commit comments

Comments
 (0)