Skip to content

Commit 59e22a2

Browse files
committed
Add unapplySeq tests
1 parent 6d645b7 commit 59e22a2

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

compiler/src/dotty/tools/dotc/transform/init/Objects.scala

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,7 @@ object Objects:
12011201
* Currently, we assume all cases are reachable, thus all patterns are assumed to match.
12021202
*/
12031203
def evalPattern(scrutinee: Value, pat: Tree): Value = log("match " + scrutinee.show + " against " + pat.show, printer, (_: Value).show):
1204+
val trace2 = Trace.trace.add(pat)
12041205
pat match
12051206
case Alternative(pats) =>
12061207
for pat <- pats do evalPattern(scrutinee, pat)
@@ -1211,11 +1212,9 @@ object Objects:
12111212
initLocal(bind.symbol, value)
12121213
scrutinee
12131214

1214-
case SeqLiteral(pats, _) =>
1215-
// TODO: handle unapplySeq
1216-
Bottom
1217-
12181215
case UnApply(fun, implicits, pats) =>
1216+
given Trace = trace2
1217+
12191218
val fun1 = funPart(fun)
12201219
val funRef = fun1.tpe.asInstanceOf[TermRef]
12211220
val unapplyResTp = funRef.widen.finalResultType
@@ -1300,7 +1299,7 @@ object Objects:
13001299
end if
13011300
scrutinee
13021301

1303-
case Ident(nme.WILDCARD) =>
1302+
case Ident(nme.WILDCARD) | Ident(nme.WILDCARD_STAR) =>
13041303
scrutinee
13051304

13061305
case Typed(pat, _) =>
@@ -1315,7 +1314,7 @@ object Objects:
13151314
/**
13161315
* Evaluate a sequence value against sequence patterns.
13171316
*/
1318-
def evalSeqPatterns(scrutinee: Value, scrutineeType: Type, elemType: Type, pats: List[Tree]): Unit =
1317+
def evalSeqPatterns(scrutinee: Value, scrutineeType: Type, elemType: Type, pats: List[Tree])(using Trace): Unit =
13191318
// call .lengthCompare or .length
13201319
val lengthCompareDenot = getMemberMethod(scrutineeType, nme.lengthCompare, lengthCompareType)
13211320
if lengthCompareDenot.exists then
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Error: tests/init-global/neg/patmat-unapplySeq.scala:8:32 -----------------------------------------------------------
2+
8 | def apply(i: Int): Box = array(i) // error
3+
| ^^^^^^^^
4+
|Reading mutable state of object A during initialization of object B.
5+
|Reading mutable state of other static objects is forbidden as it breaks initialization-time irrelevance. Calling trace:
6+
|-> object B: [ patmat-unapplySeq.scala:15 ]
7+
| ^
8+
|-> case A(b) => [ patmat-unapplySeq.scala:17 ]
9+
| ^^^^
10+
|-> def apply(i: Int): Box = array(i) // error [ patmat-unapplySeq.scala:8 ]
11+
| ^^^^^^^^
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object A:
2+
class Box(var x: Int)
3+
4+
val array: Array[Box] = new Array(1)
5+
array(0) = new Box(10)
6+
7+
def length: Int = array.length
8+
def apply(i: Int): Box = array(i) // error
9+
def drop(n: Int): Seq[Box] = array.toSeq
10+
def toSeq: Seq[Box] = array.toSeq
11+
12+
def unapplySeq(array: Array[Box]): A.type = this
13+
14+
15+
object B:
16+
A.array match
17+
case A(b) =>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
object A:
2+
class Box(var x: Int)
3+
4+
val array: Array[Box] = new Array(1)
5+
array(0) = new Box(10)
6+
7+
def length: Int = array.length
8+
def apply(i: Int): Box = array(i) // error
9+
def drop(n: Int): Seq[Box] = array.toSeq
10+
def toSeq: Seq[Box] = array.toSeq
11+
12+
def unapplySeq(array: Array[Box]): A.type = this
13+
14+
15+
object B:
16+
A.array match
17+
case A(b*) =>

0 commit comments

Comments
 (0)