Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
virtpatmat, hidden behind -Yvirtpatmat
at least one imminent TODO: undo hardwired generation of if/then/else, and decide based on type whether to call flatMap/orElse or inline those methods from Option review by extempore
- Loading branch information
Showing
51 changed files
with
1,238 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
854 changes: 854 additions & 0 deletions
854
src/compiler/scala/tools/nsc/typechecker/PatMatVirtualiser.scala
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package scala | ||
|
||
abstract class MatchingStrategy[M[+x]] { | ||
def zero: M[Nothing] | ||
def one[T](x: T): M[T] | ||
def guard[T](cond: Boolean, then: => T): M[T] // = if(cond) one(then) else zero | ||
def altFlatMap[T, U](f: T => M[U])(a: M[U], b: M[T]): M[U] // = a orElse b.flatMap(f) -- can't easily&efficiently express M[T] should have flatMap and orElse | ||
def runOrElse[T, U](x: T)(f: T => M[U]): U | ||
def isSuccess[T, U](x: T)(f: T => M[U]): Boolean | ||
|
||
// find the first alternative to successfully flatMap f | ||
// to avoid code explosion due to alternatives | ||
def or[T, U](f: T => M[U], alts: M[T]*) = (alts foldLeft (zero: M[U]))(altFlatMap(f)) | ||
|
||
def caseResult[T](x: T): M[T] = one(x) // used as a marker to distinguish the RHS of a case (case pat => RHS) and intermediate successes | ||
// when deriving a partial function from a pattern match, we need to | ||
// distinguish the RHS of a case, which should not be evaluated when computing isDefinedAt, | ||
// from an intermediate result (which must be computed) | ||
|
||
} | ||
|
||
object MatchingStrategy { | ||
implicit object OptionMatchingStrategy extends MatchingStrategy[Option] { | ||
type M[+x] = Option[x] | ||
@inline def guard[T](cond: Boolean, then: => T): M[T] = if(cond) Some(then) else None | ||
@inline def zero: M[Nothing] = None | ||
@inline def one[T](x: T): M[T] = Some(x) | ||
@inline def altFlatMap[T, U](f: T => M[U])(a: M[U], b: M[T]): M[U] = a orElse b.flatMap(f) | ||
@inline def runOrElse[T, U](x: T)(f: T => M[U]): U = f(x) getOrElse (throw new MatchError(x)) | ||
@inline def isSuccess[T, U](x: T)(f: T => M[U]): Boolean = !f(x).isEmpty | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class IntMap[+V] | ||
case class Bin[+T](m: IntMap[T]) extends IntMap[T] | ||
case class Tip[+T](x: T) extends IntMap[T] | ||
|
||
trait IntMapIterator[V, T] { | ||
def valueOf(tip: Tip[V]): T | ||
def pop: IntMap[V] | ||
|
||
def next: T = | ||
pop match { | ||
case Bin(t@Tip(_)) => { | ||
valueOf(t) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import annotation.unchecked.{ uncheckedVariance=> uV } | ||
import scala.collection.immutable.{ListMap, HashMap, ListSet, HashSet} | ||
|
||
object Test { | ||
class HashMapCollision1[A, +B](var hash: Int, var kvs: ListMap[A, B @uV]) extends HashMap[A, B @uV] | ||
class HashSetCollision1[A](var hash: Int, var ks: ListSet[A]) extends HashSet[A] | ||
|
||
def splitArray[T](ad: Array[Iterable[T]]): Any = | ||
ad(0) match { | ||
case _: HashMapCollision1[_, _] | _: HashSetCollision1[_] => null | ||
} | ||
|
||
// without type ascription for the one in the body of the last flatmap of each alternative, type inference borks on the existentials | ||
// def splitArray[T >: Nothing <: Any](ad: Array[Iterable[T]]): Any = { import OptionMatching._ | ||
// runOrElse(ad.apply(0))(((x1: Iterable[T]) => ( | ||
// or(((x4: Iterable[T]) => one(null)), | ||
// guard(x1.isInstanceOf[Iterable[T] with Test.HashMapCollision1[_,_]], x1.asInstanceOf[Iterable[T] with Test.HashMapCollision1[_,_]]).flatMap(((x2: Iterable[T] with Test.HashMapCollision1[_,_]) => one(x2))), | ||
// guard(x1.isInstanceOf[Test.HashSetCollision1[_]], x1.asInstanceOf[Iterable[T] with Test.HashSetCollision1[_]]).flatMap(((x3: Iterable[T] with Test.HashSetCollision1[_]) => one(x3)))): Option[Any]).orElse( | ||
// (zero: Option[Any]))) | ||
// ) | ||
// } | ||
|
||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class ParseResult[+T] | ||
case class MemoEntry[+T](var r: Either[Nothing,ParseResult[_]]) | ||
|
||
object Test { | ||
def grow[T]: ParseResult[T] = (null: MemoEntry[T]) match { | ||
case MemoEntry(Right(x: ParseResult[_])) => x.asInstanceOf[ParseResult[T]] | ||
} | ||
|
||
// what's the _$1 doing there? | ||
// def grow[T >: Nothing <: Any]: ParseResult[T] = { | ||
// import OptionMatching._ | ||
// runOrElse[MemoEntry[T], ParseResult[T]]((null: MemoEntry[T]))(((x1: MemoEntry[T]) => | ||
// (MemoEntry.unapply[T](x1).flatMap[ParseResult[T]](((x4: Either[Nothing,ParseResult[_]]) => | ||
// guard[Right[Nothing,ParseResult[_]]](x4.isInstanceOf[Right[Nothing,ParseResult[_]]], x4.asInstanceOf[Right[Nothing,ParseResult[_]]]).flatMap[ParseResult[T]](((cp3: Right[Nothing,ParseResult[_]]) => | ||
// scala.Right.unapply[Nothing, ParseResult[_]](cp3).flatMap[ParseResult[T]](((x5: ParseResult[_]) => | ||
// guard[ParseResult[_$1]](x5.ne(null), x5.asInstanceOf[ParseResult[_]]).flatMap[ParseResult[T]](((x6: ParseResult[_]) => | ||
// one[ParseResult[T]](x6.asInstanceOf[ParseResult[T]]))))))))): Option[ParseResult[T]] | ||
// ).orElse[ParseResult[T]]((zero: Option[ParseResult[T]])))) | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
class ReferenceQueue[T] { | ||
def wrapper(jref: ReferenceQueue[_]): ReferenceQueue[T] = | ||
jref match { | ||
case null => null | ||
} | ||
|
||
// def wrapper(jref: ReferenceQueue[_]): ReferenceQueue[T] = OptionMatching.runOrElse(jref)(((x1: ReferenceQueue[_]) => | ||
// (OptionMatching.guard(null.==(x1), x1.asInstanceOf[ReferenceQueue[_]]).flatMap(((x2: ReferenceQueue[_]) => | ||
// OptionMatching.one(null))): Option[ReferenceQueue[T]]).orElse( | ||
// (OptionMatching.zero: Option[ReferenceQueue[T]]))) | ||
// ) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import scala.collection.mutable._ | ||
object Test { | ||
def genericArrayOps[T](xs: Array[T]): ArrayOps[T] = xs match { | ||
case x: Array[AnyRef] => refArrayOps[AnyRef](x).asInstanceOf[ArrayOps[T]] | ||
case null => null | ||
} | ||
// def genericArrayOps[T >: Nothing <: Any](xs: Array[T]): scala.collection.mutable.ArrayOps[T] | ||
// = OptionMatching.runOrElse(xs)(((x1: Array[T]) => | ||
// ((OptionMatching.guard(x1.isInstanceOf[Array[AnyRef]], x1.asInstanceOf[Array[T] with Array[AnyRef]]).flatMap(((x2: Array[T] with Array[AnyRef]) => | ||
// OptionMatching.one(Test.this.refArrayOps[AnyRef](x2).asInstanceOf[scala.collection.mutable.ArrayOps[T]]))): Option[scala.collection.mutable.ArrayOps[T]]).orElse( | ||
// (OptionMatching.guard(null.==(x1), x1.asInstanceOf[Array[T]]).flatMap(((x3: Array[T]) => | ||
// OptionMatching.one(null))): Option[scala.collection.mutable.ArrayOps[T]])): Option[scala.collection.mutable.ArrayOps[T]]).orElse((OptionMatching.zero: Option[scala.collection.mutable.ArrayOps[T]])))) | ||
|
||
def refArrayOps[T <: AnyRef](xs: Array[T]): ArrayOps[T] = new ArrayOps.ofRef[T](xs) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
case class TypeBounds(a: Type, b: Type) | ||
class Type { | ||
def bounds: TypeBounds = bounds match { | ||
case TypeBounds(_: this.type, _: this.type) => TypeBounds(this, this) | ||
case oftp => oftp | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class ObjInCase { | ||
0 match { | ||
case _ => object o | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OK 5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
object Test extends App { | ||
(true, true) match { | ||
case (true, true) | (false, false) => 1 | ||
} | ||
|
||
List(5) match { | ||
case 1 :: Nil | 2 :: Nil => println("FAILED") | ||
case (x@(4 | 5 | 6)) :: Nil => println("OK "+ x) | ||
case 7 :: Nil => println("FAILED") | ||
case Nil => println("FAILED") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OK 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
object Test extends App { | ||
List(1, 2, 3) match { | ||
case Nil => println("FAIL") | ||
case x :: y :: xs if xs.length == 2 => println("FAIL") | ||
case x :: y :: xs if xs.length == 1 => println("OK "+ y) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
List(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
object Test extends App { | ||
println(List(1,2,3) match { | ||
case Nil => List(0) | ||
// since the :: extractor's argument must be a ::, there has to be a cast before its unapply is invoked | ||
case x :: y :: z :: a :: xs => xs ++ List(x) | ||
case x :: y :: z :: xs => xs ++ List(x) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
OK | ||
OK | ||
OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
object Test extends App { | ||
1 match { | ||
case 2 => println("FAILED") | ||
case 1 => println("OK") | ||
case 1 => println("FAILED") | ||
} | ||
|
||
val one = 1 | ||
1 match { | ||
case 2 => println("FAILED") | ||
case `one` => println("OK") | ||
case 1 => println("FAILED") | ||
} | ||
|
||
1 match { | ||
case 2 => println("FAILED") | ||
case Test.one => println("OK") | ||
case 1 => println("FAILED") | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
object Test extends App { | ||
List(List(1), List(2)) match { case x :: (y :: Nil) :: Nil => println(y) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
OK |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class C { | ||
class D | ||
val values = new Array[AnyRef](10) | ||
values(0) match { | ||
case name: D => println("NOK: "+ name) // the outer check on D's outer should not cause a NPE | ||
case null => println("OK") | ||
} | ||
} | ||
|
||
object Test extends C with App |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Map(a -> Some(1), b -> None) | ||
Map(a -> 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-Yvirtpatmat -Xexperimental |
Oops, something went wrong.