Skip to content

Commit e0256bd

Browse files
committed
Enhance pattern matching with capturing types; fix stdlib-cc
1 parent c535dbc commit e0256bd

File tree

4 files changed

+29
-3
lines changed

4 files changed

+29
-3
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ import Flags.*, Constants.*
1414
import Decorators.*
1515
import NameKinds.{PatMatStdBinderName, PatMatAltsName, PatMatResultName}
1616
import config.Printers.patmatch
17+
import config.Feature
1718
import reporting.*
1819
import ast.*
1920
import util.Property.*
21+
import cc.{CapturingType, Capabilities}
2022

2123
import scala.annotation.tailrec
2224
import scala.collection.mutable
@@ -427,8 +429,11 @@ object PatternMatcher {
427429
&& !hasExplicitTypeArgs(extractor)
428430
case _ => false
429431
}
432+
val castTp = if Feature.ccEnabled
433+
then CapturingType(tpt.tpe, scrutinee.termRef.singletonCaptureSet)
434+
else tpt.tpe
430435
TestPlan(TypeTest(tpt, isTrusted), scrutinee, tree.span,
431-
letAbstract(ref(scrutinee).cast(tpt.tpe)) { casted =>
436+
letAbstract(ref(scrutinee).cast(castTp)) { casted =>
432437
nonNull += casted
433438
patternPlan(casted, pat, onSuccess)
434439
})

scala2-library-cc/src/scala/collection/IndexedSeqView.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ object IndexedSeqView {
160160

161161
@SerialVersionUID(3L)
162162
class Reverse[A](underlying: SomeIndexedSeqOps[A]^) extends SeqView.Reverse[A](underlying) with IndexedSeqView[A] {
163-
override def reverse: IndexedSeqView[A] = underlying match {
163+
override def reverse: IndexedSeqView[A]^{underlying} = underlying match {
164164
case x: IndexedSeqView[A] => x
165165
case _ => super.reverse
166166
}

scala2-library-cc/src/scala/collection/mutable/CheckedIndexedSeqView.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ private[mutable] object CheckedIndexedSeqView {
101101
@SerialVersionUID(3L)
102102
class Reverse[A](underlying: SomeIndexedSeqOps[A]^)(protected val mutationCount: () ->{cap.rd} Int)
103103
extends IndexedSeqView.Reverse[A](underlying) with CheckedIndexedSeqView[A] {
104-
override def reverse: IndexedSeqView[A] = underlying match {
104+
override def reverse: IndexedSeqView[A]^{underlying} = underlying match {
105105
case x: IndexedSeqView[A] => x
106106
case _ => super.reverse
107107
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import language.experimental.captureChecking
2+
3+
trait A
4+
5+
case class B(x: AnyRef^) extends A
6+
7+
def test =
8+
val x: AnyRef^ = new AnyRef
9+
val a: A^{x} = B(x)
10+
11+
val y1: A = a match
12+
case b: B => b // error: (b: B) becomes B^{x} implicitly
13+
14+
val y2: A^{x} = a match
15+
case b: B => b // ok
16+
17+
val x3: AnyRef = a match
18+
case B(x2: AnyRef) => x2 // error: we lose some information about field x, but it still cannot be pure
19+
20+
val x4: AnyRef = a match
21+
case b: B => b.x // error

0 commit comments

Comments
 (0)