Skip to content

Commit 94a93e5

Browse files
liufengyunallanrenucci
authored andcommitted
address review
1 parent 255c5bb commit 94a93e5

File tree

3 files changed

+44
-12
lines changed

3 files changed

+44
-12
lines changed

compiler/src/dotty/tools/dotc/transform/PatternMatcher.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ object PatternMatcher {
254254
*/
255255
def matchElemsPlan(seqSym: Symbol, args: List[Tree], exact: Boolean, onSuccess: Plan) = {
256256
val selectors = args.indices.toList.map(idx =>
257-
ref(seqSym).select(nme.apply).appliedTo(Literal(Constant(idx))))
257+
ref(seqSym).select(defn.Seq_apply.matchingMember(seqSym.info)).appliedTo(Literal(Constant(idx))))
258258
TestPlan(LengthTest(args.length, exact), seqSym, seqSym.pos,
259259
matchArgsPlan(selectors, args, onSuccess))
260260
}

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

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,25 +109,21 @@ object Applications {
109109

110110
val elemTp = getTp.member(nme.apply).suchThat(_.info <:< applyTp(WildcardType)).info.resultType
111111

112-
def names1 = List(nme.lengthCompare, nme.apply, nme.drop, nme.toSeq)
113-
def types1 = List(lengthCompareTp, applyTp(elemTp), dropTp(elemTp), toSeqTp(elemTp))
114-
115-
def names2 = List(nme.length, nme.apply, nme.drop, nme.toSeq)
116-
def types2 = List(lengthTp, applyTp(elemTp), dropTp(elemTp), toSeqTp(elemTp))
112+
def test(name: Name, tp: Type) = getTp.member(name).suchThat(_.info <:< tp).exists
117113

118114
val valid = elemTp.exists &&
119-
(getTp <:< RefinedType.make(defn.AnyType, names1, types1) ||
120-
getTp <:< RefinedType.make(defn.AnyType, names2, types2))
115+
(test(nme.lengthCompare, lengthCompareTp) || test(nme.length, lengthTp)) &&
116+
test(nme.drop, dropTp(elemTp)) &&
117+
test(nme.toSeq, toSeqTp(elemTp))
121118

122119
if (valid) elemTp else NoType
123120
}
124121

125-
def validUnapplySeqType(getTp: Type): Boolean = unapplySeqTypeElemTp(getTp).exists
126-
127122
if (unapplyName == nme.unapplySeq) {
128-
if (isGetMatch(unapplyResult, pos) && validUnapplySeqType(getTp)) {
123+
if (isGetMatch(unapplyResult, pos)) {
129124
val elemTp = unapplySeqTypeElemTp(getTp)
130-
args.map(Function.const(elemTp))
125+
if (elemTp.exists) args.map(Function.const(elemTp))
126+
else fail
131127
}
132128
else fail
133129
}

tests/run/i4984e.scala

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
object Array2 {
2+
def unapplySeq(x: Array[Int]): Data = new Data
3+
4+
final class Data {
5+
def isEmpty: Boolean = false
6+
def get: Data = this
7+
def lengthCompare(len: Int): Int = 0
8+
def lengthCompare: Int = 0
9+
def apply(i: Int): Int = 3
10+
def apply(i: String): Int = 3
11+
def drop(n: Int): scala.Seq[Int] = Seq(2, 5)
12+
def drop: scala.Seq[Int] = Seq(2, 5)
13+
def toSeq: scala.Seq[Int] = Seq(6, 7)
14+
def toSeq(x: Int): scala.Seq[Int] = Seq(6, 7)
15+
}
16+
}
17+
18+
object Test {
19+
def test1(xs: Array[Int]): Int = xs match {
20+
case Array2(x, y) => x + y
21+
}
22+
23+
def test2(xs: Array[Int]): Seq[Int] = xs match {
24+
case Array2(x, y, xs:_*) => xs
25+
}
26+
27+
def test3(xs: Array[Int]): Seq[Int] = xs match {
28+
case Array2(xs:_*) => xs
29+
}
30+
31+
def main(args: Array[String]): Unit = {
32+
test1(Array(3, 5))
33+
test2(Array(3, 5))
34+
test3(Array(3, 5))
35+
}
36+
}

0 commit comments

Comments
 (0)