From 2e40898f31fbac46733ba74fce43d0ae14f84cf7 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Wed, 23 Jul 2014 10:46:57 +0200 Subject: [PATCH 01/23] Allow converting junit test to scalameter benchmark Lets start tracking performance! To run benchmarks execute `dotty-bench/test:run` in sbt --- bench/src/test/scala/TestsAsBenchmarks.scala | 61 ++++++++++++++++++++ project/Build.scala | 50 +++++++++++++++- 2 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 bench/src/test/scala/TestsAsBenchmarks.scala diff --git a/bench/src/test/scala/TestsAsBenchmarks.scala b/bench/src/test/scala/TestsAsBenchmarks.scala new file mode 100644 index 000000000000..a078c9f787e0 --- /dev/null +++ b/bench/src/test/scala/TestsAsBenchmarks.scala @@ -0,0 +1,61 @@ +package dotty.tools.benchmarks + +import java.lang.annotation.Annotation +import java.lang.reflect.Method + +import org.junit.runner.Request +import org.junit.runner.notification.RunNotifier +import org.scalameter.PerformanceTest.OnlineRegressionReport +import org.scalameter.api._ +import org.scalameter.reporting.RegressionReporter.Tester + +import scala.collection.mutable.ListBuffer + + +abstract class TestsToBenchmarkConverter +(targetClass: Class[_], + filterAnnot: Class[_ <: java.lang.annotation.Annotation] = classOf[org.junit.Test].asInstanceOf[Class[_ <: java.lang.annotation.Annotation]]) + extends OnlineRegressionReport { + + // accept all the results, do not fail + override def tester: Tester = new Tester.Accepter + + override def executor: Executor = LocalExecutor(warmer, aggregator, measurer) + val testNames = getMethodsAnnotatedWith(targetClass, filterAnnot).map(_.getName).sorted + + + val tests = testNames.map{name => + val runner = Request.method(targetClass, name).getRunner + (name, Gen.single("test")(name).map(Request.method(targetClass, _).getRunner))}.toMap + //Gen.enumeration("test")(testNames:_*) + + performance of targetClass.getSimpleName config (Context(reports.resultDir -> "./tmp")) in { + for (test <- testNames) + measure.method(test) in { + using(tests(test)) curve test in { + r => + val dummy = new RunNotifier() + r.run(dummy) + } + } + } + + def getMethodsAnnotatedWith(clazz: Class[_], annotation: Class[_ <: java.lang.annotation.Annotation]): List[Method] = { + val methods = ListBuffer[Method]() + var klass: Class[_] = clazz + while (klass ne classOf[AnyRef]) { + val allMethods = klass.getDeclaredMethods + import scala.collection.JavaConversions._ + for (method <- allMethods) { + if (annotation == null || method.isAnnotationPresent(annotation)) { + val annotInstance: Annotation = method.getAnnotation(annotation) + methods.add(method) + } + } + klass = klass.getSuperclass + } + methods.toList + } +} + +object dotcTests extends TestsToBenchmarkConverter(classOf[dotc.tests]) diff --git a/project/Build.scala b/project/Build.scala index 145e3eb8557c..8ab9e8eb24ee 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -1,6 +1,5 @@ +import sbt.Keys._ import sbt._ -import Keys._ -import Process._ object DottyBuild extends Build { @@ -71,4 +70,51 @@ object DottyBuild extends Build { ) lazy val dotty = Project(id = "dotty", base = file("."), settings = defaults) + + lazy val benchmarkSettings = Defaults.defaultSettings ++ Seq( + + // to get Scala 2.11 + resolvers += Resolver.sonatypeRepo("releases"), + + baseDirectory in (Test,run) := (baseDirectory in dotty).value, + + + libraryDependencies ++= Seq("com.storm-enroute" %% "scalameter" % "0.6" % Test, + "com.novocode" % "junit-interface" % "0.11-RC1"), + testFrameworks += new TestFramework("org.scalameter.ScalaMeterFramework"), + + // scalac options + scalacOptions in Global ++= Seq("-feature", "-deprecation", "-language:_"), + + javacOptions ++= Seq("-Xlint:unchecked", "-Xlint:deprecation"), + + fork in Test := true, + parallelExecution in Test := false, + + // http://grokbase.com/t/gg/simple-build-tool/135ke5y90p/sbt-setting-jvm-boot-paramaters-for-scala + javaOptions <++= (dependencyClasspath in Runtime, packageBin in Compile) map { (attList, bin) => + // put the Scala {library, reflect, compiler} in the classpath + val path = for { + file <- attList.map(_.data) + path = file.getAbsolutePath + prefix = if(path.endsWith(".jar")) "p" else "a" + } yield "-Xbootclasspath/"+ prefix +":" + path + // dotty itself needs to be in the bootclasspath + val fullpath = ("-Xbootclasspath/a:" + bin) :: path.toList + // System.err.println("BOOTPATH: " + fullpath) + + val travis_build = // propagate if this is a travis build + if (sys.props.isDefinedAt(TRAVIS_BUILD)) + List(s"-D$TRAVIS_BUILD=${sys.props(TRAVIS_BUILD)}") + else + List() + val res = agentOptions ::: travis_build ::: fullpath + println("Running with javaOptions: " +res) + res + } + ) + + + lazy val benchmarks = Project(id = "dotty-bench", settings = benchmarkSettings, + base = file("bench")) dependsOn(dotty % "compile->test") } From 38f335d2dec9a24146e081318aa4cbc41976ffc2 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Fri, 5 Sep 2014 12:51:36 +0200 Subject: [PATCH 02/23] Definitions used by pattern matcher --- src/dotty/tools/dotc/ast/Trees.scala | 3 ++- src/dotty/tools/dotc/core/Definitions.scala | 21 +++++++++++++++++++++ src/dotty/tools/dotc/core/Denotations.scala | 5 +++++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/ast/Trees.scala b/src/dotty/tools/dotc/ast/Trees.scala index 8a36fee3a5ae..1ffa467c277e 100644 --- a/src/dotty/tools/dotc/ast/Trees.scala +++ b/src/dotty/tools/dotc/ast/Trees.scala @@ -231,7 +231,8 @@ object Trees { * an UnAssignedTypeException is thrown. (Overridden by empty trees) */ def tpe: T @uncheckedVariance = { - if (myTpe == null) throw new UnAssignedTypeException(this) + if (myTpe == null) + throw new UnAssignedTypeException(this) myTpe } diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala index 594f0c013588..d4dc1ab377f4 100644 --- a/src/dotty/tools/dotc/core/Definitions.scala +++ b/src/dotty/tools/dotc/core/Definitions.scala @@ -4,6 +4,7 @@ package core import Types._, Contexts._, Symbols._, Denotations._, SymDenotations._, StdNames._, Names._ import Flags._, Scopes._, Decorators._, NameOps._, util.Positions._ +import TypeApplications._ import pickling.UnPickler.ensureConstructor import scala.annotation.{ switch, meta } import scala.collection.{ mutable, immutable } @@ -103,6 +104,7 @@ class Definitions { lazy val EmptyPackageVal = EmptyPackageClass.sourceModule.entered lazy val ScalaPackageVal = ctx.requiredPackage("scala") + lazy val ScalaMathPackageVal = ctx.requiredPackage("scala.math") lazy val ScalaPackageClass = ScalaPackageVal.moduleClass.asClass lazy val JavaPackageVal = ctx.requiredPackage("java") lazy val JavaLangPackageVal = ctx.requiredPackage("java.lang") @@ -177,7 +179,9 @@ class Definitions { lazy val ScalaPredefModule = ctx.requiredModule("scala.Predef") lazy val ScalaRuntimeModule = ctx.requiredModule("scala.runtime.ScalaRunTime") + lazy val ScalaRuntimeModuleClass = ScalaRuntimeModule.moduleClass.asClass lazy val BoxesRunTimeModule = ctx.requiredModule("scala.runtime.BoxesRunTime") + lazy val BoxesRunTimeClass = BoxesRunTimeModule.moduleClass lazy val DottyPredefModule = ctx.requiredModule("dotty.DottyPredef") lazy val NilModule = ctx.requiredModule("scala.collection.immutable.Nil") @@ -191,11 +195,15 @@ class Definitions { ScalaPackageClass, tpnme.Singleton, Trait | Interface | Final, List(AnyClass.typeRef), EmptyScope) lazy val SeqClass: ClassSymbol = ctx.requiredClass("scala.collection.Seq") + lazy val Seq_apply = ctx.requiredMethod(SeqClass, nme.apply) + lazy val Seq_head = ctx.requiredMethod(SeqClass, nme.head) lazy val ArrayClass: ClassSymbol = ctx.requiredClass("scala.Array") lazy val Array_apply = ctx.requiredMethod(ArrayClass, nme.apply) lazy val Array_update = ctx.requiredMethod(ArrayClass, nme.update) lazy val Array_length = ctx.requiredMethod(ArrayClass, nme.length) lazy val Array_clone = ctx.requiredMethod(ArrayClass, nme.clone_) + + lazy val traversableDropMethod = ctx.requiredMethod(ScalaRuntimeModuleClass, nme.drop) lazy val uncheckedStableClass: ClassSymbol = ctx.requiredClass("scala.annotation.unchecked.uncheckedStable") lazy val UnitClass = valueClassSymbol("scala.Unit", BoxedUnitClass, java.lang.Void.TYPE, UnitEnc) @@ -208,6 +216,13 @@ class Definitions { lazy val ShortClass = valueClassSymbol("scala.Short", BoxedShortClass, java.lang.Short.TYPE, ShortEnc) lazy val CharClass = valueClassSymbol("scala.Char", BoxedCharClass, java.lang.Character.TYPE, CharEnc) lazy val IntClass = valueClassSymbol("scala.Int", BoxedIntClass, java.lang.Integer.TYPE, IntEnc) + lazy val Int_- = IntClass.requiredMethod(nme.MINUS, List(IntType)) + lazy val Int_+ = IntClass.requiredMethod(nme.PLUS, List(IntType)) + lazy val Int_/ = IntClass.requiredMethod(nme.DIV, List(IntType)) + lazy val Int_* = IntClass.requiredMethod(nme.MUL, List(IntType)) + lazy val Int_== = IntClass.requiredMethod(nme.EQ, List(IntType)) + lazy val Int_>= = IntClass.requiredMethod(nme.GE, List(IntType)) + lazy val Int_<= = IntClass.requiredMethod(nme.LE, List(IntType)) lazy val LongClass = valueClassSymbol("scala.Long", BoxedLongClass, java.lang.Long.TYPE, LongEnc) lazy val Long_XOR_Long = LongClass.info.member(nme.XOR).requiredSymbol( x => (x is Method) && (x.info.firstParamTypes.head isRef defn.LongClass) @@ -253,6 +268,8 @@ class Definitions { lazy val JavaCloneableClass = ctx.requiredClass("java.lang.Cloneable") lazy val StringBuilderClass = ctx.requiredClass("scala.collection.mutable.StringBuilder") lazy val NullPointerExceptionClass = ctx.requiredClass(jnme.NPException) + lazy val MatchErrorClass = ctx.requiredClass("scala.MatchError") + lazy val MatchErrorType = MatchErrorClass.typeRef lazy val StringAddClass = ctx.requiredClass("scala.runtime.StringAdd") @@ -414,6 +431,10 @@ class Definitions { arity <= MaxTupleArity && (tp isRef TupleClass(arity)) } + def tupleType(elems: List[Type]) = { + TupleClass(elems.size).typeRef.appliedTo(elems) + } + def isProductSubType(tp: Type)(implicit ctx: Context) = (tp derivesFrom ProductClass) && tp.baseClasses.exists(ProductClasses contains _) diff --git a/src/dotty/tools/dotc/core/Denotations.scala b/src/dotty/tools/dotc/core/Denotations.scala index 43fff62ec388..4b21d8dc2a02 100644 --- a/src/dotty/tools/dotc/core/Denotations.scala +++ b/src/dotty/tools/dotc/core/Denotations.scala @@ -190,6 +190,11 @@ object Denotations { def requiredMethod(name: PreName)(implicit ctx: Context): TermSymbol = info.member(name.toTermName).requiredSymbol(_ is Method).asTerm + def requiredMethod(name: PreName, argTypes: List[Type])(implicit ctx: Context): TermSymbol = + info.member(name.toTermName).requiredSymbol(x=> + (x is Method) && x.info.paramTypess == List(argTypes) + ).asTerm + def requiredValue(name: PreName)(implicit ctx: Context): TermSymbol = info.member(name.toTermName).requiredSymbol(_.info.isParameterless).asTerm From ff9455e5e00086ba54b2333dd6d109311d22ac1c Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:12:36 +0200 Subject: [PATCH 03/23] initValue helper method --- src/dotty/tools/dotc/ast/tpd.scala | 18 ++++++++++++++++- src/dotty/tools/dotc/transform/LazyVals.scala | 20 ++++--------------- 2 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/dotty/tools/dotc/ast/tpd.scala b/src/dotty/tools/dotc/ast/tpd.scala index fecfefd37ac4..86164b53ef99 100644 --- a/src/dotty/tools/dotc/ast/tpd.scala +++ b/src/dotty/tools/dotc/ast/tpd.scala @@ -317,6 +317,20 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { Thicket(valdef, clsdef) } + def initValue(tpe: Types.Type)(implicit ctx: Context) = { + val tpw = tpe.widen + + if (tpw =:= defn.IntType) Literal(Constant(0)) + else if (tpw =:= defn.LongType) Literal(Constant(0L)) + else if (tpw =:= defn.BooleanType) Literal(Constant(false)) + else if (tpw =:= defn.CharType) Literal(Constant('\u0000')) + else if (tpw =:= defn.FloatType) Literal(Constant(0f)) + else if (tpw =:= defn.DoubleType) Literal(Constant(0d)) + else if (tpw =:= defn.ByteType) Literal(Constant(0.toByte)) + else if (tpw =:= defn.ShortType) Literal(Constant(0.toShort)) + else Literal(Constant(null)).select(defn.Any_asInstanceOf).appliedToType(tpe) + } + private class FindLocalDummyAccumulator(cls: ClassSymbol)(implicit ctx: Context) extends TreeAccumulator[Symbol] { def apply(sym: Symbol, tree: Tree) = if (sym.exists) sym @@ -523,7 +537,9 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { } } - def runtimeCall(name: TermName, args: List[Tree])(implicit ctx: Context): Tree = ??? + def runtimeCall(name: TermName, args: List[Tree])(implicit ctx: Context): Tree = { + Ident(defn.ScalaRuntimeModule.requiredMethod(name).termRef).appliedToArgs(args) + } def mkAnd(tree1: Tree, tree2: Tree)(implicit ctx: Context) = tree1.select(defn.Boolean_and).appliedTo(tree2) diff --git a/src/dotty/tools/dotc/transform/LazyVals.scala b/src/dotty/tools/dotc/transform/LazyVals.scala index 75dc10ce46b6..eaecbc258e71 100644 --- a/src/dotty/tools/dotc/transform/LazyVals.scala +++ b/src/dotty/tools/dotc/transform/LazyVals.scala @@ -172,18 +172,6 @@ class LazyValTranformContext { If(cond, init, exp) } - def initValue(tpe: Types.Type)(implicit ctx: Context) = - if (tpe =:= defn.IntType) Constant(0) - else if (tpe =:= defn.LongType) Constant(0L) - else if (tpe =:= defn.BooleanType) Constant(false) - else if (tpe =:= defn.CharType) Constant('\u0000') - else if (tpe =:= defn.FloatType) Constant(0f) - else if (tpe =:= defn.DoubleType) Constant(0d) - else if (tpe =:= defn.ByteType) Constant(0.toByte) - else if (tpe =:= defn.ShortType) Constant(0.toShort) - else Constant(null) - - def transformFieldValDefNonVolatile(x: ValDef)(implicit ctx: Context) = x match { case x@ValDef(mods, name, tpt, rhs) if (mods is Flags.Lazy) => val claz = x.symbol.owner.asClass @@ -193,8 +181,8 @@ class LazyValTranformContext { val containerSymbol = ctx.newSymbol(claz, containerName, (mods &~ Flags.Lazy | containerFlags).flags, tpe, coord = x.symbol.coord) addSym(claz, containerSymbol) - val containerTree = ValDef(containerSymbol, Literal(initValue(tpe))) - if (x.tpe.isNotNull && initValue(tpe).tag == Constants.NullTag) { + val containerTree = ValDef(containerSymbol, initValue(tpe)) + if (x.tpe.isNotNull && tpe <:< defn.AnyRefType) { // can use 'null' value instead of flag val slowPath = DefDef(x.symbol.asTerm, mkDefNonThreadSafeNonNullable(containerSymbol, rhs)) Thicket(List(containerTree, slowPath)) } @@ -252,7 +240,7 @@ class LazyValTranformContext { val thiz = This(claz)(ctx.fresh.setOwner(claz)) val resultSymbol = ctx.newSymbol(methodSymbol, "result".toTermName, containerFlags, tp) - val resultDef = ValDef(resultSymbol, Literal(initValue(tp.widen))) + val resultDef = ValDef(resultSymbol, initValue(tp)) val retrySymbol = ctx.newSymbol(methodSymbol, "retry".toTermName, containerFlags, defn.BooleanType) val retryDef = ValDef(retrySymbol, Literal(Constants.Constant(true))) @@ -355,7 +343,7 @@ class LazyValTranformContext { val containerName = ctx.freshName(name.toString + StdNames.nme.LAZY_LOCAL).toTermName val containerSymbol = ctx.newSymbol(claz, containerName, (mods &~ Flags.Lazy | containerFlags).flags, tpe, coord = x.symbol.coord) addSym(claz, containerSymbol) - val containerTree = ValDef(containerSymbol, Literal(initValue(tpe))) + val containerTree = ValDef(containerSymbol, initValue(tpe)) val offset = Select(Ident(companion.termRef), offsetSymbol.name) val getFlag = Select(Ident(helperModule.termRef), LazyVals.Names.get.toTermName) From 9b78066b9c2b9fc3f06f3aa87b74a0d4af150bc1 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:15:23 +0200 Subject: [PATCH 04/23] Making all case classes implement Product. Requires adding Product0 --- src/dotty/tools/dotc/ast/Desugar.scala | 9 +++++--- src/scala/Product0.scala | 29 ++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 src/scala/Product0.scala diff --git a/src/dotty/tools/dotc/ast/Desugar.scala b/src/dotty/tools/dotc/ast/Desugar.scala index ba2b62faaa26..3239d3f6e1ba 100644 --- a/src/dotty/tools/dotc/ast/Desugar.scala +++ b/src/dotty/tools/dotc/ast/Desugar.scala @@ -302,13 +302,16 @@ object desugar { def anyRef = ref(defn.AnyRefAlias.typeRef) def productConstr(n: Int) = { val tycon = ref(defn.ProductNClass(n).typeRef) - val targs = constrVparamss.head map (_.tpt) - AppliedTypeTree(tycon, targs) + if(n > 0) { + val targs = constrVparamss.head map (_.tpt) + AppliedTypeTree(tycon, targs) + } + else tycon } // Case classes get a ProductN parent var parents1 = parents - if ((mods is Case) && 2 <= arity && arity <= Definitions.MaxTupleArity) + if ((mods is Case) && arity <= Definitions.MaxTupleArity) parents1 = parents1 :+ productConstr(arity) // The thicket which is the desugared version of the companion object diff --git a/src/scala/Product0.scala b/src/scala/Product0.scala new file mode 100644 index 000000000000..e58282c07d15 --- /dev/null +++ b/src/scala/Product0.scala @@ -0,0 +1,29 @@ +package scala + +object Product0 { + def unapply(x: Product0): Option[Product0] = + Some(x) +} + +/** Product1 is a cartesian product of 1 component. + * @since 2.3 + */ +trait Product0 extends Any with Product { + /** The arity of this product. + * @return 1 + */ + override def productArity = 0 + + + /** Returns the n-th projection of this product if 0 < n <= productArity, + * otherwise throws an `IndexOutOfBoundsException`. + * + * @param n number of the projection to be returned + * @return same as `._(n+1)`, for example `productElement(0)` is the same as `._1`. + * @throws IndexOutOfBoundsException + */ + + @throws(classOf[IndexOutOfBoundsException]) + override def productElement(n: Int) = throw new IndexOutOfBoundsException(n.toString()) + +} \ No newline at end of file From 45b67a466a20fb07f33626049e2961c28ade818a Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:16:35 +0200 Subject: [PATCH 05/23] Unapply doc update --- src/dotty/tools/dotc/ast/Trees.scala | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dotty/tools/dotc/ast/Trees.scala b/src/dotty/tools/dotc/ast/Trees.scala index 1ffa467c277e..87f4e9fa1e4d 100644 --- a/src/dotty/tools/dotc/ast/Trees.scala +++ b/src/dotty/tools/dotc/ast/Trees.scala @@ -684,6 +684,7 @@ object Trees { * @param implicits Any implicit parameters passed to the unapply after the selector * @param patterns The argument patterns in the pattern match. * + * It is typed with same type as first `fun` argument * Given a match selector `sel` a pattern UnApply(fun, implicits, patterns) is roughly translated as follows * * val result = fun(sel)(implicits) From 7ebc55c0ad673029448537a8dd0a0d15900d9d00 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:18:00 +0200 Subject: [PATCH 06/23] More complicated patterns. --- tests/pos/Patterns.scala | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/pos/Patterns.scala b/tests/pos/Patterns.scala index 4470eb232ce2..ffb486b42ecd 100644 --- a/tests/pos/Patterns.scala +++ b/tests/pos/Patterns.scala @@ -4,6 +4,30 @@ object Patterns { case _ => false } + object Breakdown { + def unapplySeq(x: Int): Some[List[String]] = Some(List("", "there")) + } + + object Test2 { + 42 match { + case Breakdown("") => // needed to trigger bug + case Breakdown("foo") => // needed to trigger bug + case Breakdown("", who) => println ("hello " + who) + } + } + + val names = List("a", "b", "c") + object SeqExtractors { + val y = names match { + case List(x, z) => x + case List(x) => x + case List() => "" + } + val yy: String = y + } + + + val xs = List('2' -> "ABC", '3' -> "DEF") xs filter { From 09d8aa6e5ec4030511c41de1cf390b7faad33cea Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:19:24 +0200 Subject: [PATCH 07/23] Definitions used by patmat --- src/dotty/tools/dotc/core/Definitions.scala | 2 +- src/dotty/tools/dotc/core/Names.scala | 2 + src/dotty/tools/dotc/core/StdNames.scala | 59 +++++++++++++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/dotty/tools/dotc/core/Definitions.scala b/src/dotty/tools/dotc/core/Definitions.scala index d4dc1ab377f4..d2473dd4ab96 100644 --- a/src/dotty/tools/dotc/core/Definitions.scala +++ b/src/dotty/tools/dotc/core/Definitions.scala @@ -407,7 +407,7 @@ class Definitions { lazy val Function0_apply = FunctionClass(0).requiredMethod(nme.apply) lazy val TupleClass = mkArityArray("scala.Tuple", MaxTupleArity, 2) - lazy val ProductNClass = mkArityArray("scala.Product", MaxTupleArity, 2) + lazy val ProductNClass = mkArityArray("scala.Product", MaxTupleArity, 0) lazy val FunctionClasses: Set[Symbol] = FunctionClass.toSet lazy val TupleClasses: Set[Symbol] = TupleClass.toSet diff --git a/src/dotty/tools/dotc/core/Names.scala b/src/dotty/tools/dotc/core/Names.scala index 7585e1f37cc8..cf53d044529d 100644 --- a/src/dotty/tools/dotc/core/Names.scala +++ b/src/dotty/tools/dotc/core/Names.scala @@ -132,6 +132,8 @@ object Names { i < length } + def firstChar = chrs(start) + // ----- Collections integration ------------------------------------- override protected[this] def thisCollection: WrappedString = new WrappedString(repr.toString) diff --git a/src/dotty/tools/dotc/core/StdNames.scala b/src/dotty/tools/dotc/core/StdNames.scala index 3ab0ec36edcd..9958a14b1e13 100644 --- a/src/dotty/tools/dotc/core/StdNames.scala +++ b/src/dotty/tools/dotc/core/StdNames.scala @@ -268,6 +268,29 @@ object StdNames { val x_8 : N = "x$8" val x_9 : N = "x$9" + val _1 : N = "_1" + val _2 : N = "_2" + val _3 : N = "_3" + val _4 : N = "_4" + val _5 : N = "_5" + val _6 : N = "_6" + val _7 : N = "_7" + val _8 : N = "_8" + val _9 : N = "_9" + val _10 : N = "_10" + val _11 : N = "_11" + val _12 : N = "_12" + val _13 : N = "_13" + val _14 : N = "_14" + val _15 : N = "_15" + val _16 : N = "_16" + val _17 : N = "_17" + val _18 : N = "_18" + val _19 : N = "_19" + val _20 : N = "_20" + val _21 : N = "_21" + val _22 : N = "_22" + val ??? = encode("???") val genericWrapArray: N = "genericWrapArray" @@ -627,6 +650,32 @@ object StdNames { case _ => termName("x$" + i) } + @switch def productAccessorName(j: Int): TermName = j match { + case 1 => nme._1 + case 2 => nme._2 + case 3 => nme._3 + case 4 => nme._4 + case 5 => nme._5 + case 6 => nme._6 + case 7 => nme._7 + case 8 => nme._8 + case 9 => nme._9 + case 10 => nme._10 + case 11 => nme._11 + case 12 => nme._12 + case 13 => nme._13 + case 14 => nme._14 + case 15 => nme._15 + case 16 => nme._16 + case 17 => nme._17 + case 18 => nme._18 + case 19 => nme._19 + case 20 => nme._20 + case 21 => nme._21 + case 22 => nme._22 + case _ => termName("_" + j) + } + def syntheticParamNames(num: Int): List[TermName] = (0 until num).map(syntheticParamName)(breakOut) @@ -636,6 +685,16 @@ object StdNames { def newBitmapName(bitmapPrefix: TermName, n: Int): TermName = bitmapPrefix ++ n.toString def selectorName(n: Int): TermName = "_" + (n + 1) + + /** Is name a variable name? */ + def isVariableName(name: Name): Boolean = { + val first = name.firstChar + ( ((first.isLower && first.isLetter) || first == '_') + && (name != nme.false_) + && (name != nme.true_) + && (name != nme.null_) + ) + } } class ScalaTypeNames extends ScalaNames[TypeName] { From 7c13ea847115373fac31744b90483ba21d071c65 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:20:51 +0200 Subject: [PATCH 08/23] Common functionality to be shared between patmat and typer --- src/dotty/tools/dotc/typer/Applications.scala | 88 ++++++++++--------- 1 file changed, 48 insertions(+), 40 deletions(-) diff --git a/src/dotty/tools/dotc/typer/Applications.scala b/src/dotty/tools/dotc/typer/Applications.scala index 242985b57ffc..811139356b40 100644 --- a/src/dotty/tools/dotc/typer/Applications.scala +++ b/src/dotty/tools/dotc/typer/Applications.scala @@ -31,6 +31,53 @@ object Applications { private val isNamedArg = (arg: Any) => arg.isInstanceOf[Trees.NamedArg[_]] def hasNamedArg(args: List[Any]) = args exists isNamedArg + def extractorMemberType(tp: Type, name: Name, errorPos: Position = NoPosition)(implicit ctx:Context) = { + val ref = tp member name + if (ref.isOverloaded) + errorType(i"Overloaded reference to $ref is not allowed in extractor", errorPos) + else if (ref.info.isInstanceOf[PolyType]) + errorType(i"Reference to polymorphic $ref: ${ref.info} is not allowed in extractor", errorPos) + else + ref.info.widenExpr.dealias + } + + def productSelectorTypes(tp: Type, errorPos: Position = NoPosition)(implicit ctx:Context): List[Type] = { + val sels = for (n <- Iterator.from(0)) yield extractorMemberType(tp, nme.selectorName(n), errorPos) + sels.takeWhile(_.exists).toList + } + + def productSelectors(tp: Type)(implicit ctx:Context): List[Symbol] = { + val sels = for (n <- Iterator.from(0)) yield tp.member(nme.selectorName(n)).symbol + sels.takeWhile(_.exists).toList + } + + def getUnapplySelectors(tp: Type, args:List[untpd.Tree], pos: Position = NoPosition)(implicit ctx: Context): List[Type] = + if (defn.isProductSubType(tp) && args.length > 1) productSelectorTypes(tp, pos) + else tp :: Nil + + def unapplyArgs(unapplyResult: Type, unapplyFn:Tree, args:List[untpd.Tree], pos: Position = NoPosition)(implicit ctx: Context): List[Type] = { + + def seqSelector = defn.RepeatedParamType.appliedTo(unapplyResult.elemType :: Nil) + def getTp = extractorMemberType(unapplyResult, nme.get, pos) + + // println(s"unapply $unapplyResult ${extractorMemberType(unapplyResult, nme.isDefined)}") + if (extractorMemberType(unapplyResult, nme.isDefined, pos) isRef defn.BooleanClass) { + if (getTp.exists) + if (unapplyFn.symbol.name == nme.unapplySeq) { + val seqArg = boundsToHi(getTp.firstBaseArgInfo(defn.SeqClass)) + if (seqArg.exists) return args map Function.const(seqArg) + } + else return getUnapplySelectors(getTp, args, pos) + else if (defn.isProductSubType(unapplyResult)) return productSelectorTypes(unapplyResult, pos) + } + if (unapplyResult derivesFrom defn.SeqClass) seqSelector :: Nil + else if (unapplyResult isRef defn.BooleanClass) Nil + else { + ctx.error(i"$unapplyResult is not a valid result type of an unapply method of an extractor", pos) + Nil + } + } + def wrapDefs(defs: mutable.ListBuffer[Tree], tree: Tree)(implicit ctx: Context): Tree = if (defs != null && defs.nonEmpty) tpd.Block(defs.toList, tree) else tree } @@ -581,45 +628,6 @@ trait Applications extends Compatibility { self: Typer => def fromScala2x = unapplyFn.symbol.exists && (unapplyFn.symbol.owner is Scala2x) - def unapplyArgs(unapplyResult: Type)(implicit ctx: Context): List[Type] = { - def extractorMemberType(tp: Type, name: Name) = { - val ref = tp member name - if (ref.isOverloaded) - errorType(i"Overloaded reference to $ref is not allowed in extractor", tree.pos) - else if (ref.info.isInstanceOf[PolyType]) - errorType(i"Reference to polymorphic $ref: ${ref.info} is not allowed in extractor", tree.pos) - else - ref.info.widenExpr.dealias - } - - def productSelectors(tp: Type): List[Type] = { - val sels = for (n <- Iterator.from(0)) yield extractorMemberType(tp, nme.selectorName(n)) - sels.takeWhile(_.exists).toList - } - def seqSelector = defn.RepeatedParamType.appliedTo(unapplyResult.elemType :: Nil) - def getSelectors(tp: Type): List[Type] = - if (defn.isProductSubType(tp) && args.length > 1) productSelectors(tp) - else tp :: Nil - def getTp = extractorMemberType(unapplyResult, nme.get) - - // println(s"unapply $unapplyResult ${extractorMemberType(unapplyResult, nme.isDefined)}") - if (extractorMemberType(unapplyResult, nme.isDefined) isRef defn.BooleanClass) { - if (getTp.exists) - if (unapplyFn.symbol.name == nme.unapplySeq) { - val seqArg = boundsToHi(getTp.firstBaseArgInfo(defn.SeqClass)) - if (seqArg.exists) return args map Function.const(seqArg) - } - else return getSelectors(getTp) - else if (defn.isProductSubType(unapplyResult)) return productSelectors(unapplyResult) - } - if (unapplyResult derivesFrom defn.SeqClass) seqSelector :: Nil - else if (unapplyResult isRef defn.BooleanClass) Nil - else { - ctx.error(i"$unapplyResult is not a valid result type of an unapply method of an extractor", tree.pos) - Nil - } - } - /** Can `subtp` be made to be a subtype of `tp`, possibly by dropping some * refinements in `tp`? */ @@ -679,7 +687,7 @@ trait Applications extends Compatibility { self: Typer => case Apply(unapply, `dummyArg` :: Nil) => Nil } - var argTypes = unapplyArgs(unapplyApp.tpe) + var argTypes = unapplyArgs(unapplyApp.tpe, unapplyFn, args, tree.pos) for (argType <- argTypes) assert(!argType.isInstanceOf[TypeBounds], unapplyApp.tpe.show) val bunchedArgs = argTypes match { case argType :: Nil => From 2946689d7a08f3e0ad5589bb86aa042530ec8a6a Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:24:59 +0200 Subject: [PATCH 09/23] CommonCodegen for patMat --- .../tools/dotc/transform/PatternMatcher.scala | 143 +++++++++++++++++- 1 file changed, 139 insertions(+), 4 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 40a1574832e4..2420ee928c28 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -13,14 +13,149 @@ import core.transform.Erasure.isUnboundedGeneric import typer.ErrorReporting._ import ast.Trees._ +import dotty.tools.dotc.util.Positions.Position +import dotty.tools.dotc.core.Decorators._ +import dotty.tools.dotc.core.Flags + /** This transform eliminates patterns. Right now it's a dummy. * Awaiting the real pattern matcher. */ class PatternMatcher extends TreeTransform { - import ast.tpd._ + import dotty.tools.dotc.ast.tpd._ + + implicit val ctx: Context = ??? + + def name: String = "patternMatcher" + + /*override def transformCaseDef(tree: CaseDef)(implicit ctx: Context, info: TransformerInfo): Tree = + cpy.CaseDef(tree, Literal(Constant("")), tree.guard, tree.body)*/ + + + /*case Try(block, catches, finalizer) => + treeCopy.Try(tree, transform(block), translator.translateTry(transformTrees(catches).asInstanceOf[List[CaseDef]], tree.tpe, tree.pos), transform(finalizer)) + case _ => super.transform(tree)*/ + /*override def transformMatch(tree: tpd.Match)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { + case Match(sel, cases) => + val origTp = tree.tpe + // setType origTp intended for CPS -- TODO: is it necessary? + val translated = translator.translateMatch(treeCopy.Match(tree, transform(sel), transformTrees(cases).asInstanceOf[List[CaseDef]])) + try { + localTyper.typed(translated) setType origTp + } catch { + case x: (Types#TypeError) => + // TODO: this should never happen; error should've been reported during type checking + unit.error(tree.pos, "error during expansion of this match (this is a scalac bug).\nThe underlying error was: "+ x.msg) + translated + } + }*/ + + + def translator = { + new OptimizingMatchTranslator/*(localTyper)*/ + } + + class OptimizingMatchTranslator/*(val typer: analyzer.Typer)*/ /*extends MatchTranslator + with MatchOptimizer*/ + + trait Debugging { + + // TODO: the inliner fails to inline the closures to debug.patmat unless the method is nested in an object + object debug { + //val printPatmat = global.settings.Ypatmatdebug.value + final def patmat(s: => String) = /*if (printPatmat) Console.err.*/ + println(s) + final def patmatResult[T](s: => String)(result: T): T = { + /*if (printPatmat) Console.err.*/ + println(s + ": " + result) + result + } + } + } + + trait MatchMonadInterface { + // val typer: Typer + def matchOwner(implicit ctx: Context) = ctx.owner + def pureType(tp: Type): Type = tp + + def reportUnreachable(pos: Position) = { + ctx.warning("unreachable code", pos) + } + def reportMissingCases(pos: Position, counterExamples: List[String]) = { + val ceString = + if (counterExamples.tail.isEmpty) "input: " + counterExamples.head + else "inputs: " + counterExamples.mkString(", ") + + ctx.warning("match may not be exhaustive.\nIt would fail on the following "+ ceString, pos) + } + } + + trait CodegenCore extends MatchMonadInterface { + private var ctr = 0 + def freshName(prefix: String) = ctx.freshName(prefix).toTermName + + // assert(owner ne null); assert(owner ne NoSymbol) + def freshSym(pos: Position, tp: Type = NoType, prefix: String = "x") = + ctx.newSymbol(ctx.owner, freshName(prefix) ,Flags.Synthetic, tp, coord = pos) + + def newSynthCaseLabel(name: String) = ??? + //NoSymbol.newLabel(freshName(name), NoPosition) setFlag treeInfo.SYNTH_CASE_FLAGS + + // codegen relevant to the structure of the translation (how extractors are combined) + trait AbsCodegen { + def matcher(scrut: Tree, scrutSym: Symbol, restpe: Type)(cases: List[Casegen => Tree], matchFailGen: Option[Tree => Tree]): Tree + + // local / context-free + def _asInstanceOf(b: Symbol, tp: Type): Tree + def _equals(checker: Tree, binder: Symbol): Tree + def _isInstanceOf(b: Symbol, tp: Type): Tree + def drop(tgt: Tree)(n: Int): Tree + def index(tgt: Tree)(i: Int): Tree + def mkZero(tp: Type): Tree + def tupleSel(binder: Symbol)(i: Int): Tree + } + + // structure + trait Casegen extends AbsCodegen { + def one(res: Tree): Tree + + def flatMap(prev: Tree, b: Symbol, next: Tree): Tree + def flatMapCond(cond: Tree, res: Tree, nextBinder: Symbol, next: Tree): Tree + def flatMapGuard(cond: Tree, next: Tree): Tree + def ifThenElseZero(c: Tree, thenp: Tree): Tree = + If(c, thenp, zero) + protected def zero: Tree + } + + def codegen: AbsCodegen + + abstract class CommonCodegen extends AbsCodegen { + def fun(arg: TermSymbol, body: Tree): Tree = + DefDef(arg, body) + + def tupleSel(binder: Symbol)(i: Int): Tree = ref(binder).select(nme.productAccessorName(i)) // make tree that accesses the i'th component of the tuple referenced by binder + def index(tgt: Tree)(i: Int): Tree = tgt.appliedTo(Literal(Constant(i))) + + // Right now this blindly calls drop on the result of the unapplySeq + // unless it verifiably has no drop method (this is the case in particular + // with Array.) You should not actually have to write a method called drop + // for name-based matching, but this was an expedient route for the basics. + def drop(tgt: Tree)(n: Int): Tree = { + def callDirect = tgt.select(nme.drop).appliedTo(Literal(Constant(n))) + def callRuntime = ref(ctx.definitions.traversableDropMethod).appliedTo(tgt, Literal(Constant(n))) + + def needsRuntime = (tgt.tpe ne null) && tgt.tpe.baseTypeRef(ctx.definitions.SeqType.classSymbol).member(nme.drop).exists /*typeOfMemberNamedDrop(tgt.tpe) == NoType*/ + + if (needsRuntime) callRuntime else callDirect + } + + // NOTE: checker must be the target of the ==, that's the patmat semantics for ya + def _equals(checker: Tree, binder: Symbol): Tree = checker.select(defn.Any_equals).appliedTo(ref(binder)) - override def name: String = "patternMatcher" + // the force is needed mainly to deal with the GADT typing hack (we can't detect it otherwise as tp nor pt need contain an abstract type, we're just casting wildly) + def _asInstanceOf(b: Symbol, tp: Type): Tree = if (b.info <:< tp) ref(b) else ref(b).select(defn.Any_asInstanceOf).appliedToType(tp) + def _isInstanceOf(b: Symbol, tp: Type): Tree = ref(b).select(defn.Any_isInstanceOf).appliedToType(tp) - override def transformCaseDef(tree: CaseDef)(implicit ctx: Context, info: TransformerInfo): Tree = - cpy.CaseDef(tree, Literal(Constant("")), tree.guard, tree.body) + def mkZero(tp: Type): Tree = initValue(tp) + } + } } \ No newline at end of file From c6473bd502beca0e2208900f2996834b47115fd5 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:28:15 +0200 Subject: [PATCH 10/23] Optimized codegen for patmat --- .../tools/dotc/transform/PatternMatcher.scala | 114 +++++++++++++++++- 1 file changed, 112 insertions(+), 2 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 2420ee928c28..775bf93a410c 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -97,12 +97,12 @@ class PatternMatcher extends TreeTransform { def freshSym(pos: Position, tp: Type = NoType, prefix: String = "x") = ctx.newSymbol(ctx.owner, freshName(prefix) ,Flags.Synthetic, tp, coord = pos) - def newSynthCaseLabel(name: String) = ??? + def newSynthCaseLabel(name: String, tpe:Type) = ctx.newSymbol(ctx.owner, ctx.freshName(name).toTermName, Flags.Label, tpe) //NoSymbol.newLabel(freshName(name), NoPosition) setFlag treeInfo.SYNTH_CASE_FLAGS // codegen relevant to the structure of the translation (how extractors are combined) trait AbsCodegen { - def matcher(scrut: Tree, scrutSym: Symbol, restpe: Type)(cases: List[Casegen => Tree], matchFailGen: Option[Tree => Tree]): Tree + def matcher(scrut: Tree, scrutSym: Symbol, restpe: Type)(cases: List[Casegen => Tree], matchFailGen: Option[Symbol => Tree]): Tree // local / context-free def _asInstanceOf(b: Symbol, tp: Type): Tree @@ -158,4 +158,114 @@ class PatternMatcher extends TreeTransform { def mkZero(tp: Type): Tree = initValue(tp) } } + + trait OptimizedCodegen extends CodegenCore /*with TypedSubstitution*/ with MatchMonadInterface { + override def codegen: AbsCodegen = optimizedCodegen + + // when we know we're targetting Option, do some inlining the optimizer won't do + // for example, `o.flatMap(f)` becomes `if(o == None) None else f(o.get)`, similarly for orElse and guard + // this is a special instance of the advanced inlining optimization that takes a method call on + // an object of a type that only has two concrete subclasses, and inlines both bodies, guarded by an if to distinguish the two cases + object optimizedCodegen extends CommonCodegen { //import CODE._ + + /** Inline runOrElse and get rid of Option allocations + * + * runOrElse(scrut: scrutTp)(matcher): resTp = matcher(scrut) getOrElse ${catchAll(`scrut`)} + * the matcher's optional result is encoded as a flag, keepGoing, where keepGoing == true encodes result.isEmpty, + * if keepGoing is false, the result Some(x) of the naive translation is encoded as matchRes == x + */ + def matcher(scrut: Tree, scrutSym: Symbol, restpe: Type)(cases: List[Casegen => Tree], matchFailGen: Option[Symbol => Tree]): Tree = { + val matchRes = ctx.newSymbol(NoSymbol, ctx.freshName("x").toTermName, Flags.Synthetic | Flags.Param, restpe /*withoutAnnotations*/) + //NoSymbol.newValueParameter(newTermName("x"), NoPosition, newFlags = SYNTHETIC) setInfo restpe.withoutAnnotations + val mtype = MethodType(List("x".toTermName), List(restpe))(_=>restpe) + val matchEnd = newSynthCaseLabel("matchEnd", mtype) + val matchEndDef = DefDef(matchEnd, args => args.head.head) + var lastSymbol: TermSymbol = null + def newCaseSym = { + lastSymbol = newSynthCaseLabel(ctx.freshName("case"), MethodType(Nil, restpe)) + lastSymbol + } + + // must compute catchAll after caseLabels (side-effects nextCase) + // catchAll.isEmpty iff no synthetic default case needed (the (last) user-defined case is a default) + // if the last user-defined case is a default, it will never jump to the next case; it will go immediately to matchEnd + val catchAllDef = matchFailGen.map({ matchFailGen => + DefDef(newCaseSym, _ => Block(List(matchEndDef), ref(matchEnd).appliedTo(matchFailGen(scrutSym)))) + }) // at most 1 element + + + + val caseDefs = cases.foldLeft[Tree](catchAllDef.getOrElse(matchEndDef)){ (acc: Tree, mkCase: Casegen => Tree) => + val nextCase = lastSymbol.orElse(matchEnd) + + + DefDef(newCaseSym, _ => Block(List(acc), mkCase(new OptimizedCasegen(matchEnd, nextCase)))) + } + + + // scrutSym == NoSymbol when generating an alternatives matcher + // val scrutDef = scrutSym.fold(List[Tree]())(ValDef(_, scrut) :: Nil) // for alternatives + + // the generated block is taken apart in TailCalls under the following assumptions + // the assumption is once we encounter a case, the remainder of the block will consist of cases + // the prologue may be empty, usually it is the valdef that stores the scrut + // val (prologue, cases) = stats span (s => !s.isInstanceOf[LabelDef]) + Block( + List(caseDefs), + ref(lastSymbol).appliedToNone + ) + } + + class OptimizedCasegen(matchEnd: Symbol, nextCase: Symbol) extends CommonCodegen with Casegen { + def matcher(scrut: Tree, scrutSym: Symbol, restpe: Type)(cases: List[Casegen => Tree], matchFailGen: Option[Symbol => Tree]): Tree = + optimizedCodegen.matcher(scrut, scrutSym, restpe)(cases, matchFailGen) + + // only used to wrap the RHS of a body + // res: T + // returns MatchMonad[T] + def one(res: Tree): Tree = ref(matchEnd) appliedTo res // a jump to a case label is special-cased in typedApply + protected def zero: Tree = ref(nextCase) appliedToNone + + // prev: MatchMonad[T] + // b: T + // next: MatchMonad[U] + // returns MatchMonad[U] + def flatMap(prev: Tree, b: Symbol, next: Tree): Tree = { + val prevSym = freshSym(prev.pos, prev.tpe, "o") + Block( + List(ValDef(prevSym, prev)), + // must be isEmpty and get as we don't control the target of the call (prev is an extractor call) + ifThenElseZero( + ref(prevSym).select("isEmpty".toTermName).select(ctx.definitions.Boolean_!), + /*Substitution(b, prevSym DOT vpmName.get)*/(next) // todo? + ) + ) + } + + // cond: Boolean + // res: T + // nextBinder: T + // next == MatchMonad[U] + // returns MatchMonad[U] + def flatMapCond(cond: Tree, res: Tree, nextBinder: Symbol, next: Tree): Tree = { + val rest = Block(List(ValDef(nextBinder.asTerm, res)), next) + ifThenElseZero(cond, rest) + } + + // guardTree: Boolean + // next: MatchMonad[T] + // returns MatchMonad[T] + def flatMapGuard(guardTree: Tree, next: Tree): Tree = + ifThenElseZero(guardTree, next) + + def flatMapCondStored(cond: Tree, condSym: Symbol, res: Tree, nextBinder: Symbol, next: Tree): Tree = + ifThenElseZero(cond, Block( + List(Assign(ref(condSym), Literal(Constant(true))), + Assign(ref(nextBinder), res)), + next + )) + } + + } + } } \ No newline at end of file From b790f838a2edb3b52c99300434ceded9b83b596b Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:31:06 +0200 Subject: [PATCH 11/23] TreeMakers for patmat --- .../tools/dotc/transform/PatternMatcher.scala | 619 ++++++++++++++++++ 1 file changed, 619 insertions(+) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 775bf93a410c..2d0dfe00f668 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -10,6 +10,8 @@ import core.Types._ import core.Constants._ import core.StdNames._ import core.transform.Erasure.isUnboundedGeneric +import dotty.tools.dotc.transform.PatternMatcher.CodegenCore.Casegen +import dotty.tools.dotc.util.Positions import typer.ErrorReporting._ import ast.Trees._ @@ -268,4 +270,621 @@ class PatternMatcher extends TreeTransform { } } + final case class Suppression(exhaustive: Boolean, unreachable: Boolean) + object Suppression { + val NoSuppression = Suppression(false, false) + } + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // the making of the trees + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + trait TreeMakers /*extends TypedSubstitution*/ extends CodegenCore { + def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) + def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit + + def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Tree => Tree], unchecked: Boolean): Option[Tree] = + None + + // for catch (no need to customize match failure) + def emitTypeSwitch(bindersAndCases: List[(Symbol, List[TreeMaker])], pt: Type): Option[List[CaseDef]] = + None + + abstract class TreeMaker{ + def pos: Position + + /** captures the scope and the value of the bindings in patterns + * important *when* the substitution happens (can't accumulate and do at once after the full matcher has been constructed) + */ + /*def substitution: Substitution = + if (currSub eq null) localSubstitution + else currSub*/ + + //protected def localSubstitution: Substitution + + /*private[TreeMakers] def incorporateOuterSubstitution(outerSubst: Substitution): Unit = { + if (currSub ne null) { + debug.patmat("BUG: incorporateOuterSubstitution called more than once for "+ ((this, currSub, outerSubst))) + Thread.dumpStack() + } + else currSub = outerSubst >> substitution + } + private[this] var currSub: Substitution = null*/ + + /** The substitution that specifies the trees that compute the values of the subpattern binders. + * + * Should not be used to perform actual substitution! + * Only used to reason symbolically about the values the subpattern binders are bound to. + * See TreeMakerToCond#updateSubstitution. + * + * Overridden in PreserveSubPatBinders to pretend it replaces the subpattern binders by subpattern refs + * (Even though we don't do so anymore -- see SI-5158, SI-5739 and SI-6070.) + * + * TODO: clean this up, would be nicer to have some higher-level way to compute + * the binders bound by this tree maker and the symbolic values that correspond to them + */ + // def subPatternsAsSubstitution: Substitution = substitution + + // build Tree that chains `next` after the current extractor + def chainBefore(next: Tree)(casegen: Casegen): Tree + } + + sealed trait NoNewBinders extends TreeMaker { + //protected val localSubstitution: Substitution = EmptySubstitution + } + + case class TrivialTreeMaker(tree: Tree) extends TreeMaker with NoNewBinders { + def pos = tree.pos + + def chainBefore(next: Tree)(casegen: Casegen): Tree = tree + } + + case class BodyTreeMaker(body: Tree, matchPt: Type) extends TreeMaker with NoNewBinders { + def pos = body.pos + + def chainBefore(next: Tree)(casegen: Casegen): Tree = // assert(next eq EmptyTree) + /*atPos(body.pos)*/(casegen.one(/*substitution(*/body/*)*/)) // since SubstOnly treemakers are dropped, need to do it here + override def toString = "B"+((body, matchPt)) + } + + case class SubstOnlyTreeMaker(prevBinder: Symbol, nextBinder: Symbol) extends TreeMaker { + val pos = Positions.NoPosition + + //val localSubstitution = Substitution(prevBinder, CODE.REF(nextBinder)) + def chainBefore(next: Tree)(casegen: Casegen): Tree = /*substitution(*/next//) + override def toString = "S"//+ localSubstitution + } + + sealed abstract class FunTreeMaker extends TreeMaker { + val nextBinder: Symbol + def pos = nextBinder.pos + } + + sealed abstract class CondTreeMaker extends FunTreeMaker { + val prevBinder: Symbol + val nextBinderTp: Type + val cond: Tree + val res: Tree + + lazy val nextBinder = freshSym(pos, nextBinderTp) + //lazy val localSubstitution = Substitution(List(prevBinder), List(CODE.REF(nextBinder))) + + def chainBefore(next: Tree)(casegen: Casegen): Tree = + /*atPos(pos)(*/casegen.flatMapCond(cond, res, nextBinder, /*substitution(*/next/*)*/)) + } + + // unless we're optimizing, emit local variable bindings for all subpatterns of extractor/case class patterns + protected val debugInfoEmitVars = true //!settings.optimise.value + + sealed trait PreserveSubPatBinders extends TreeMaker { + val subPatBinders: List[Symbol] + val subPatRefs: List[Tree] + val ignoredSubPatBinders: Set[Symbol] + + // unless `debugInfoEmitVars`, this set should contain the bare minimum for correctness + // mutable case class fields need to be stored regardless (SI-5158, SI-6070) -- see override in ProductExtractorTreeMaker + // sub patterns bound to wildcard (_) are never stored as they can't be referenced + // dirty debuggers will have to get dirty to see the wildcards + lazy val storedBinders: Set[Symbol] = + (if (debugInfoEmitVars) subPatBinders.toSet else Set.empty) ++ extraStoredBinders -- ignoredSubPatBinders + + // e.g., mutable fields of a case class in ProductExtractorTreeMaker + def extraStoredBinders: Set[Symbol] + + def emitVars = storedBinders.nonEmpty + + private lazy val (stored, substed) = (subPatBinders, subPatRefs).zipped.partition{ case (sym, _) => storedBinders(sym) } + + protected lazy val localSubstitution: Substitution = if (!emitVars) Substitution(subPatBinders, subPatRefs) + else { + val (subPatBindersSubstituted, subPatRefsSubstituted) = substed.unzip + Substitution(subPatBindersSubstituted.toList, subPatRefsSubstituted.toList) + } + + /** The substitution that specifies the trees that compute the values of the subpattern binders. + * + * We pretend to replace the subpattern binders by subpattern refs + * (Even though we don't do so anymore -- see SI-5158, SI-5739 and SI-6070.) + */ + override def subPatternsAsSubstitution = + Substitution(subPatBinders, subPatRefs) >> super.subPatternsAsSubstitution + + def bindSubPats(in: Tree): Tree = + if (!emitVars) in + else { + // binders in `subPatBindersStored` that are referenced by tree `in` + val usedBinders = new mutable.HashSet[Symbol]() + // all potentially stored subpat binders + val potentiallyStoredBinders = stored.unzip._1.toSet + // compute intersection of all symbols in the tree `in` and all potentially stored subpat binders + in.foreach(t => if (potentiallyStoredBinders(t.symbol)) usedBinders += t.symbol) + + if (usedBinders.isEmpty) in + else { + // only store binders actually used + val (subPatBindersStored, subPatRefsStored) = stored.filter{case (b, _) => usedBinders(b)}.unzip + Block(map2(subPatBindersStored.toList, subPatRefsStored.toList)(ValDef(_, _)), in) + } + } + } + + /** + * Make a TreeMaker that will result in an extractor call specified by `extractor` + * the next TreeMaker (here, we don't know which it'll be) is chained after this one by flatMap'ing + * a function with binder `nextBinder` over our extractor's result + * the function's body is determined by the next TreeMaker + * (furthermore, the interpretation of `flatMap` depends on the codegen instance we're using). + * + * The values for the subpatterns, as computed by the extractor call in `extractor`, + * are stored in local variables that re-use the symbols in `subPatBinders`. + * This makes extractor patterns more debuggable (SI-5739). + */ + case class ExtractorTreeMaker(extractor: Tree, extraCond: Option[Tree], nextBinder: Symbol)( + val subPatBinders: List[Symbol], + val subPatRefs: List[Tree], + extractorReturnsBoolean: Boolean, + val checkedLength: Option[Int], + val prevBinder: Symbol, + val ignoredSubPatBinders: Set[Symbol] + ) extends FunTreeMaker with PreserveSubPatBinders { + + def extraStoredBinders: Set[Symbol] = Set() + + debug.patmat(s""" + |ExtractorTreeMaker($extractor, $extraCond, $nextBinder) { + | $subPatBinders + | $subPatRefs + | $extractorReturnsBoolean + | $checkedLength + | $prevBinder + | $ignoredSubPatBinders + |}""".stripMargin) + + def chainBefore(next: Tree)(casegen: Casegen): Tree = { + val condAndNext = extraCond match { + case Some(cond) => + casegen.ifThenElseZero(substitution(cond), bindSubPats(substitution(next))) + case _ => + bindSubPats(substitution(next)) + } + atPos(extractor.pos)( + if (extractorReturnsBoolean) casegen.flatMapCond(extractor, CODE.UNIT, nextBinder, condAndNext) + else casegen.flatMap(extractor, nextBinder, condAndNext) + ) + } + + override def toString = "X"+((extractor, nextBinder.name)) + } + + /** + * An optimized version of ExtractorTreeMaker for Products. + * For now, this is hard-coded to case classes, and we simply extract the case class fields. + * + * The values for the subpatterns, as specified by the case class fields at the time of extraction, + * are stored in local variables that re-use the symbols in `subPatBinders`. + * This makes extractor patterns more debuggable (SI-5739) as well as + * avoiding mutation after the pattern has been matched (SI-5158, SI-6070) + * + * TODO: make this user-definable as follows + * When a companion object defines a method `def unapply_1(x: T): U_1`, but no `def unapply` or `def unapplySeq`, + * the extractor is considered to match any non-null value of type T + * the pattern is expected to have as many sub-patterns as there are `def unapply_I(x: T): U_I` methods, + * and the type of the I'th sub-pattern is `U_I`. + * The same exception for Seq patterns applies: if the last extractor is of type `Seq[U_N]`, + * the pattern must have at least N arguments (exactly N if the last argument is annotated with `: _*`). + * The arguments starting at N (and beyond) are taken from the sequence returned by apply_N, + * and it is checked that that sequence has enough elements to provide values for all expected sub-patterns. + * + * For a case class C, the implementation is assumed to be `def unapply_I(x: C) = x._I`, + * and the extractor call is inlined under that assumption. + */ + case class ProductExtractorTreeMaker(prevBinder: Symbol, extraCond: Option[Tree])( + val subPatBinders: List[Symbol], + val subPatRefs: List[Tree], + val mutableBinders: List[Symbol], + binderKnownNonNull: Boolean, + val ignoredSubPatBinders: Set[Symbol] + ) extends FunTreeMaker with PreserveSubPatBinders { + + import CODE._ + val nextBinder = prevBinder // just passing through + + // mutable binders must be stored to avoid unsoundness or seeing mutation of fields after matching (SI-5158, SI-6070) + def extraStoredBinders: Set[Symbol] = mutableBinders.toSet + + def chainBefore(next: Tree)(casegen: Casegen): Tree = { + val nullCheck = REF(prevBinder) OBJ_NE NULL + val cond = + if (binderKnownNonNull) extraCond + else (extraCond map (nullCheck AND _) + orElse Some(nullCheck)) + + cond match { + case Some(cond) => + casegen.ifThenElseZero(cond, bindSubPats(substitution(next))) + case _ => + bindSubPats(substitution(next)) + } + } + + override def toString = "P"+((prevBinder.name, extraCond getOrElse "", localSubstitution)) + } + + object IrrefutableExtractorTreeMaker { + // will an extractor with unapply method of methodtype `tp` always succeed? + // note: this assumes the other side-conditions implied by the extractor are met + // (argument of the right type, length check succeeds for unapplySeq,...) + def irrefutableExtractorType(tp: Type): Boolean = tp.resultType.dealias match { + case TypeRef(_, SomeClass, _) => true + // probably not useful since this type won't be inferred nor can it be written down (yet) + case ConstantTrue => true + case _ => false + } + + def unapply(xtm: ExtractorTreeMaker): Option[(Tree, Symbol)] = xtm match { + case ExtractorTreeMaker(extractor, None, nextBinder) if irrefutableExtractorType(extractor.tpe) => + Some((extractor, nextBinder)) + case _ => + None + } + } + + object TypeTestTreeMaker { + // factored out so that we can consistently generate other representations of the tree that implements the test + // (e.g. propositions for exhaustivity and friends, boolean for isPureTypeTest) + trait TypeTestCondStrategy { + type Result + + def outerTest(testedBinder: Symbol, expectedTp: Type): Result + // TODO: can probably always widen + def typeTest(testedBinder: Symbol, expectedTp: Type): Result + def nonNullTest(testedBinder: Symbol): Result + def equalsTest(pat: Tree, testedBinder: Symbol): Result + def eqTest(pat: Tree, testedBinder: Symbol): Result + def and(a: Result, b: Result): Result + def tru: Result + } + + object treeCondStrategy extends TypeTestCondStrategy { import CODE._ + type Result = Tree + + def and(a: Result, b: Result): Result = a AND b + def tru = mkTRUE + def typeTest(testedBinder: Symbol, expectedTp: Type) = codegen._isInstanceOf(testedBinder, expectedTp) + def nonNullTest(testedBinder: Symbol) = REF(testedBinder) OBJ_NE NULL + def equalsTest(pat: Tree, testedBinder: Symbol) = codegen._equals(pat, testedBinder) + def eqTest(pat: Tree, testedBinder: Symbol) = REF(testedBinder) OBJ_EQ pat + + def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = { + val expectedOuter = expectedTp.prefix match { + case ThisType(clazz) => This(clazz) + case NoType => mkTRUE // fallback for SI-6183 + case pre => REF(pre.prefix, pre.termSymbol) + } + + // ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` + // if there's an outer accessor, otherwise the condition becomes `true` -- TODO: can we improve needsOuterTest so there's always an outerAccessor? + val outer = expectedTp.typeSymbol.newMethod(vpmName.outer, newFlags = SYNTHETIC | ARTIFACT) setInfo expectedTp.prefix + + (Select(codegen._asInstanceOf(testedBinder, expectedTp), outer)) OBJ_EQ expectedOuter + } + } + + object pureTypeTestChecker extends TypeTestCondStrategy { + type Result = Boolean + + def typeTest(testedBinder: Symbol, expectedTp: Type): Result = true + + def outerTest(testedBinder: Symbol, expectedTp: Type): Result = false + def nonNullTest(testedBinder: Symbol): Result = false + def equalsTest(pat: Tree, testedBinder: Symbol): Result = false + def eqTest(pat: Tree, testedBinder: Symbol): Result = false + def and(a: Result, b: Result): Result = false // we don't and type tests, so the conjunction must include at least one false + def tru = true + } + + def nonNullImpliedByTestChecker(binder: Symbol) = new TypeTestCondStrategy { + type Result = Boolean + + def typeTest(testedBinder: Symbol, expectedTp: Type): Result = testedBinder eq binder + def outerTest(testedBinder: Symbol, expectedTp: Type): Result = false + def nonNullTest(testedBinder: Symbol): Result = testedBinder eq binder + def equalsTest(pat: Tree, testedBinder: Symbol): Result = false // could in principle analyse pat and see if it's statically known to be non-null + def eqTest(pat: Tree, testedBinder: Symbol): Result = false // could in principle analyse pat and see if it's statically known to be non-null + def and(a: Result, b: Result): Result = a || b + def tru = false + } + } + + /** implements the run-time aspects of (ยง8.2) (typedPattern has already done the necessary type transformations) + * + * Type patterns consist of types, type variables, and wildcards. A type pattern T is of one of the following forms: + - A reference to a class C, p.C, or T#C. + This type pattern matches any non-null instance of the given class. + Note that the prefix of the class, if it is given, is relevant for determining class instances. + For instance, the pattern p.C matches only instances of classes C which were created with the path p as prefix. + The bottom types scala.Nothing and scala.Null cannot be used as type patterns, because they would match nothing in any case. + + - A singleton type p.type. + This type pattern matches only the value denoted by the path p + (that is, a pattern match involved a comparison of the matched value with p using method eq in class AnyRef). // TODO: the actual pattern matcher uses ==, so that's what I'm using for now + // https://issues.scala-lang.org/browse/SI-4577 "pattern matcher, still disappointing us at equality time" + + - A compound type pattern T1 with ... with Tn where each Ti is a type pat- tern. + This type pattern matches all values that are matched by each of the type patterns Ti. + + - A parameterized type pattern T[a1,...,an], where the ai are type variable patterns or wildcards _. + This type pattern matches all values which match T for some arbitrary instantiation of the type variables and wildcards. + The bounds or alias type of these type variable are determined as described in (ยง8.3). + + - A parameterized type pattern scala.Array[T1], where T1 is a type pattern. // TODO + This type pattern matches any non-null instance of type scala.Array[U1], where U1 is a type matched by T1. + **/ + case class TypeTestTreeMaker(prevBinder: Symbol, testedBinder: Symbol, expectedTp: Type, nextBinderTp: Type)(override val pos: Position, extractorArgTypeTest: Boolean = false) extends CondTreeMaker { + import TypeTestTreeMaker._ + debug.patmat("TTTM"+((prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp))) + + lazy val outerTestNeeded = ( + (expectedTp.prefix ne NoPrefix) + && !expectedTp.prefix.typeSymbol.isPackageClass + && needsOuterTest(expectedTp, testedBinder.info, matchOwner) + ) + + // the logic to generate the run-time test that follows from the fact that + // a `prevBinder` is expected to have type `expectedTp` + // the actual tree-generation logic is factored out, since the analyses generate Cond(ition)s rather than Trees + // TODO: `null match { x : T }` will yield a check that (indirectly) tests whether `null ne null` + // don't bother (so that we don't end up with the warning "comparing values of types Null and Null using `ne' will always yield false") + def renderCondition(cs: TypeTestCondStrategy): cs.Result = { + import cs._ + + // propagate expected type + def expTp(t: Tree): t.type = t setType expectedTp + + def testedWide = testedBinder.info.widen + def expectedWide = expectedTp.widen + def isAnyRef = testedWide <:< AnyRefTpe + def isAsExpected = testedWide <:< expectedTp + def isExpectedPrimitiveType = isAsExpected && isPrimitiveValueType(expectedTp) + def isExpectedReferenceType = isAsExpected && (expectedTp <:< AnyRefTpe) + def mkNullTest = nonNullTest(testedBinder) + def mkOuterTest = outerTest(testedBinder, expectedTp) + def mkTypeTest = typeTest(testedBinder, expectedWide) + + def mkEqualsTest(lhs: Tree): cs.Result = equalsTest(lhs, testedBinder) + def mkEqTest(lhs: Tree): cs.Result = eqTest(lhs, testedBinder) + def addOuterTest(res: cs.Result): cs.Result = if (outerTestNeeded) and(res, mkOuterTest) else res + + // If we conform to expected primitive type: + // it cannot be null and cannot have an outer pointer. No further checking. + // If we conform to expected reference type: + // have to test outer and non-null + // If we do not conform to expected type: + // have to test type and outer (non-null is implied by successful type test) + def mkDefault = ( + if (isExpectedPrimitiveType) tru + else addOuterTest( + if (isExpectedReferenceType) mkNullTest + else mkTypeTest + ) + ) + + // true when called to type-test the argument to an extractor + // don't do any fancy equality checking, just test the type + // TODO: verify that we don't need to special-case Array + // I think it's okay: + // - the isInstanceOf test includes a test for the element type + // - Scala's arrays are invariant (so we don't drop type tests unsoundly) + if (extractorArgTypeTest) mkDefault + else expectedTp match { + case SingleType(_, sym) => mkEqTest(gen.mkAttributedQualifier(expectedTp)) // SI-4577, SI-4897 + case ThisType(sym) if sym.isModule => and(mkEqualsTest(CODE.REF(sym)), mkTypeTest) // must use == to support e.g. List() == Nil + case ConstantType(Constant(null)) if isAnyRef => mkEqTest(expTp(CODE.NULL)) + case ConstantType(const) => mkEqualsTest(expTp(Literal(const))) + case ThisType(sym) => mkEqTest(expTp(This(sym))) + case _ => mkDefault + } + } + + val cond = renderCondition(treeCondStrategy) + val res = codegen._asInstanceOf(testedBinder, nextBinderTp) + + // is this purely a type test, e.g. no outer check, no equality tests (used in switch emission) + def isPureTypeTest = renderCondition(pureTypeTestChecker) + + def impliesBinderNonNull(binder: Symbol) = renderCondition(nonNullImpliedByTestChecker(binder)) + + override def toString = "TT"+((expectedTp, testedBinder.name, nextBinderTp)) + } + + // need to substitute to deal with existential types -- TODO: deal with existentials better, don't substitute (see RichClass during quick.comp) + case class EqualityTestTreeMaker(prevBinder: Symbol, patTree: Tree, override val pos: Position) extends CondTreeMaker { + val nextBinderTp = prevBinder.info.widen + + // NOTE: generate `patTree == patBinder`, since the extractor must be in control of the equals method (also, patBinder may be null) + // equals need not be well-behaved, so don't intersect with pattern's (stabilized) type (unlike MaybeBoundTyped's accumType, where it's required) + val cond = codegen._equals(patTree, prevBinder) + val res = CODE.REF(prevBinder) + override def toString = "ET"+((prevBinder.name, patTree)) + } + + case class AlternativesTreeMaker(prevBinder: Symbol, var altss: List[List[TreeMaker]], pos: Position) extends TreeMaker with NoNewBinders { + // don't substitute prevBinder to nextBinder, a set of alternatives does not need to introduce a new binder, simply reuse the previous one + + override private[TreeMakers] def incorporateOuterSubstitution(outerSubst: Substitution): Unit = { + super.incorporateOuterSubstitution(outerSubst) + altss = altss map (alts => propagateSubstitution(alts, substitution)) + } + + def chainBefore(next: Tree)(codegenAlt: Casegen): Tree = { + atPos(pos){ + // one alternative may still generate multiple trees (e.g., an extractor call + equality test) + // (for now,) alternatives may not bind variables (except wildcards), so we don't care about the final substitution built internally by makeTreeMakers + val combinedAlts = altss map (altTreeMakers => + ((casegen: Casegen) => combineExtractors(altTreeMakers :+ TrivialTreeMaker(casegen.one(mkTRUE)))(casegen)) + ) + + val findAltMatcher = codegenAlt.matcher(EmptyTree, NoSymbol, BooleanTpe)(combinedAlts, Some(x => mkFALSE)) + codegenAlt.ifThenElseZero(findAltMatcher, substitution(next)) + } + } + } + + case class GuardTreeMaker(guardTree: Tree) extends TreeMaker with NoNewBinders { + val pos = guardTree.pos + + def chainBefore(next: Tree)(casegen: Casegen): Tree = casegen.flatMapGuard(substitution(guardTree), next) + override def toString = "G("+ guardTree +")" + } + + // combineExtractors changes the current substitution's of the tree makers in `treeMakers` + // requires propagateSubstitution(treeMakers) has been called + def combineExtractors(treeMakers: List[TreeMaker])(casegen: Casegen): Tree = + treeMakers.foldRight(EmptyTree: Tree)((a, b) => a.chainBefore(b)(casegen)) + + + def removeSubstOnly(makers: List[TreeMaker]) = makers filterNot (_.isInstanceOf[SubstOnlyTreeMaker]) + + // a foldLeft to accumulate the localSubstitution left-to-right + // it drops SubstOnly tree makers, since their only goal in life is to propagate substitutions to the next tree maker, which is fullfilled by propagateSubstitution + def propagateSubstitution(treeMakers: List[TreeMaker], initial: Substitution): List[TreeMaker] = { + var accumSubst: Substitution = initial + treeMakers foreach { maker => + maker incorporateOuterSubstitution accumSubst + accumSubst = maker.substitution + } + removeSubstOnly(treeMakers) + } + + // calls propagateSubstitution on the treemakers + def combineCases(scrut: Tree, scrutSym: Symbol, casesRaw: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Tree => Tree]): Tree = { + // drops SubstOnlyTreeMakers, since their effect is now contained in the TreeMakers that follow them + val casesNoSubstOnly = casesRaw map (propagateSubstitution(_, EmptySubstitution)) + combineCasesNoSubstOnly(scrut, scrutSym, casesNoSubstOnly, pt, owner, matchFailGenOverride) + } + + // pt is the fully defined type of the cases (either pt or the lub of the types of the cases) + def combineCasesNoSubstOnly(scrut: Tree, scrutSym: Symbol, casesNoSubstOnly: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Tree => Tree]): Tree = + fixerUpper(owner, scrut.pos) { + def matchFailGen = matchFailGenOverride orElse Some(Throw(MatchErrorClass.tpe, _: Tree)) + + debug.patmat("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) + + val (suppression, requireSwitch): (Suppression, Boolean) = + if (settings.XnoPatmatAnalysis) (Suppression.NoSuppression, false) + else scrut match { + case Typed(tree, tpt) => + val suppressExhaustive = tpt.tpe hasAnnotation UncheckedClass + val supressUnreachable = tree match { + case Ident(name) if name startsWith nme.CHECK_IF_REFUTABLE_STRING => true // SI-7183 don't warn for withFilter's that turn out to be irrefutable. + case _ => false + } + val suppression = Suppression(suppressExhaustive, supressUnreachable) + // matches with two or fewer cases need not apply for switchiness (if-then-else will do) + val requireSwitch = treeInfo.isSwitchAnnotation(tpt.tpe) && casesNoSubstOnly.lengthCompare(2) > 0 + (suppression, requireSwitch) + case _ => + (Suppression.NoSuppression, false) + } + + emitSwitch(scrut, scrutSym, casesNoSubstOnly, pt, matchFailGenOverride, suppression.exhaustive).getOrElse{ + if (requireSwitch) typer.context.unit.warning(scrut.pos, "could not emit switch for @switch annotated match") + + if (casesNoSubstOnly nonEmpty) { + // before optimizing, check casesNoSubstOnly for presence of a default case, + // since DCE will eliminate trivial cases like `case _ =>`, even if they're the last one + // exhaustivity and reachability must be checked before optimization as well + // TODO: improve notion of trivial/irrefutable -- a trivial type test before the body still makes for a default case + // ("trivial" depends on whether we're emitting a straight match or an exception, or more generally, any supertype of scrutSym.tpe is a no-op) + // irrefutability checking should use the approximation framework also used for CSE, unreachability and exhaustivity checking + val synthCatchAll = + if (casesNoSubstOnly.nonEmpty && { + val nonTrivLast = casesNoSubstOnly.last + nonTrivLast.nonEmpty && nonTrivLast.head.isInstanceOf[BodyTreeMaker] + }) None + else matchFailGen + + analyzeCases(scrutSym, casesNoSubstOnly, pt, suppression) + + val (cases, toHoist) = optimizeCases(scrutSym, casesNoSubstOnly, pt) + + val matchRes = codegen.matcher(scrut, scrutSym, pt)(cases map combineExtractors, synthCatchAll) + + if (toHoist isEmpty) matchRes else Block(toHoist, matchRes) + } else { + codegen.matcher(scrut, scrutSym, pt)(Nil, matchFailGen) + } + } + } + + // TODO: do this during tree construction, but that will require tracking the current owner in treemakers + // TODO: assign more fine-grained positions + // fixes symbol nesting, assigns positions + protected def fixerUpper(origOwner: Symbol, pos: Position) = new Traverser { + currentOwner = origOwner + + override def traverse(t: Tree) { + if (t != EmptyTree && t.pos == NoPosition) { + t.setPos(pos) + } + t match { + case Function(_, _) if t.symbol == NoSymbol => + t.symbol = currentOwner.newAnonymousFunctionValue(t.pos) + debug.patmat("new symbol for "+ ((t, t.symbol.ownerChain))) + case Function(_, _) if (t.symbol.owner == NoSymbol) || (t.symbol.owner == origOwner) => + debug.patmat("fundef: "+ ((t, t.symbol.ownerChain, currentOwner.ownerChain))) + t.symbol.owner = currentOwner + case d : DefTree if (d.symbol != NoSymbol) && ((d.symbol.owner == NoSymbol) || (d.symbol.owner == origOwner)) => // don't indiscriminately change existing owners! (see e.g., pos/t3440, pos/t3534, pos/unapplyContexts2) + debug.patmat("def: "+ ((d, d.symbol.ownerChain, currentOwner.ownerChain))) + + d.symbol.moduleClass andAlso (_.owner = currentOwner) + d.symbol.owner = currentOwner + // TODO DD: + // case _ if (t.symbol != NoSymbol) && (t.symbol ne null) => + debug.patmat("untouched "+ ((t, t.getClass, t.symbol.ownerChain, currentOwner.ownerChain))) + case _ => + } + super.traverse(t) + } + + // override def apply + // debug.patmat("before fixerupper: "+ xTree) + // currentRun.trackerFactory.snapshot() + // debug.patmat("after fixerupper") + // currentRun.trackerFactory.snapshot() + } + } + + trait MatchOptimizer extends OptimizedCodegen + /*with SwitchEmission // todo: toBe ported + with CommonSubconditionElimination*/ { + override def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) = { + // TODO: do CSE on result of doDCE(prevBinder, cases, pt) + val optCases = cases// todo: doCSE(prevBinder, cases, pt) + val toHoist = ( + for (treeMakers <- optCases) + yield treeMakers.collect{case tm: ReusedCondTreeMaker => tm.treesToHoist} + ).flatten.flatten.toList + (optCases, toHoist) + } + } } \ No newline at end of file From 55fd952163ac2d234e8ddd2677a7858690efebd2 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:37:27 +0200 Subject: [PATCH 12/23] add Substitution to patmat. Some compilation errors fixed --- .../tools/dotc/transform/PatternMatcher.scala | 188 +++++++++++------- 1 file changed, 119 insertions(+), 69 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 2d0dfe00f668..eac98ce2ef77 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -10,7 +10,7 @@ import core.Types._ import core.Constants._ import core.StdNames._ import core.transform.Erasure.isUnboundedGeneric -import dotty.tools.dotc.transform.PatternMatcher.CodegenCore.Casegen +import dotty.tools.dotc.ast.tpd import dotty.tools.dotc.util.Positions import typer.ErrorReporting._ import ast.Trees._ @@ -19,6 +19,8 @@ import dotty.tools.dotc.util.Positions.Position import dotty.tools.dotc.core.Decorators._ import dotty.tools.dotc.core.Flags +import scala.reflect.internal.util.Collections + /** This transform eliminates patterns. Right now it's a dummy. * Awaiting the real pattern matcher. */ @@ -161,7 +163,60 @@ class PatternMatcher extends TreeTransform { } } - trait OptimizedCodegen extends CodegenCore /*with TypedSubstitution*/ with MatchMonadInterface { + trait TypedSubstitution extends MatchMonadInterface { + object Substitution { + def apply(from: Symbol, to: Tree) = new Substitution(List(from), List(to)) + // requires sameLength(from, to) + def apply(from: List[Symbol], to: List[Tree]) = + if (from nonEmpty) new Substitution(from, to) else EmptySubstitution + } + + class Substitution(val from: List[Symbol], val to: List[Tree]) { + + // We must explicitly type the trees that we replace inside some other tree, since the latter may already have been typed, + // and will thus not be retyped. This means we might end up with untyped subtrees inside bigger, typed trees. + def apply(tree: Tree): Tree = { + // according to -Ystatistics 10% of translateMatch's time is spent in this method... + // since about half of the typedSubst's end up being no-ops, the check below shaves off 5% of the time spent in typedSubst + /*if (!tree.exists { case i@Ident(_) => from contains i.symbol case _ => false}) tree + else*/ (new TreeMap { + /*private def typedIfOrigTyped(to: Tree, origTp: Type): Tree = + if (origTp == null || origTp == NoType) to + // important: only type when actually substing and when original tree was typed + // (don't need to use origTp as the expected type, though, and can't always do this anyway due to unknown type params stemming from polymorphic extractors) + else typer.typed(to)*/ + + override def transform(tree: Tree)(implicit ctx: Context): Tree = { + def subst(from: List[Symbol], to: List[Tree]): Tree = + if (from.isEmpty) tree + else if (tree.symbol == from.head) to.head //typedIfOrigTyped(to.head.shallowDuplicate.setPos(tree.pos), tree.tpe) + else subst(from.tail, to.tail) + + tree match { + case Ident(_) => subst(from, to) + case _ => super.transform(tree) + } + } + }).transform(tree) + } + + + // the substitution that chains `other` before `this` substitution + // forall t: Tree. this(other(t)) == (this >> other)(t) + def >>(other: Substitution): Substitution = { + val (fromFiltered, toFiltered) = (from, to).zipped filter { (f, t) => !other.from.contains(f) } + new Substitution(other.from ++ fromFiltered, other.to.map(apply) ++ toFiltered) // a quick benchmarking run indicates the `.map(apply)` is not too costly + } + override def toString = (from.map(_.name) zip to) mkString("Substitution(", ", ", ")") + } + + object EmptySubstitution extends Substitution(Nil, Nil) { + override def apply(tree: Tree): Tree = tree + override def >>(other: Substitution): Substitution = other + } + } + + trait OptimizedCodegen extends CodegenCore with TypedSubstitution with MatchMonadInterface { override def codegen: AbsCodegen = optimizedCodegen // when we know we're targetting Option, do some inlining the optimizer won't do @@ -239,7 +294,7 @@ class PatternMatcher extends TreeTransform { // must be isEmpty and get as we don't control the target of the call (prev is an extractor call) ifThenElseZero( ref(prevSym).select("isEmpty".toTermName).select(ctx.definitions.Boolean_!), - /*Substitution(b, prevSym DOT vpmName.get)*/(next) // todo? + Substitution(b, ref(prevSym).select("get".toTermName))(next) ) ) } @@ -278,7 +333,7 @@ class PatternMatcher extends TreeTransform { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // the making of the trees /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - trait TreeMakers /*extends TypedSubstitution*/ extends CodegenCore { + trait TreeMakers extends TypedSubstitution with CodegenCore { def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit @@ -295,20 +350,20 @@ class PatternMatcher extends TreeTransform { /** captures the scope and the value of the bindings in patterns * important *when* the substitution happens (can't accumulate and do at once after the full matcher has been constructed) */ - /*def substitution: Substitution = + def substitution: Substitution = if (currSub eq null) localSubstitution - else currSub*/ + else currSub - //protected def localSubstitution: Substitution + protected def localSubstitution: Substitution - /*private[TreeMakers] def incorporateOuterSubstitution(outerSubst: Substitution): Unit = { + private[TreeMakers] def incorporateOuterSubstitution(outerSubst: Substitution): Unit = { if (currSub ne null) { - debug.patmat("BUG: incorporateOuterSubstitution called more than once for "+ ((this, currSub, outerSubst))) + println("BUG: incorporateOuterSubstitution called more than once for "+ ((this, currSub, outerSubst))) Thread.dumpStack() } else currSub = outerSubst >> substitution } - private[this] var currSub: Substitution = null*/ + private[this] var currSub: Substitution = null /** The substitution that specifies the trees that compute the values of the subpattern binders. * @@ -322,14 +377,14 @@ class PatternMatcher extends TreeTransform { * TODO: clean this up, would be nicer to have some higher-level way to compute * the binders bound by this tree maker and the symbolic values that correspond to them */ - // def subPatternsAsSubstitution: Substitution = substitution + def subPatternsAsSubstitution: Substitution = substitution // build Tree that chains `next` after the current extractor def chainBefore(next: Tree)(casegen: Casegen): Tree } sealed trait NoNewBinders extends TreeMaker { - //protected val localSubstitution: Substitution = EmptySubstitution + protected val localSubstitution: Substitution = EmptySubstitution } case class TrivialTreeMaker(tree: Tree) extends TreeMaker with NoNewBinders { @@ -349,9 +404,9 @@ class PatternMatcher extends TreeTransform { case class SubstOnlyTreeMaker(prevBinder: Symbol, nextBinder: Symbol) extends TreeMaker { val pos = Positions.NoPosition - //val localSubstitution = Substitution(prevBinder, CODE.REF(nextBinder)) + val localSubstitution = Substitution(prevBinder, ref(nextBinder)) def chainBefore(next: Tree)(casegen: Casegen): Tree = /*substitution(*/next//) - override def toString = "S"//+ localSubstitution + override def toString = "S" + localSubstitution } sealed abstract class FunTreeMaker extends TreeMaker { @@ -366,10 +421,10 @@ class PatternMatcher extends TreeTransform { val res: Tree lazy val nextBinder = freshSym(pos, nextBinderTp) - //lazy val localSubstitution = Substitution(List(prevBinder), List(CODE.REF(nextBinder))) + lazy val localSubstitution = Substitution(List(prevBinder), List(ref(nextBinder))) def chainBefore(next: Tree)(casegen: Casegen): Tree = - /*atPos(pos)(*/casegen.flatMapCond(cond, res, nextBinder, /*substitution(*/next/*)*/)) + /*atPos(pos)(*/casegen.flatMapCond(cond, res, nextBinder, substitution(next))//) } // unless we're optimizing, emit local variable bindings for all subpatterns of extractor/case class patterns @@ -412,17 +467,17 @@ class PatternMatcher extends TreeTransform { if (!emitVars) in else { // binders in `subPatBindersStored` that are referenced by tree `in` - val usedBinders = new mutable.HashSet[Symbol]() + val usedBinders = new collection.mutable.HashSet[Symbol]() // all potentially stored subpat binders val potentiallyStoredBinders = stored.unzip._1.toSet // compute intersection of all symbols in the tree `in` and all potentially stored subpat binders - in.foreach(t => if (potentiallyStoredBinders(t.symbol)) usedBinders += t.symbol) + new DeepFolder[Unit]((x: Unit, t:Tree) => if (potentiallyStoredBinders(t.symbol)) usedBinders += t.symbol).apply((), in) if (usedBinders.isEmpty) in else { // only store binders actually used val (subPatBindersStored, subPatRefsStored) = stored.filter{case (b, _) => usedBinders(b)}.unzip - Block(map2(subPatBindersStored.toList, subPatRefsStored.toList)(ValDef(_, _)), in) + Block(Collections.map2(subPatBindersStored.toList, subPatRefsStored.toList)((bind, ref) => ValDef(bind.asTerm, ref)), in) } } } @@ -449,7 +504,7 @@ class PatternMatcher extends TreeTransform { def extraStoredBinders: Set[Symbol] = Set() - debug.patmat(s""" + println(s""" |ExtractorTreeMaker($extractor, $extraCond, $nextBinder) { | $subPatBinders | $subPatRefs @@ -461,15 +516,15 @@ class PatternMatcher extends TreeTransform { def chainBefore(next: Tree)(casegen: Casegen): Tree = { val condAndNext = extraCond match { - case Some(cond) => + case Some(cond: Tree) => casegen.ifThenElseZero(substitution(cond), bindSubPats(substitution(next))) case _ => bindSubPats(substitution(next)) } - atPos(extractor.pos)( - if (extractorReturnsBoolean) casegen.flatMapCond(extractor, CODE.UNIT, nextBinder, condAndNext) + + if (extractorReturnsBoolean) casegen.flatMapCond(extractor, unitLiteral, nextBinder, condAndNext) else casegen.flatMap(extractor, nextBinder, condAndNext) - ) + } override def toString = "X"+((extractor, nextBinder.name)) @@ -505,21 +560,19 @@ class PatternMatcher extends TreeTransform { val ignoredSubPatBinders: Set[Symbol] ) extends FunTreeMaker with PreserveSubPatBinders { - import CODE._ val nextBinder = prevBinder // just passing through // mutable binders must be stored to avoid unsoundness or seeing mutation of fields after matching (SI-5158, SI-6070) def extraStoredBinders: Set[Symbol] = mutableBinders.toSet def chainBefore(next: Tree)(casegen: Casegen): Tree = { - val nullCheck = REF(prevBinder) OBJ_NE NULL - val cond = + val nullCheck: Tree = ref(prevBinder).select(ctx.definitions.Object_ne).appliedTo(Literal(Constant(null))) + val cond: Option[Tree] = if (binderKnownNonNull) extraCond - else (extraCond map (nullCheck AND _) - orElse Some(nullCheck)) + else extraCond.map(nullCheck.select(ctx.definitions.Boolean_and).appliedTo).orElse(Some(nullCheck)) cond match { - case Some(cond) => + case Some(cond: Tree) => casegen.ifThenElseZero(cond, bindSubPats(substitution(next))) case _ => bindSubPats(substitution(next)) @@ -534,9 +587,9 @@ class PatternMatcher extends TreeTransform { // note: this assumes the other side-conditions implied by the extractor are met // (argument of the right type, length check succeeds for unapplySeq,...) def irrefutableExtractorType(tp: Type): Boolean = tp.resultType.dealias match { - case TypeRef(_, SomeClass, _) => true + // case TypeRef(_, SomeClass, _) => true todo // probably not useful since this type won't be inferred nor can it be written down (yet) - case ConstantTrue => true + // case ConstantTrue => true todo case _ => false } @@ -564,17 +617,17 @@ class PatternMatcher extends TreeTransform { def tru: Result } - object treeCondStrategy extends TypeTestCondStrategy { import CODE._ + object treeCondStrategy extends TypeTestCondStrategy { type Result = Tree - def and(a: Result, b: Result): Result = a AND b - def tru = mkTRUE + def and(a: Result, b: Result): Result = a.select(ctx.definitions.Boolean_and).appliedTo(b) + def tru = Literal(Constant(true)) def typeTest(testedBinder: Symbol, expectedTp: Type) = codegen._isInstanceOf(testedBinder, expectedTp) - def nonNullTest(testedBinder: Symbol) = REF(testedBinder) OBJ_NE NULL + def nonNullTest(testedBinder: Symbol) = ref(testedBinder).select(ctx.definitions.Object_ne).appliedTo(Literal(Constant(null))) def equalsTest(pat: Tree, testedBinder: Symbol) = codegen._equals(pat, testedBinder) - def eqTest(pat: Tree, testedBinder: Symbol) = REF(testedBinder) OBJ_EQ pat + def eqTest(pat: Tree, testedBinder: Symbol) = ref(testedBinder).select(ctx.definitions.Object_eq).appliedTo(pat) - def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = { + def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = ??? /*{ val expectedOuter = expectedTp.prefix match { case ThisType(clazz) => This(clazz) case NoType => mkTRUE // fallback for SI-6183 @@ -586,7 +639,7 @@ class PatternMatcher extends TreeTransform { val outer = expectedTp.typeSymbol.newMethod(vpmName.outer, newFlags = SYNTHETIC | ARTIFACT) setInfo expectedTp.prefix (Select(codegen._asInstanceOf(testedBinder, expectedTp), outer)) OBJ_EQ expectedOuter - } + }*/ } object pureTypeTestChecker extends TypeTestCondStrategy { @@ -641,12 +694,12 @@ class PatternMatcher extends TreeTransform { **/ case class TypeTestTreeMaker(prevBinder: Symbol, testedBinder: Symbol, expectedTp: Type, nextBinderTp: Type)(override val pos: Position, extractorArgTypeTest: Boolean = false) extends CondTreeMaker { import TypeTestTreeMaker._ - debug.patmat("TTTM"+((prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp))) + println("TTTM"+((prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp))) lazy val outerTestNeeded = ( - (expectedTp.prefix ne NoPrefix) - && !expectedTp.prefix.typeSymbol.isPackageClass - && needsOuterTest(expectedTp, testedBinder.info, matchOwner) + (expectedTp.normalizedPrefix ne NoPrefix) + && !expectedTp.normalizedPrefix.typeSymbol.isPackageObject + && true //needsOuterTest(expectedTp, testedBinder.info, matchOwner) // todo ) // the logic to generate the run-time test that follows from the fact that @@ -658,14 +711,14 @@ class PatternMatcher extends TreeTransform { import cs._ // propagate expected type - def expTp(t: Tree): t.type = t setType expectedTp + def expTp(t: Tree): t.type = t // setType expectedTp todo: def testedWide = testedBinder.info.widen def expectedWide = expectedTp.widen - def isAnyRef = testedWide <:< AnyRefTpe + def isAnyRef = testedWide <:< ctx.definitions.AnyRefType def isAsExpected = testedWide <:< expectedTp - def isExpectedPrimitiveType = isAsExpected && isPrimitiveValueType(expectedTp) - def isExpectedReferenceType = isAsExpected && (expectedTp <:< AnyRefTpe) + def isExpectedPrimitiveType = isAsExpected && ctx.definitions.ScalaValueClasses.contains(expectedTp.classSymbol) + def isExpectedReferenceType = isAsExpected && (expectedTp <:< ctx.definitions.AnyRefType) def mkNullTest = nonNullTest(testedBinder) def mkOuterTest = outerTest(testedBinder, expectedTp) def mkTypeTest = typeTest(testedBinder, expectedWide) @@ -696,9 +749,9 @@ class PatternMatcher extends TreeTransform { // - Scala's arrays are invariant (so we don't drop type tests unsoundly) if (extractorArgTypeTest) mkDefault else expectedTp match { - case SingleType(_, sym) => mkEqTest(gen.mkAttributedQualifier(expectedTp)) // SI-4577, SI-4897 - case ThisType(sym) if sym.isModule => and(mkEqualsTest(CODE.REF(sym)), mkTypeTest) // must use == to support e.g. List() == Nil - case ConstantType(Constant(null)) if isAnyRef => mkEqTest(expTp(CODE.NULL)) + case t:SingletonType => mkEqTest(singleton(expectedTp)) // SI-4577, SI-4897 + case ThisType(sym) if sym.flags is Flags.Module => and(mkEqualsTest(ref(sym)), mkTypeTest) // must use == to support e.g. List() == Nil + case ConstantType(Constant(null)) if isAnyRef => mkEqTest(expTp(Literal(Constant(null)))) case ConstantType(const) => mkEqualsTest(expTp(Literal(const))) case ThisType(sym) => mkEqTest(expTp(This(sym))) case _ => mkDefault @@ -723,7 +776,7 @@ class PatternMatcher extends TreeTransform { // NOTE: generate `patTree == patBinder`, since the extractor must be in control of the equals method (also, patBinder may be null) // equals need not be well-behaved, so don't intersect with pattern's (stabilized) type (unlike MaybeBoundTyped's accumType, where it's required) val cond = codegen._equals(patTree, prevBinder) - val res = CODE.REF(prevBinder) + val res = ref(prevBinder) override def toString = "ET"+((prevBinder.name, patTree)) } @@ -736,14 +789,14 @@ class PatternMatcher extends TreeTransform { } def chainBefore(next: Tree)(codegenAlt: Casegen): Tree = { - atPos(pos){ + /*atPos(pos)*/{ // one alternative may still generate multiple trees (e.g., an extractor call + equality test) // (for now,) alternatives may not bind variables (except wildcards), so we don't care about the final substitution built internally by makeTreeMakers val combinedAlts = altss map (altTreeMakers => - ((casegen: Casegen) => combineExtractors(altTreeMakers :+ TrivialTreeMaker(casegen.one(mkTRUE)))(casegen)) + ((casegen: Casegen) => combineExtractors(altTreeMakers :+ TrivialTreeMaker(casegen.one(Literal(Constant(true)))))(casegen)) ) - val findAltMatcher = codegenAlt.matcher(EmptyTree, NoSymbol, BooleanTpe)(combinedAlts, Some(x => mkFALSE)) + val findAltMatcher = codegenAlt.matcher(EmptyTree, NoSymbol, ctx.definitions.BooleanType)(combinedAlts, Some(x => Literal(Constant(false)))) codegenAlt.ifThenElseZero(findAltMatcher, substitution(next)) } } @@ -784,14 +837,14 @@ class PatternMatcher extends TreeTransform { // pt is the fully defined type of the cases (either pt or the lub of the types of the cases) def combineCasesNoSubstOnly(scrut: Tree, scrutSym: Symbol, casesNoSubstOnly: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Tree => Tree]): Tree = - fixerUpper(owner, scrut.pos) { - def matchFailGen = matchFailGenOverride orElse Some(Throw(MatchErrorClass.tpe, _: Tree)) + /*fixerUpper(owner, scrut.pos)*/ { + def matchFailGen = matchFailGenOverride orElse Some(arg: Tree => Throw(New(defn.MatchErrorClass, arg))) - debug.patmat("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) + println("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) val (suppression, requireSwitch): (Suppression, Boolean) = - if (settings.XnoPatmatAnalysis) (Suppression.NoSuppression, false) - else scrut match { + /*if (settings.XnoPatmatAnalysis)*/ (Suppression.NoSuppression, false) + /*else scrut match { case Typed(tree, tpt) => val suppressExhaustive = tpt.tpe hasAnnotation UncheckedClass val supressUnreachable = tree match { @@ -804,10 +857,10 @@ class PatternMatcher extends TreeTransform { (suppression, requireSwitch) case _ => (Suppression.NoSuppression, false) - } + }*/ emitSwitch(scrut, scrutSym, casesNoSubstOnly, pt, matchFailGenOverride, suppression.exhaustive).getOrElse{ - if (requireSwitch) typer.context.unit.warning(scrut.pos, "could not emit switch for @switch annotated match") + if (requireSwitch) ctx.warning("could not emit switch for @switch annotated match", scrut.pos) if (casesNoSubstOnly nonEmpty) { // before optimizing, check casesNoSubstOnly for presence of a default case, @@ -839,13 +892,10 @@ class PatternMatcher extends TreeTransform { // TODO: do this during tree construction, but that will require tracking the current owner in treemakers // TODO: assign more fine-grained positions // fixes symbol nesting, assigns positions - protected def fixerUpper(origOwner: Symbol, pos: Position) = new Traverser { + /*protected def fixerUpper(origOwner: Symbol, pos: Position) = new Traverser { currentOwner = origOwner override def traverse(t: Tree) { - if (t != EmptyTree && t.pos == NoPosition) { - t.setPos(pos) - } t match { case Function(_, _) if t.symbol == NoSymbol => t.symbol = currentOwner.newAnonymousFunctionValue(t.pos) @@ -871,19 +921,19 @@ class PatternMatcher extends TreeTransform { // currentRun.trackerFactory.snapshot() // debug.patmat("after fixerupper") // currentRun.trackerFactory.snapshot() - } + }*/ } - trait MatchOptimizer extends OptimizedCodegen + trait MatchOptimizer extends OptimizedCodegen with TreeMakers /*with SwitchEmission // todo: toBe ported with CommonSubconditionElimination*/ { override def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) = { // TODO: do CSE on result of doDCE(prevBinder, cases, pt) val optCases = cases// todo: doCSE(prevBinder, cases, pt) - val toHoist = ( + val toHoist = Nil/*( for (treeMakers <- optCases) yield treeMakers.collect{case tm: ReusedCondTreeMaker => tm.treesToHoist} - ).flatten.flatten.toList + ).flatten.flatten.toList*/ (optCases, toHoist) } } From 95302306c05ab30455d4a9cc112bca75901d1cf6 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:43:43 +0200 Subject: [PATCH 13/23] Match translator for patmat --- .../tools/dotc/transform/PatternMatcher.scala | 747 +++++++++++++++++- 1 file changed, 732 insertions(+), 15 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index eac98ce2ef77..5561fd0d0762 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -58,8 +58,7 @@ class PatternMatcher extends TreeTransform { new OptimizingMatchTranslator/*(localTyper)*/ } - class OptimizingMatchTranslator/*(val typer: analyzer.Typer)*/ /*extends MatchTranslator - with MatchOptimizer*/ + class OptimizingMatchTranslator extends MatchOptimizer/*(val typer: analyzer.Typer)*/ /*extends MatchTranslator*/ trait Debugging { @@ -337,7 +336,7 @@ class PatternMatcher extends TreeTransform { def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit - def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Tree => Tree], unchecked: Boolean): Option[Tree] = + def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Symbol => Tree], unchecked: Boolean): Option[Tree] = None // for catch (no need to customize match failure) @@ -627,19 +626,19 @@ class PatternMatcher extends TreeTransform { def equalsTest(pat: Tree, testedBinder: Symbol) = codegen._equals(pat, testedBinder) def eqTest(pat: Tree, testedBinder: Symbol) = ref(testedBinder).select(ctx.definitions.Object_eq).appliedTo(pat) - def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = ??? /*{ - val expectedOuter = expectedTp.prefix match { + def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = { + val expectedOuter = expectedTp.normalizedPrefix match { case ThisType(clazz) => This(clazz) - case NoType => mkTRUE // fallback for SI-6183 - case pre => REF(pre.prefix, pre.termSymbol) + case NoType => Literal(Constant(true)) // fallback for SI-6183 todo? + case pre => ref(pre.termSymbol) } // ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` // if there's an outer accessor, otherwise the condition becomes `true` -- TODO: can we improve needsOuterTest so there's always an outerAccessor? - val outer = expectedTp.typeSymbol.newMethod(vpmName.outer, newFlags = SYNTHETIC | ARTIFACT) setInfo expectedTp.prefix + // val outer = expectedTp.typeSymbol.newMethod(vpmName.outer, newFlags = SYNTHETIC | ARTIFACT) setInfo expectedTp.prefix - (Select(codegen._asInstanceOf(testedBinder, expectedTp), outer)) OBJ_EQ expectedOuter - }*/ + codegen._asInstanceOf(testedBinder, expectedTp).select("".toTermName).select(ctx.definitions.Object_eq).appliedTo(expectedOuter) + } } object pureTypeTestChecker extends TypeTestCondStrategy { @@ -749,8 +748,8 @@ class PatternMatcher extends TreeTransform { // - Scala's arrays are invariant (so we don't drop type tests unsoundly) if (extractorArgTypeTest) mkDefault else expectedTp match { - case t:SingletonType => mkEqTest(singleton(expectedTp)) // SI-4577, SI-4897 case ThisType(sym) if sym.flags is Flags.Module => and(mkEqualsTest(ref(sym)), mkTypeTest) // must use == to support e.g. List() == Nil + case t:SingletonType => mkEqTest(singleton(expectedTp)) // SI-4577, SI-4897 case ConstantType(Constant(null)) if isAnyRef => mkEqTest(expTp(Literal(Constant(null)))) case ConstantType(const) => mkEqualsTest(expTp(Literal(const))) case ThisType(sym) => mkEqTest(expTp(This(sym))) @@ -829,16 +828,16 @@ class PatternMatcher extends TreeTransform { } // calls propagateSubstitution on the treemakers - def combineCases(scrut: Tree, scrutSym: Symbol, casesRaw: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Tree => Tree]): Tree = { + def combineCases(scrut: Tree, scrutSym: Symbol, casesRaw: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Symbol => Tree]): Tree = { // drops SubstOnlyTreeMakers, since their effect is now contained in the TreeMakers that follow them val casesNoSubstOnly = casesRaw map (propagateSubstitution(_, EmptySubstitution)) combineCasesNoSubstOnly(scrut, scrutSym, casesNoSubstOnly, pt, owner, matchFailGenOverride) } // pt is the fully defined type of the cases (either pt or the lub of the types of the cases) - def combineCasesNoSubstOnly(scrut: Tree, scrutSym: Symbol, casesNoSubstOnly: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Tree => Tree]): Tree = + def combineCasesNoSubstOnly(scrut: Tree, scrutSym: Symbol, casesNoSubstOnly: List[List[TreeMaker]], pt: Type, owner: Symbol, matchFailGenOverride: Option[Symbol => Tree]): Tree = /*fixerUpper(owner, scrut.pos)*/ { - def matchFailGen = matchFailGenOverride orElse Some(arg: Tree => Throw(New(defn.MatchErrorClass, arg))) + def matchFailGen = matchFailGenOverride orElse Some((arg: Symbol) => Throw(New(defn.MatchErrorType, List(ref(arg))))) println("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) @@ -869,7 +868,7 @@ class PatternMatcher extends TreeTransform { // TODO: improve notion of trivial/irrefutable -- a trivial type test before the body still makes for a default case // ("trivial" depends on whether we're emitting a straight match or an exception, or more generally, any supertype of scrutSym.tpe is a no-op) // irrefutability checking should use the approximation framework also used for CSE, unreachability and exhaustivity checking - val synthCatchAll = + val synthCatchAll: Option[Symbol => Tree] = if (casesNoSubstOnly.nonEmpty && { val nonTrivLast = casesNoSubstOnly.last nonTrivLast.nonEmpty && nonTrivLast.head.isInstanceOf[BodyTreeMaker] @@ -937,4 +936,722 @@ class PatternMatcher extends TreeTransform { (optCases, toHoist) } } + + trait MatchTranslator extends TreeMakers { + + def isBackquoted(x: Ident) = false //x.hasAttachment[BackquotedIdentifierAttachment.type] + + def isVarPattern(pat: Tree): Boolean = pat match { + case x: Ident => !isBackquoted(x) && nme.isVariableName(x.name) + case _ => false + } + + /** A conservative approximation of which patterns do not discern anything. + * They are discarded during the translation. + */ + object WildcardPattern { + def unapply(pat: Tree): Boolean = pat match { + case Bind(nme.WILDCARD, WildcardPattern()) => true // don't skip when binding an interesting symbol! + //case Star(WildcardPattern()) => true // dd todo:? + case x: Ident => isVarPattern(x) + case Alternative(ps) => ps.forall(x => unapply(x)) + case EmptyTree => true + case _ => false + } + } + + object PatternBoundToUnderscore { + def unapply(pat: Tree): Boolean = pat match { + case Bind(nme.WILDCARD, _) => true // don't skip when binding an interesting symbol! + case Ident(nme.WILDCARD) => true + case Alternative(ps) => ps forall unapply + case Typed(PatternBoundToUnderscore(), _) => true + case _ => false + } + } + + object SymbolBound { + def unapply(tree: Tree): Option[(Symbol, Tree)] = tree match { + case Bind(_, expr) if tree.symbol.exists => Some(tree.symbol -> expr) + case _ => None + } + } + + // Always map repeated params to sequences + private def setVarInfo(sym: Symbol, info: Type) ={ + //setInfo debug.patmatResult(s"changing ${sym.defString} to")(repeatedToSeq(info)) + if(sym.info =:= info) assert(false, "should this happen?") + sym + } + + + def newBoundTree(tree: Tree, pt: Type): BoundTree = tree match { + case SymbolBound(sym, expr) => BoundTree(setVarInfo(sym, pt), expr) + case _ => BoundTree(setVarInfo(freshSym(tree.pos, prefix = "p"), pt), tree) + } + + final case class BoundTree(binder: Symbol, tree: Tree) { + private lazy val extractor = ExtractorCall(tree) + + def pos = tree.pos + def tpe = binder.info.dealias.widen // the type of the variable bound to the pattern + def pt = unbound match { + // case Star(tpt) => this glbWith seqType(tpt.tpe) dd todo: + case TypeBound(tpe) => tpe + case tree => tree.tpe + } + def glbWith(other: Type) = ctx.typeComparer.glb(tpe :: other :: Nil)// .normalize + + object SymbolAndTypeBound { + def unapply(tree: Tree): Option[(Symbol, Type)] = tree match { + case SymbolBound(sym, TypeBound(tpe)) => Some(sym -> tpe) + case TypeBound(tpe) => Some(binder -> tpe) + case _ => None + } + } + + object TypeBound { + def unapply(tree: Tree): Option[Type] = tree match { + case Typed(Ident(_), _) if tree.tpe != null => Some(tree.tpe) + case _ => None + } + } + + private def rebindTo(pattern: Tree) = BoundTree(binder, pattern) + private def step(treeMakers: TreeMaker*)(subpatterns: BoundTree*): TranslationStep = TranslationStep(treeMakers.toList, subpatterns.toList) + + private def bindingStep(sub: Symbol, subpattern: Tree) = step(SubstOnlyTreeMaker(sub, binder))(rebindTo(subpattern)) + private def equalityTestStep() = step(EqualityTestTreeMaker(binder, tree, pos))() + private def typeTestStep(sub: Symbol, subPt: Type) = step(TypeTestTreeMaker(sub, binder, subPt, glbWith(subPt))(pos))() + private def alternativesStep(alts: List[Tree]) = step(AlternativesTreeMaker(binder, translatedAlts(alts), alts.head.pos))() + private def translatedAlts(alts: List[Tree]) = alts map (alt => rebindTo(alt).translate()) + private def noStep() = step()() + + private def unsupportedPatternMsg = sm""" + |unsupported pattern: ${tree.show} / $this (this is a scalac bug.) + |""".trim + + // example check: List[Int] <:< ::[Int] + private def extractorStep(): TranslationStep = { + def paramType = extractor.aligner.wholeType + import extractor.treeMaker + // chain a type-testing extractor before the actual extractor call + // it tests the type, checks the outer pointer and casts to the expected type + // TODO: the outer check is mandated by the spec for case classes, but we do it for user-defined unapplies as well [SPEC] + // (the prefix of the argument passed to the unapply must equal the prefix of the type of the binder) + lazy val typeTest = TypeTestTreeMaker(binder, binder, paramType, paramType)(pos, extractorArgTypeTest = true) + // check whether typetest implies binder is not null, + // even though the eventual null check will be on typeTest.nextBinder + // it'll be equal to binder casted to paramType anyway (and the type test is on binder) + def extraction: TreeMaker = treeMaker(typeTest.nextBinder, typeTest impliesBinderNonNull binder, pos) + + // paramType = the type expected by the unapply + // TODO: paramType may contain unbound type params (run/t2800, run/t3530) + val makers = ( + // Statically conforms to paramType + if (this ensureConformsTo paramType) treeMaker(binder, false, pos) :: Nil + else typeTest :: extraction :: Nil + ) + step(makers: _*)(extractor.subBoundTrees: _*) + } + + // Summary of translation cases. I moved the excerpts from the specification further below so all + // the logic can be seen at once. + // + // [1] skip wildcard trees -- no point in checking them + // [2] extractor and constructor patterns + // [3] replace subpatBinder by patBinder, as if the Bind was not there. + // It must be patBinder, as subpatBinder has the wrong info: even if the bind assumes a better type, + // this is not guaranteed until we cast + // [4] typed patterns - a typed pattern never has any subtrees + // must treat Typed and Bind together -- we need to know the patBinder of the Bind pattern to get at the actual type + // [5] literal and stable id patterns + // [6] pattern alternatives + // [7] symbol-less bind patterns - this happens in certain ill-formed programs, there'll be an error later + // don't fail here though (or should we?) + def nextStep(): TranslationStep = tree match { + case WildcardPattern() => noStep() + case _: UnApply | _: Apply => extractorStep() + case SymbolAndTypeBound(sym, tpe) => typeTestStep(sym, tpe) + case TypeBound(tpe) => typeTestStep(binder, tpe) + case SymbolBound(sym, expr) => bindingStep(sym, expr) + case Literal(Constant(_)) | Ident(_) | Select(_, _) | This(_) => equalityTestStep() + case Alternative(alts) => alternativesStep(alts) + case _ => ctx.error(unsupportedPatternMsg, pos) ; noStep() + } + def translate(): List[TreeMaker] = nextStep() merge (_.translate()) + + private def setInfo(paramType: Type): Boolean = { + ctx.warning(s"resetting info of $this to $paramType") + setVarInfo(binder, paramType) + true + } + // If <:< but not =:=, no type test needed, but the tree maker relies on the binder having + // exactly paramType (and not just some type compatible with it.) SI-6624 shows this is necessary + // because apparently patBinder may have an unfortunate type (.decls don't have the case field + // accessors) TODO: get to the bottom of this -- I assume it happens when type checking + // infers a weird type for an unapply call. By going back to the parameterType for the + // extractor call we get a saner type, so let's just do that for now. + def ensureConformsTo(paramType: Type): Boolean = ( + (tpe =:= paramType) + || (tpe <:< paramType) && setInfo(paramType) + ) + + private def concreteType = tpe.bounds.hi + private def unbound = unbind(tree) + private def tpe_s = if (pt <:< concreteType) "" + pt else s"$pt (binder: $tpe)" + private def at_s = unbound match { + case WildcardPattern() => "" + case pat => s" @ $pat" + } + override def toString = s"${binder.name}: $tpe_s$at_s" + } + + // a list of TreeMakers that encode `patTree`, and a list of arguments for recursive invocations of `translatePattern` to encode its subpatterns + final case class TranslationStep(makers: List[TreeMaker], subpatterns: List[BoundTree]) { + def merge(f: BoundTree => List[TreeMaker]): List[TreeMaker] = makers ::: (subpatterns flatMap f) + override def toString = if (subpatterns.isEmpty) "" else subpatterns.mkString("(", ", ", ")") + } + + /** Implement a pattern match by turning its cases (including the implicit failure case) + * into the corresponding (monadic) extractors, and combining them with the `orElse` combinator. + * + * For `scrutinee match { case1 ... caseN }`, the resulting tree has the shape + * `runOrElse(scrutinee)(x => translateCase1(x).orElse(translateCase2(x)).....orElse(zero))` + * + * NOTE: the resulting tree is not type checked, nor are nested pattern matches transformed + * thus, you must typecheck the result (and that will in turn translate nested matches) + * this could probably optimized... (but note that the matchStrategy must be solved for each nested patternmatch) + */ + def translateMatch(match_ : Match): Tree = { + val Match(selector, cases) = match_ + + val (nonSyntheticCases, defaultOverride) = cases match { + case init :+ last if treeInfo isSyntheticDefaultCase last => (init, Some(((scrut: Tree) => last.body))) + case _ => (cases, None) + } + + checkMatchVariablePatterns(nonSyntheticCases) + + // we don't transform after uncurry + // (that would require more sophistication when generating trees, + // and the only place that emits Matches after typers is for exception handling anyway) + if (phase.id >= currentRun.uncurryPhase.id) + devWarning(s"running translateMatch past uncurry (at $phase) on $selector match $cases") + + debug.patmat("translating "+ cases.mkString("{", "\n", "}")) + + val start = if (Statistics.canEnable) Statistics.startTimer(patmatNanos) else null + + val selectorTp = repeatedToSeq(elimAnonymousClass(selector.tpe.widen.withoutAnnotations)) + + // when one of the internal cps-type-state annotations is present, strip all CPS annotations + val origPt = removeCPSFromPt(match_.tpe) + // relevant test cases: pos/existentials-harmful.scala, pos/gadt-gilles.scala, pos/t2683.scala, pos/virtpatmat_exist4.scala + // pt is the skolemized version + val pt = repeatedToSeq(origPt) + + // val packedPt = repeatedToSeq(typer.packedType(match_, context.owner)) + val selectorSym = freshSym(selector.pos, pureType(selectorTp)) setFlag treeInfo.SYNTH_CASE_FLAGS + + // pt = Any* occurs when compiling test/files/pos/annotDepMethType.scala with -Xexperimental + val combined = combineCases(selector, selectorSym, nonSyntheticCases map translateCase(selectorSym, pt), pt, matchOwner, defaultOverride) + + if (Statistics.canEnable) Statistics.stopTimer(patmatNanos, start) + combined + } + + // return list of typed CaseDefs that are supported by the backend (typed/bind/wildcard) + // we don't have a global scrutinee -- the caught exception must be bound in each of the casedefs + // there's no need to check the scrutinee for null -- "throw null" becomes "throw new NullPointerException" + // try to simplify to a type-based switch, or fall back to a catch-all case that runs a normal pattern match + // unlike translateMatch, we type our result before returning it + def translateTry(caseDefs: List[CaseDef], pt: Type, pos: Position): List[CaseDef] = + // if they're already simple enough to be handled by the back-end, we're done + if (caseDefs forall treeInfo.isCatchCase) caseDefs + else { + val swatches = { // switch-catches + val bindersAndCases = caseDefs map { caseDef => + // generate a fresh symbol for each case, hoping we'll end up emitting a type-switch (we don't have a global scrut there) + // if we fail to emit a fine-grained switch, have to do translateCase again with a single scrutSym (TODO: uniformize substitution on treemakers so we can avoid this) + val caseScrutSym = freshSym(pos, pureType(ThrowableTpe)) + (caseScrutSym, propagateSubstitution(translateCase(caseScrutSym, pt)(caseDef), EmptySubstitution)) + } + + for(cases <- emitTypeSwitch(bindersAndCases, pt).toList + if cases forall treeInfo.isCatchCase; // must check again, since it's not guaranteed -- TODO: can we eliminate this? e.g., a type test could test for a trait or a non-trivial prefix, which are not handled by the back-end + cse <- cases) yield fixerUpper(matchOwner, pos)(cse).asInstanceOf[CaseDef] + } + + val catches = if (swatches.nonEmpty) swatches else { + val scrutSym = freshSym(pos, pureType(ThrowableTpe)) + val casesNoSubstOnly = caseDefs map { caseDef => (propagateSubstitution(translateCase(scrutSym, pt)(caseDef), EmptySubstitution))} + + val exSym = freshSym(pos, pureType(ThrowableTpe), "ex") + + List( + atPos(pos) { + CaseDef( + Bind(exSym, Ident(nme.WILDCARD)), // TODO: does this need fixing upping? + EmptyTree, + combineCasesNoSubstOnly(REF(exSym), scrutSym, casesNoSubstOnly, pt, matchOwner, Some(scrut => Throw(REF(exSym)))) + ) + }) + } + + typer.typedCases(catches, ThrowableTpe, WildcardType) + } + + /** The translation of `pat if guard => body` has two aspects: + * 1) the substitution due to the variables bound by patterns + * 2) the combination of the extractor calls using `flatMap`. + * + * 2) is easy -- it looks like: `translatePattern_1.flatMap(translatePattern_2....flatMap(translatePattern_N.flatMap(translateGuard.flatMap((x_i) => success(Xbody(x_i)))))...)` + * this must be right-leaning tree, as can be seen intuitively by considering the scope of bound variables: + * variables bound by pat_1 must be visible from the function inside the left-most flatMap right up to Xbody all the way on the right + * 1) is tricky because translatePattern_i determines the shape of translatePattern_i+1: + * zoom in on `translatePattern_1.flatMap(translatePattern_2)` for example -- it actually looks more like: + * `translatePattern_1(x_scrut).flatMap((x_1) => {y_i -> x_1._i}translatePattern_2)` + * + * `x_1` references the result (inside the monad) of the extractor corresponding to `pat_1`, + * this result holds the values for the constructor arguments, which translatePattern_1 has extracted + * from the object pointed to by `x_scrut`. The `y_i` are the symbols bound by `pat_1` (in order) + * in the scope of the remainder of the pattern, and they must thus be replaced by: + * - (for 1-ary unapply) x_1 + * - (for n-ary unapply, n > 1) selection of the i'th tuple component of `x_1` + * - (for unapplySeq) x_1.apply(i) + * + * in the treemakers, + * + * Thus, the result type of `translatePattern_i`'s extractor must conform to `M[(T_1,..., T_n)]`. + * + * Operationally, phase 1) is a foldLeft, since we must consider the depth-first-flattening of + * the transformed patterns from left to right. For every pattern ast node, it produces a transformed ast and + * a function that will take care of binding and substitution of the next ast (to the right). + * + */ + def translateCase(scrutSym: Symbol, pt: Type)(caseDef: CaseDef) = { + val CaseDef(pattern, guard, body) = caseDef + translatePattern(BoundTree(scrutSym, pattern)) ++ translateGuard(guard) :+ translateBody(body, pt) + } + + def translatePattern(bound: BoundTree): List[TreeMaker] = bound.translate() + + def translateGuard(guard: Tree): List[TreeMaker] = + if (guard == EmptyTree) Nil + else List(GuardTreeMaker(guard)) + + // TODO: 1) if we want to support a generalisation of Kotlin's patmat continue, must not hard-wire lifting into the monad (which is now done by codegen.one), + // so that user can generate failure when needed -- use implicit conversion to lift into monad on-demand? + // to enable this, probably need to move away from Option to a monad specific to pattern-match, + // so that we can return Option's from a match without ambiguity whether this indicates failure in the monad, or just some result in the monad + // 2) body.tpe is the type of the body after applying the substitution that represents the solution of GADT type inference + // need the explicit cast in case our substitutions in the body change the type to something that doesn't take GADT typing into account + def translateBody(body: Tree, matchPt: Type): TreeMaker = + BodyTreeMaker(body, matchPt) + + // Some notes from the specification + + /*A constructor pattern is of the form c(p1, ..., pn) where n โ‰ฅ 0. + It consists of a stable identifier c, followed by element patterns p1, ..., pn. + The constructor c is a simple or qualified name which denotes a case class (ยง5.3.2). + + If the case class is monomorphic, then it must conform to the expected type of the pattern, + and the formal parameter types of xโ€™s primary constructor (ยง5.3) are taken as the expected + types of the element patterns p1, ..., pn. + + If the case class is polymorphic, then its type parameters are instantiated so that the + instantiation of c conforms to the expected type of the pattern. + The instantiated formal parameter types of cโ€™s primary constructor are then taken as the + expected types of the component patterns p1, ..., pn. + + The pattern matches all objects created from constructor invocations c(v1, ..., vn) + where each element pattern pi matches the corresponding value vi . + A special case arises when cโ€™s formal parameter types end in a repeated parameter. + This is further discussed in (ยง8.1.9). + **/ + + /* A typed pattern x : T consists of a pattern variable x and a type pattern T. + The type of x is the type pattern T, where each type variable and wildcard is replaced by a fresh, unknown type. + This pattern matches any value matched by the type pattern T (ยง8.2); it binds the variable name to that value. + */ + + /* A pattern binder x@p consists of a pattern variable x and a pattern p. + The type of the variable x is the static type T of the pattern p. + This pattern matches any value v matched by the pattern p, + provided the run-time type of v is also an instance of T, <-- TODO! https://issues.scala-lang.org/browse/SI-1503 + and it binds the variable name to that value. + */ + + /* 8.1.4 Literal Patterns + A literal pattern L matches any value that is equal (in terms of ==) to the literal L. + The type of L must conform to the expected type of the pattern. + + 8.1.5 Stable Identifier Patterns (a stable identifier r (see ยง3.1)) + The pattern matches any value v such that r == v (ยง12.1). + The type of r must conform to the expected type of the pattern. + */ + + + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + // helper methods: they analyze types and trees in isolation, but they are not (directly) concerned with the structure of the overall translation + /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + object ExtractorCall { + // TODO: check unargs == args + def apply(tree: Tree): ExtractorCall = tree match { + case UnApply(unfun, args) => new ExtractorCallRegular(alignPatterns(tree), unfun, args) // extractor + case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree), fun, args) // case class + } + } + + abstract class ExtractorCall(val aligner: PatternAligned) { + import aligner._ + def fun: Tree + def args: List[Tree] + + // don't go looking for selectors if we only expect one pattern + def rawSubPatTypes = aligner.extractedTypes + def resultInMonad = if (isBool) UnitTpe else typeOfMemberNamedGet(resultType) + def resultType = fun.tpe.finalResultType + + /** Create the TreeMaker that embodies this extractor call + * + * `binder` has been casted to `paramType` if necessary + * `binderKnownNonNull` indicates whether the cast implies `binder` cannot be null + * when `binderKnownNonNull` is `true`, `ProductExtractorTreeMaker` does not do a (redundant) null check on binder + */ + def treeMaker(binder: Symbol, binderKnownNonNull: Boolean, pos: Position): TreeMaker + + // `subPatBinders` are the variables bound by this pattern in the following patterns + // subPatBinders are replaced by references to the relevant part of the extractor's result (tuple component, seq element, the result as-is) + // must set infos to `subPatTypes`, which are provided by extractor's result, + // as b.info may be based on a Typed type ascription, which has not been taken into account yet by the translation + // (it will later result in a type test when `tp` is not a subtype of `b.info`) + // TODO: can we simplify this, together with the Bound case? + def subPatBinders = subBoundTrees map (_.binder) + lazy val subBoundTrees = (args, subPatTypes).zipped map newBoundTree + + // never store these in local variables (for PreserveSubPatBinders) + lazy val ignoredSubPatBinders: Set[Symbol] = subPatBinders zip args collect { case (b, PatternBoundToUnderscore()) => b } toSet + + // do repeated-parameter expansion to match up with the expected number of arguments (in casu, subpatterns) + private def nonStarSubPatTypes = aligner.typedNonStarPatterns map (_.tpe) + + def subPatTypes: List[Type] = typedPatterns map (_.tpe) + + // there are `productArity` non-seq elements in the tuple. + protected def firstIndexingBinder = productArity + protected def expectedLength = elementArity + protected def lastIndexingBinder = totalArity - starArity - 1 + + private def productElemsToN(binder: Symbol, n: Int): List[Tree] = 1 to n map tupleSel(binder) toList + private def genTake(binder: Symbol, n: Int): List[Tree] = (0 until n).toList map (codegen index seqTree(binder)) + private def genDrop(binder: Symbol, n: Int): List[Tree] = codegen.drop(seqTree(binder))(expectedLength) :: Nil + + // codegen.drop(seqTree(binder))(nbIndexingIndices)))).toList + protected def seqTree(binder: Symbol) = tupleSel(binder)(firstIndexingBinder + 1) + protected def tupleSel(binder: Symbol)(i: Int): Tree = codegen.tupleSel(binder)(i) + + // the trees that select the subpatterns on the extractor's result, + // referenced by `binder` + protected def subPatRefsSeq(binder: Symbol): List[Tree] = { + def lastTrees: List[Tree] = ( + if (!aligner.isStar) Nil + else if (expectedLength == 0) seqTree(binder) :: Nil + else genDrop(binder, expectedLength) + ) + // this error-condition has already been checked by checkStarPatOK: + // if(isSeq) assert(firstIndexingBinder + nbIndexingIndices + (if(lastIsStar) 1 else 0) == totalArity, "(resultInMonad, ts, subPatTypes, subPats)= "+(resultInMonad, ts, subPatTypes, subPats)) + + // [1] there are `firstIndexingBinder` non-seq tuple elements preceding the Seq + // [2] then we have to index the binder that represents the sequence for the remaining subpatterns, except for... + // [3] the last one -- if the last subpattern is a sequence wildcard: + // drop the prefix (indexed by the refs on the preceding line), return the remainder + ( productElemsToN(binder, firstIndexingBinder) + ++ genTake(binder, expectedLength) + ++ lastTrees + ).toList + } + + // the trees that select the subpatterns on the extractor's result, referenced by `binder` + // require (nbSubPats > 0 && (!lastIsStar || isSeq)) + protected def subPatRefs(binder: Symbol): List[Tree] = ( + if (totalArity > 0 && isSeq) subPatRefsSeq(binder) + else productElemsToN(binder, totalArity) + ) + + private def compareInts(t1: Tree, t2: Tree) = + gen.mkMethodCall(termMember(ScalaPackage, "math"), TermName("signum"), Nil, (t1 INT_- t2) :: Nil) + + protected def lengthGuard(binder: Symbol): Option[Tree] = + // no need to check unless it's an unapplySeq and the minimal length is non-trivially satisfied + checkedLength map { expectedLength => + // `binder.lengthCompare(expectedLength)` + // ...if binder has a lengthCompare method, otherwise + // `scala.math.signum(binder.length - expectedLength)` + def checkExpectedLength = sequenceType member nme.lengthCompare match { + case NoSymbol => compareInts(Select(seqTree(binder), nme.length), LIT(expectedLength)) + case lencmp => (seqTree(binder) DOT lencmp)(LIT(expectedLength)) + } + + // the comparison to perform + // when the last subpattern is a wildcard-star the expectedLength is but a lower bound + // (otherwise equality is required) + def compareOp: (Tree, Tree) => Tree = + if (aligner.isStar) _ INT_>= _ + else _ INT_== _ + + // `if (binder != null && $checkExpectedLength [== | >=] 0) then else zero` + (seqTree(binder) ANY_!= NULL) AND compareOp(checkExpectedLength, ZERO) + } + + def checkedLength: Option[Int] = + // no need to check unless it's an unapplySeq and the minimal length is non-trivially satisfied + if (!isSeq || expectedLength < starArity) None + else Some(expectedLength) + } + + // TODO: to be called when there's a def unapplyProd(x: T): U + // U must have N members _1,..., _N -- the _i are type checked, call their type Ti, + // for now only used for case classes -- pretending there's an unapplyProd that's the identity (and don't call it) + class ExtractorCallProd(aligner: PatternAligned, val fun: Tree, val args: List[Tree]) extends ExtractorCall(aligner) { + /** Create the TreeMaker that embodies this extractor call + * + * `binder` has been casted to `paramType` if necessary + * `binderKnownNonNull` indicates whether the cast implies `binder` cannot be null + * when `binderKnownNonNull` is `true`, `ProductExtractorTreeMaker` does not do a (redundant) null check on binder + */ + def treeMaker(binder: Symbol, binderKnownNonNull: Boolean, pos: Position): TreeMaker = { + val paramAccessors = binder.constrParamAccessors + // binders corresponding to mutable fields should be stored (SI-5158, SI-6070) + // make an exception for classes under the scala package as they should be well-behaved, + // to optimize matching on List + val mutableBinders = ( + if (!binder.info.typeSymbol.hasTransOwner(ScalaPackageClass) && + (paramAccessors exists (_.isMutable))) + subPatBinders.zipWithIndex.collect{ case (binder, idx) if paramAccessors(idx).isMutable => binder } + else Nil + ) + + // checks binder ne null before chaining to the next extractor + ProductExtractorTreeMaker(binder, lengthGuard(binder))(subPatBinders, subPatRefs(binder), mutableBinders, binderKnownNonNull, ignoredSubPatBinders) + } + + // reference the (i-1)th case accessor if it exists, otherwise the (i-1)th tuple component + override protected def tupleSel(binder: Symbol)(i: Int): Tree = { + val accessors = binder.caseFieldAccessors + if (accessors isDefinedAt (i-1)) REF(binder) DOT accessors(i-1) + else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN + } + } + + class ExtractorCallRegular(aligner: PatternAligned, extractorCallIncludingDummy: Tree, val args: List[Tree]) extends ExtractorCall(aligner) { + val Unapplied(fun) = extractorCallIncludingDummy + + /** Create the TreeMaker that embodies this extractor call + * + * `binder` has been casted to `paramType` if necessary + * `binderKnownNonNull` is not used in this subclass + * + * TODO: implement review feedback by @retronym: + * Passing the pair of values around suggests: + * case class Binder(sym: Symbol, knownNotNull: Boolean). + * Perhaps it hasn't reached critical mass, but it would already clean things up a touch. + */ + def treeMaker(patBinderOrCasted: Symbol, binderKnownNonNull: Boolean, pos: Position): TreeMaker = { + // the extractor call (applied to the binder bound by the flatMap corresponding + // to the previous (i.e., enclosing/outer) pattern) + val extractorApply = atPos(pos)(spliceApply(patBinderOrCasted)) + // can't simplify this when subPatBinders.isEmpty, since UnitTpe is definitely + // wrong when isSeq, and resultInMonad should always be correct since it comes + // directly from the extractor's result type + val binder = freshSym(pos, pureType(resultInMonad)) + + ExtractorTreeMaker(extractorApply, lengthGuard(binder), binder)( + subPatBinders, + subPatRefs(binder), + aligner.isBool, + checkedLength, + patBinderOrCasted, + ignoredSubPatBinders + ) + } + + override protected def seqTree(binder: Symbol): Tree = + if (firstIndexingBinder == 0) REF(binder) + else super.seqTree(binder) + + // the trees that select the subpatterns on the extractor's result, referenced by `binder` + // require (totalArity > 0 && (!lastIsStar || isSeq)) + override protected def subPatRefs(binder: Symbol): List[Tree] = + if (aligner.isSingle) ref(binder) :: Nil // special case for extractors + else super.subPatRefs(binder) + + protected def spliceApply(binder: Symbol): Tree = { + object splice extends Transformer { + def binderRef(pos: Position): Tree = + ref(binder) //setPos pos + override def transform(t: Tree) = t match { + // duplicated with the extractor Unapplied + case Apply(x, List(i @ Ident(nme.SELECTOR_DUMMY))) => + cpy.Apply(t, x, binderRef(i.pos) :: Nil) + // SI-7868 Account for numeric widening, e.g. .toInt + case Apply(x, List(i @ (sel @ Select(Ident(nme.SELECTOR_DUMMY), name)))) => + cpy.Apply(t, x, cpy.Select(sel, binderRef(i.pos), name) :: Nil) + case _ => + super.transform(t) + } + } + splice transform extractorCallIncludingDummy + } + + override def rawSubPatTypes = aligner.extractor.varargsTypes + } + } + + /** An extractor returns: F1, F2, ..., Fi, opt[Seq[E] or E*] + * A case matches: P1, P2, ..., Pj, opt[Seq[E]] + * Put together: P1/F1, P2/F2, ... Pi/Fi, Pi+1/E, Pi+2/E, ... Pj/E, opt[Seq[E]] + * + * Here Pm/Fi is the last pattern to match the fixed arity section. + * + * productArity: the value of i, i.e. the number of non-sequence types in the extractor + * nonStarArity: the value of j, i.e. the number of non-star patterns in the case definition + * elementArity: j - i, i.e. the number of non-star patterns which must match sequence elements + * starArity: 1 or 0 based on whether there is a star (sequence-absorbing) pattern + * totalArity: nonStarArity + starArity, i.e. the number of patterns in the case definition + * + * Note that productArity is a function only of the extractor, and + * nonStar/star/totalArity are all functions of the patterns. The key + * value for aligning and typing the patterns is elementArity, as it + * is derived from both sets of information. + */ + trait PatternExpander[Pattern, Type] { + /** You'll note we're not inside the cake. "Pattern" and "Type" are + * arbitrary types here, and NoPattern and NoType arbitrary values. + */ + def NoPattern: Pattern + def NoType: Type + + /** It's not optimal that we're carrying both sequence and repeated + * type here, but the implementation requires more unraveling before + * it can be avoided. + * + * sequenceType is Seq[T], elementType is T, repeatedType is T*. + */ + sealed case class Repeated(sequenceType: Type, elementType: Type, repeatedType: Type) { + def exists = elementType != NoType + + def elementList = if (exists) elementType :: Nil else Nil + def sequenceList = if (exists) sequenceType :: Nil else Nil + def repeatedList = if (exists) repeatedType :: Nil else Nil + + override def toString = s"${elementType}*" + } + object NoRepeated extends Repeated(NoType, NoType, NoType) { + override def toString = "" + } + + final case class Patterns(fixed: List[Pattern], star: Pattern) { + def hasStar = star != NoPattern + def starArity = if (hasStar) 1 else 0 + def nonStarArity = fixed.length + def totalArity = nonStarArity + starArity + def starPatterns = if (hasStar) star :: Nil else Nil + def all = fixed ::: starPatterns + + override def toString = all mkString ", " + } + + /** An 'extractor' can be a case class or an unapply or unapplySeq method. + * Decoding what it is that they extract takes place before we arrive here, + * so that this class can concentrate only on the relationship between + * patterns and types. + * + * In a case class, the class is the unextracted type and the fixed and + * repeated types are derived from its constructor parameters. + * + * In an unapply, this is reversed: the parameter to the unapply is the + * unextracted type, and the other types are derived based on the return + * type of the unapply method. + * + * In other words, this case class and unapply are encoded the same: + * + * case class Foo(x: Int, y: Int, zs: Char*) + * def unapplySeq(x: Foo): Option[(Int, Int, Seq[Char])] + * + * Both are Extractor(Foo, Int :: Int :: Nil, Repeated(Seq[Char], Char, Char*)) + * + * @param whole The type in its unextracted form + * @param fixed The non-sequence types which are extracted + * @param repeated The sequence type which is extracted + */ + final case class Extractor(whole: Type, fixed: List[Type], repeated: Repeated) { + require(whole != NoType, s"expandTypes($whole, $fixed, $repeated)") + + def productArity = fixed.length + def hasSeq = repeated.exists + def elementType = repeated.elementType + def sequenceType = repeated.sequenceType + def allTypes = fixed ::: repeated.sequenceList + def varargsTypes = fixed ::: repeated.repeatedList + def isErroneous = allTypes contains NoType + + private def typeStrings = fixed.map("" + _) ::: ( if (hasSeq) List("" + repeated) else Nil ) + + def offeringString = if (isErroneous) "" else typeStrings match { + case Nil => "Boolean" + case tp :: Nil => tp + case tps => tps.mkString("(", ", ", ")") + } + override def toString = "%s => %s".format(whole, offeringString) + } + + final case class TypedPat(pat: Pattern, tpe: Type) { + override def toString = s"$pat: $tpe" + } + + /** If elementArity is... + * 0: A perfect match between extractor and the fixed patterns. + * If there is a star pattern it will match any sequence. + * > 0: There are more patterns than products. There will have to be a + * sequence which can populate at least patterns. + * < 0: There are more products than patterns: compile time error. + */ + final case class Aligned(patterns: Patterns, extractor: Extractor) { + def elementArity = patterns.nonStarArity - productArity + def productArity = extractor.productArity + def starArity = patterns.starArity + def totalArity = patterns.totalArity + + def wholeType = extractor.whole + def sequenceType = extractor.sequenceType + def productTypes = extractor.fixed + def extractedTypes = extractor.allTypes + def typedNonStarPatterns = products ::: elements + def typedPatterns = typedNonStarPatterns ::: stars + + def isBool = !isSeq && productArity == 0 + def isSingle = !isSeq && totalArity == 1 + def isStar = patterns.hasStar + def isSeq = extractor.hasSeq + + private def typedAsElement(pat: Pattern) = TypedPat(pat, extractor.elementType) + private def typedAsSequence(pat: Pattern) = TypedPat(pat, extractor.sequenceType) + private def productPats = patterns.fixed take productArity + private def elementPats = patterns.fixed drop productArity + private def products = (productPats, productTypes).zipped map TypedPat + private def elements = elementPats map typedAsElement + private def stars = patterns.starPatterns map typedAsSequence + + override def toString = s""" + |Aligned { + | patterns $patterns + | extractor $extractor + | arities $productArity/$elementArity/$starArity // product/element/star + | typed ${typedPatterns mkString ", "} + |}""".stripMargin.trim + } + } } \ No newline at end of file From 073d3e629d897b3f7500caae29506177f893b55a Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:44:56 +0200 Subject: [PATCH 14/23] Pattern expanders for patmat --- .../tools/dotc/transform/PatternMatcher.scala | 143 +++++++++++++++++- 1 file changed, 141 insertions(+), 2 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 5561fd0d0762..7e38475a5f2e 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -939,7 +939,7 @@ class PatternMatcher extends TreeTransform { trait MatchTranslator extends TreeMakers { - def isBackquoted(x: Ident) = false //x.hasAttachment[BackquotedIdentifierAttachment.type] + def isBackquoted(x: Ident) = x.isInstanceOf[BackquotedIdent] def isVarPattern(pat: Tree): Boolean = pat match { case x: Ident => !isBackquoted(x) && nme.isVariableName(x.name) @@ -954,7 +954,7 @@ class PatternMatcher extends TreeTransform { case Bind(nme.WILDCARD, WildcardPattern()) => true // don't skip when binding an interesting symbol! //case Star(WildcardPattern()) => true // dd todo:? case x: Ident => isVarPattern(x) - case Alternative(ps) => ps.forall(x => unapply(x)) + case Alternative(ps) => ps forall unapply case EmptyTree => true case _ => false } @@ -1654,4 +1654,143 @@ class PatternMatcher extends TreeTransform { |}""".stripMargin.trim } } + + /** This is scalac-specific logic layered on top of the scalac-agnostic + * "matching products to patterns" logic defined in PatternExpander. + */ + trait ScalacPatternExpanders { + + type PatternAligned = ScalacPatternExpander#Aligned + + implicit class AlignedOps(val aligned: PatternAligned) { + import aligned._ + def expectedTypes = typedPatterns map (_.tpe) + def unexpandedFormals = extractor.varargsTypes + } + trait ScalacPatternExpander extends PatternExpander[Tree, Type] { + def NoPattern = EmptyTree + def NoType = NoType + + def newPatterns(patterns: List[Tree]): Patterns = patterns match { + case init :+ last if isStar(last) => Patterns(init, last) + case _ => Patterns(patterns, NoPattern) + } + def elementTypeOf(tpe: Type) = { + val seq = repeatedToSeq(tpe) + + ( typeOfMemberNamedHead(seq) + orElse typeOfMemberNamedApply(seq) + orElse definitions.elementType(ArrayClass, seq) + ) + } + def newExtractor(whole: Type, fixed: List[Type], repeated: Repeated): Extractor = + logResult(s"newExtractor($whole, $fixed, $repeated")(Extractor(whole, fixed, repeated)) + + // Turn Seq[A] into Repeated(Seq[A], A, A*) + def repeatedFromSeq(seqType: Type): Repeated = { + val elem = elementTypeOf(seqType) + val repeated = scalaRepeatedType(elem) + + Repeated(seqType, elem, repeated) + } + // Turn A* into Repeated(Seq[A], A, A*) + def repeatedFromVarargs(repeated: Type): Repeated = + Repeated(repeatedToSeq(repeated), repeatedToSingle(repeated), repeated) + + /** In this case we are basing the pattern expansion on a case class constructor. + * The argument is the MethodType carried by the primary constructor. + */ + def applyMethodTypes(method: Type): Extractor = { + val whole = method.finalResultType + + method.paramTypes match { + case init :+ last if isScalaRepeatedParamType(last) => newExtractor(whole, init, repeatedFromVarargs(last)) + case tps => newExtractor(whole, tps, NoRepeated) + } + } + + /** In this case, expansion is based on an unapply or unapplySeq method. + * Unfortunately the MethodType does not carry the information of whether + * it was unapplySeq, so we have to funnel that information in separately. + */ + def unapplyMethodTypes(method: Type, isSeq: Boolean): Extractor = { + val whole = firstParamType(method) + val result = method.finalResultType + val expanded = ( + if (result =:= BooleanTpe) Nil + else typeOfMemberNamedGet(result) match { + case rawGet if !hasSelectors(rawGet) => rawGet :: Nil + case rawGet => typesOfSelectors(rawGet) + } + ) + expanded match { + case init :+ last if isSeq => newExtractor(whole, init, repeatedFromSeq(last)) + case tps => newExtractor(whole, tps, NoRepeated) + } + } + } + object alignPatterns extends ScalacPatternExpander { + /** Converts a T => (A, B, C) extractor to a T => ((A, B, CC)) extractor. + */ + def tupleExtractor(extractor: Extractor): Extractor = + extractor.copy(fixed = tupleType(extractor.fixed) :: Nil) + + private def validateAligned(tree: Tree, aligned: Aligned): Aligned = { + import aligned._ + + def owner = tree.symbol.owner + def offering = extractor.offeringString + def symString = tree.symbol.fullLocationString + def offerString = if (extractor.isErroneous) "" else s" offering $offering" + def arityExpected = ( if (extractor.hasSeq) "at least " else "" ) + productArity + + def err(msg: String) = currentUnit.error(tree.pos, msg) + def warn(msg: String) = currentUnit.warning(tree.pos, msg) + def arityError(what: String) = err(s"$what patterns for $owner$offerString: expected $arityExpected, found $totalArity") + + if (isStar && !isSeq) + err("Star pattern must correspond with varargs or unapplySeq") + else if (elementArity < 0) + arityError("not enough") + else if (elementArity > 0 && !extractor.hasSeq) + arityError("too many") + + aligned + } + + def apply(sel: Tree, args: List[Tree]): Aligned = { + val fn = sel match { + case Unapplied(fn) => fn + case _ => sel + } + val patterns = newPatterns(args) + val isSeq = sel.symbol.name == nme.unapplySeq + val isUnapply = sel.symbol.name == nme.unapply + val extractor = sel.symbol.name match { + case nme.unapply => unapplyMethodTypes(fn.tpe, isSeq = false) + case nme.unapplySeq => unapplyMethodTypes(fn.tpe, isSeq = true) + case _ => applyMethodTypes(fn.tpe) + } + + /** Rather than let the error that is SI-6675 pollute the entire matching + * process, we will tuple the extractor before creation Aligned so that + * it contains known good values. + */ + def productArity = extractor.productArity + def acceptMessage = if (extractor.isErroneous) "" else s" to hold ${extractor.offeringString}" + val requiresTupling = isUnapply && patterns.totalArity == 1 && productArity > 1 + + if (requiresTupling && effectivePatternArity(args) == 1) + currentUnit.deprecationWarning(sel.pos, s"${sel.symbol.owner} expects $productArity patterns$acceptMessage but crushing into $productArity-tuple to fit single pattern (SI-6675)") + + val normalizedExtractor = if (requiresTupling) tupleExtractor(extractor) else extractor + validateAligned(fn, Aligned(patterns, normalizedExtractor)) + } + + def apply(tree: Tree): Aligned = tree match { + case Apply(fn, args) => apply(fn, args) + case UnApply(fn, args) => apply(fn, args) + } + } + } } \ No newline at end of file From 4b2626e4775c89657d9970bcc8b08ca6b5fd4bd0 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:46:42 +0200 Subject: [PATCH 15/23] Helper methods for patmat --- .../tools/dotc/transform/PatternMatcher.scala | 66 +++++++++++++++---- 1 file changed, 53 insertions(+), 13 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 7e38475a5f2e..f528aa9589bf 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -23,6 +23,7 @@ import scala.reflect.internal.util.Collections /** This transform eliminates patterns. Right now it's a dummy. * Awaiting the real pattern matcher. + * elimRepeated is required */ class PatternMatcher extends TreeTransform { import dotty.tools.dotc.ast.tpd._ @@ -98,7 +99,7 @@ class PatternMatcher extends TreeTransform { // assert(owner ne null); assert(owner ne NoSymbol) def freshSym(pos: Position, tp: Type = NoType, prefix: String = "x") = - ctx.newSymbol(ctx.owner, freshName(prefix) ,Flags.Synthetic, tp, coord = pos) + ctx.newSymbol(ctx.owner, freshName(prefix), Flags.Synthetic, tp, coord = pos) def newSynthCaseLabel(name: String, tpe:Type) = ctx.newSymbol(ctx.owner, ctx.freshName(name).toTermName, Flags.Label, tpe) //NoSymbol.newLabel(freshName(name), NoPosition) setFlag treeInfo.SYNTH_CASE_FLAGS @@ -937,7 +938,7 @@ class PatternMatcher extends TreeTransform { } } - trait MatchTranslator extends TreeMakers { + trait MatchTranslator extends TreeMakers with ScalacPatternExpanders { def isBackquoted(x: Ident) = x.isInstanceOf[BackquotedIdent] @@ -1113,6 +1114,44 @@ class PatternMatcher extends TreeTransform { override def toString = if (subpatterns.isEmpty) "" else subpatterns.mkString("(", ", ", ")") } + def isSyntheticDefaultCase(cdef: CaseDef) = cdef match { + case CaseDef(Bind(nme.DEFAULT_CASE, _), EmptyTree, _) => true + case _ => false + } + + def elimAnonymousClass(t: Type) = t match { + case t:TypeRef if t.symbol.isAnonymousClass => + t.symbol.asClass.typeRef.asSeenFrom(t.prefix, t.symbol.owner) + case _ => + t + } + + /** Is this pattern node a catch-all or type-test pattern? */ + def isCatchCase(cdef: CaseDef) = cdef match { + case CaseDef(Typed(Ident(nme.WILDCARD), tpt), EmptyTree, _) => + isSimpleThrowable(tpt.tpe) + case CaseDef(Bind(_, Typed(Ident(nme.WILDCARD), tpt)), EmptyTree, _) => + isSimpleThrowable(tpt.tpe) + case _ => + isDefaultCase(cdef) + } + + def isNonBottomSubClass(thiz: Symbol, that: Symbol)(implicit ctx: Context): Boolean = ( + (thiz eq that) || thiz.isError || that.isError || + thiz.info.baseClasses.contains(that) + ) + + private def isSimpleThrowable(tp: Type)(implicit ctx: Context): Boolean = tp match { + case tp @ TypeRef(pre, _) => + val sym = tp.symbol + (pre == NoPrefix || pre.widen.typeSymbol.isStatic) && + (isNonBottomSubClass(sym, ctx.definitions.ThrowableClass)) && /* bq */ !(sym is Flags.Trait) + case _ => + false + } + + + /** Implement a pattern match by turning its cases (including the implicit failure case) * into the corresponding (monadic) extractors, and combining them with the `orElse` combinator. * @@ -1127,37 +1166,38 @@ class PatternMatcher extends TreeTransform { val Match(selector, cases) = match_ val (nonSyntheticCases, defaultOverride) = cases match { - case init :+ last if treeInfo isSyntheticDefaultCase last => (init, Some(((scrut: Tree) => last.body))) + case init :+ last if isSyntheticDefaultCase(last) => (init, Some(((scrut: Symbol) => last.body))) case _ => (cases, None) } - checkMatchVariablePatterns(nonSyntheticCases) + // checkMatchVariablePatterns(nonSyntheticCases) // only used for warnings // we don't transform after uncurry // (that would require more sophistication when generating trees, // and the only place that emits Matches after typers is for exception handling anyway) - if (phase.id >= currentRun.uncurryPhase.id) - devWarning(s"running translateMatch past uncurry (at $phase) on $selector match $cases") + /*if (phase.id >= currentRun.uncurryPhase.id) + devWarning(s"running translateMatch past uncurry (at $phase) on $selector match $cases")*/ - debug.patmat("translating "+ cases.mkString("{", "\n", "}")) + println("translating "+ cases.mkString("{", "\n", "}")) - val start = if (Statistics.canEnable) Statistics.startTimer(patmatNanos) else null + //val start = if (Statistics.canEnable) Statistics.startTimer(patmatNanos) else null - val selectorTp = repeatedToSeq(elimAnonymousClass(selector.tpe.widen.withoutAnnotations)) + val selectorTp = elimAnonymousClass(selector.tpe.widen/*withoutAnnotations*/) // when one of the internal cps-type-state annotations is present, strip all CPS annotations - val origPt = removeCPSFromPt(match_.tpe) + ///val origPt = removeCPSFromPt(match_.tpe) // relevant test cases: pos/existentials-harmful.scala, pos/gadt-gilles.scala, pos/t2683.scala, pos/virtpatmat_exist4.scala // pt is the skolemized version - val pt = repeatedToSeq(origPt) + val pt = match_.tpe //repeatedToSeq(origPt) // val packedPt = repeatedToSeq(typer.packedType(match_, context.owner)) - val selectorSym = freshSym(selector.pos, pureType(selectorTp)) setFlag treeInfo.SYNTH_CASE_FLAGS + val selectorSym = freshSym(selector.pos, pureType(selectorTp)) + selectorSym.setFlag(Flags.SyntheticCase) // pt = Any* occurs when compiling test/files/pos/annotDepMethType.scala with -Xexperimental val combined = combineCases(selector, selectorSym, nonSyntheticCases map translateCase(selectorSym, pt), pt, matchOwner, defaultOverride) - if (Statistics.canEnable) Statistics.stopTimer(patmatNanos, start) + // if (Statistics.canEnable) Statistics.stopTimer(patmatNanos, start) combined } From 4f8ffe08e2377e7b00c14d21b89c3579bebc148a Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:48:50 +0200 Subject: [PATCH 16/23] Adapting patmat for dotty --- .../tools/dotc/transform/PatternMatcher.scala | 71 +++++++++++-------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index f528aa9589bf..b94e4e178a13 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -953,7 +953,7 @@ class PatternMatcher extends TreeTransform { object WildcardPattern { def unapply(pat: Tree): Boolean = pat match { case Bind(nme.WILDCARD, WildcardPattern()) => true // don't skip when binding an interesting symbol! - //case Star(WildcardPattern()) => true // dd todo:? + case t if(tpd.isWildcardArg(t)) => true case x: Ident => isVarPattern(x) case Alternative(ps) => ps forall unapply case EmptyTree => true @@ -1208,38 +1208,37 @@ class PatternMatcher extends TreeTransform { // unlike translateMatch, we type our result before returning it def translateTry(caseDefs: List[CaseDef], pt: Type, pos: Position): List[CaseDef] = // if they're already simple enough to be handled by the back-end, we're done - if (caseDefs forall treeInfo.isCatchCase) caseDefs + if (caseDefs forall isCatchCase) caseDefs else { val swatches = { // switch-catches val bindersAndCases = caseDefs map { caseDef => // generate a fresh symbol for each case, hoping we'll end up emitting a type-switch (we don't have a global scrut there) // if we fail to emit a fine-grained switch, have to do translateCase again with a single scrutSym (TODO: uniformize substitution on treemakers so we can avoid this) - val caseScrutSym = freshSym(pos, pureType(ThrowableTpe)) + val caseScrutSym = freshSym(pos, pureType(ctx.definitions.ThrowableType)) (caseScrutSym, propagateSubstitution(translateCase(caseScrutSym, pt)(caseDef), EmptySubstitution)) } for(cases <- emitTypeSwitch(bindersAndCases, pt).toList - if cases forall treeInfo.isCatchCase; // must check again, since it's not guaranteed -- TODO: can we eliminate this? e.g., a type test could test for a trait or a non-trivial prefix, which are not handled by the back-end - cse <- cases) yield fixerUpper(matchOwner, pos)(cse).asInstanceOf[CaseDef] + if cases forall isCatchCase; // must check again, since it's not guaranteed -- TODO: can we eliminate this? e.g., a type test could test for a trait or a non-trivial prefix, which are not handled by the back-end + cse <- cases) yield /*fixerUpper(matchOwner, pos)*/(cse).asInstanceOf[CaseDef] } val catches = if (swatches.nonEmpty) swatches else { - val scrutSym = freshSym(pos, pureType(ThrowableTpe)) + val scrutSym = freshSym(pos, pureType(ctx.definitions.ThrowableType)) val casesNoSubstOnly = caseDefs map { caseDef => (propagateSubstitution(translateCase(scrutSym, pt)(caseDef), EmptySubstitution))} - val exSym = freshSym(pos, pureType(ThrowableTpe), "ex") + val exSym = freshSym(pos, pureType(ctx.definitions.ThrowableType), "ex") List( - atPos(pos) { CaseDef( - Bind(exSym, Ident(nme.WILDCARD)), // TODO: does this need fixing upping? + Bind(exSym, Ident(??? /*nme.WILDCARD*/)), // TODO: does this need fixing upping? EmptyTree, - combineCasesNoSubstOnly(REF(exSym), scrutSym, casesNoSubstOnly, pt, matchOwner, Some(scrut => Throw(REF(exSym)))) + combineCasesNoSubstOnly(ref(exSym), scrutSym, casesNoSubstOnly, pt, matchOwner, Some(scrut => Throw(ref(exSym)))) ) - }) + ) } - typer.typedCases(catches, ThrowableTpe, WildcardType) + /*typer.typedCases(*/catches/*, ctx.definitions.ThrowableType, WildcardType)*/ } /** The translation of `pat if guard => body` has two aspects: @@ -1340,7 +1339,7 @@ class PatternMatcher extends TreeTransform { object ExtractorCall { // TODO: check unargs == args def apply(tree: Tree): ExtractorCall = tree match { - case UnApply(unfun, args) => new ExtractorCallRegular(alignPatterns(tree), unfun, args) // extractor + case UnApply(unfun, implicits, args) => new ExtractorCallRegular(alignPatterns(tree), unfun, args) // extractor case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree), fun, args) // case class } } @@ -1352,7 +1351,15 @@ class PatternMatcher extends TreeTransform { // don't go looking for selectors if we only expect one pattern def rawSubPatTypes = aligner.extractedTypes - def resultInMonad = if (isBool) UnitTpe else typeOfMemberNamedGet(resultType) + + def typeArgOfBaseTypeOr(tp: Type, baseClass: Symbol)(or: => Type): Type = (tp.baseTypeWithArgs(baseClass)).argInfos match { + case x :: Nil => x + case _ => or + } + + def typeOfMemberNamedGet(tp: Type) = typeArgOfBaseTypeOr(tp, defn.OptionClass)(tp.member(nme.get).info) + + def resultInMonad = if (isBool) defn.UnitType else typeOfMemberNamedGet(resultType) def resultType = fun.tpe.finalResultType /** Create the TreeMaker that embodies this extractor call @@ -1421,8 +1428,12 @@ class PatternMatcher extends TreeTransform { else productElemsToN(binder, totalArity) ) + val mathSignum = ref(defn.ScalaMathPackageVal).select("signum".toTermName) + + private def compareInts(t1: Tree, t2: Tree) = - gen.mkMethodCall(termMember(ScalaPackage, "math"), TermName("signum"), Nil, (t1 INT_- t2) :: Nil) + mathSignum.appliedTo(t1.select(defn.Int_-).appliedTo(t2)) + //gen.mkMethodCall(termMember(ScalaPackage, "math"), TermName("signum"), Nil, (t1 INT_- t2) :: Nil) protected def lengthGuard(binder: Symbol): Option[Tree] = // no need to check unless it's an unapplySeq and the minimal length is non-trivially satisfied @@ -1430,20 +1441,23 @@ class PatternMatcher extends TreeTransform { // `binder.lengthCompare(expectedLength)` // ...if binder has a lengthCompare method, otherwise // `scala.math.signum(binder.length - expectedLength)` - def checkExpectedLength = sequenceType member nme.lengthCompare match { - case NoSymbol => compareInts(Select(seqTree(binder), nme.length), LIT(expectedLength)) - case lencmp => (seqTree(binder) DOT lencmp)(LIT(expectedLength)) + def checkExpectedLength: Tree = sequenceType.member(nme.lengthCompare) match { + case NoDenotation => compareInts(Select(seqTree(binder), nme.length), Literal(Constant(expectedLength))) + case x:SingleDenotation => (seqTree(binder).select(x.symbol)).appliedTo(Literal(Constant(expectedLength))) + case _ => + ctx.error("TODO: multiple lengthCompare") + EmptyTree } // the comparison to perform // when the last subpattern is a wildcard-star the expectedLength is but a lower bound // (otherwise equality is required) def compareOp: (Tree, Tree) => Tree = - if (aligner.isStar) _ INT_>= _ - else _ INT_== _ + if (aligner.isStar) _.select(defn.Int_>=).appliedTo(_) + else _.select(defn.Int_==).appliedTo(_) // `if (binder != null && $checkExpectedLength [== | >=] 0) then else zero` - (seqTree(binder) ANY_!= NULL) AND compareOp(checkExpectedLength, ZERO) + (seqTree(binder).select(defn.Any_!=).appliedTo(Literal(Constant(null)))).select(defn.Boolean_and).appliedTo(compareOp(checkExpectedLength, Literal(Constant(0)))) } def checkedLength: Option[Int] = @@ -1463,14 +1477,15 @@ class PatternMatcher extends TreeTransform { * when `binderKnownNonNull` is `true`, `ProductExtractorTreeMaker` does not do a (redundant) null check on binder */ def treeMaker(binder: Symbol, binderKnownNonNull: Boolean, pos: Position): TreeMaker = { - val paramAccessors = binder.constrParamAccessors + val paramAccessors = binder.info.costrParamAccessors // binders corresponding to mutable fields should be stored (SI-5158, SI-6070) // make an exception for classes under the scala package as they should be well-behaved, // to optimize matching on List val mutableBinders = ( - if (!binder.info.typeSymbol.hasTransOwner(ScalaPackageClass) && - (paramAccessors exists (_.isMutable))) - subPatBinders.zipWithIndex.collect{ case (binder, idx) if paramAccessors(idx).isMutable => binder } + if (//!binder.info.typeSymbol.hasTransOwner(ScalaPackageClass) // TODO: DDD ??? + // && + (paramAccessors exists (_.hasAltWith(x => x.symbol is Flags.Mutable)))) + subPatBinders.zipWithIndex.collect{ case (binder, idx) if paramAccessors(idx).hasAltWith(x => x.symbol is Flags.Mutable) => binder } else Nil ) @@ -1480,8 +1495,8 @@ class PatternMatcher extends TreeTransform { // reference the (i-1)th case accessor if it exists, otherwise the (i-1)th tuple component override protected def tupleSel(binder: Symbol)(i: Int): Tree = { - val accessors = binder.caseFieldAccessors - if (accessors isDefinedAt (i-1)) REF(binder) DOT accessors(i-1) + val accessors = product binder + if (accessors isDefinedAt (i-1)) ref(binder).select(accessors(i-1)) else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN } } @@ -1712,7 +1727,7 @@ class PatternMatcher extends TreeTransform { def NoType = NoType def newPatterns(patterns: List[Tree]): Patterns = patterns match { - case init :+ last if isStar(last) => Patterns(init, last) + case init :+ last if tpd.isWildcardStarArg(last) => Patterns(init, last) case _ => Patterns(patterns, NoPattern) } def elementTypeOf(tpe: Type) = { From 435a50ac315067a882b65efb3015f4bc98ec9f09 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:54:11 +0200 Subject: [PATCH 17/23] more of adapting patmat for dotty --- .../tools/dotc/transform/PatternMatcher.scala | 130 ++++++++++-------- 1 file changed, 71 insertions(+), 59 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index b94e4e178a13..94eca4683132 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -11,9 +11,14 @@ import core.Constants._ import core.StdNames._ import core.transform.Erasure.isUnboundedGeneric import dotty.tools.dotc.ast.tpd +import dotty.tools.dotc.core +import dotty.tools.dotc.core.{TypeApplications, Flags} +import dotty.tools.dotc.typer.Applications import dotty.tools.dotc.util.Positions import typer.ErrorReporting._ import ast.Trees._ +import Applications._ +import TypeApplications._ import dotty.tools.dotc.util.Positions.Position import dotty.tools.dotc.core.Decorators._ @@ -28,9 +33,7 @@ import scala.reflect.internal.util.Collections class PatternMatcher extends TreeTransform { import dotty.tools.dotc.ast.tpd._ - implicit val ctx: Context = ??? - - def name: String = "patternMatcher" + def name: String = "patternMatcher" /*override def transformCaseDef(tree: CaseDef)(implicit ctx: Context, info: TransformerInfo): Tree = cpy.CaseDef(tree, Literal(Constant("")), tree.guard, tree.body)*/ @@ -39,27 +42,29 @@ class PatternMatcher extends TreeTransform { /*case Try(block, catches, finalizer) => treeCopy.Try(tree, transform(block), translator.translateTry(transformTrees(catches).asInstanceOf[List[CaseDef]], tree.tpe, tree.pos), transform(finalizer)) case _ => super.transform(tree)*/ - /*override def transformMatch(tree: tpd.Match)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { - case Match(sel, cases) => + override def transformMatch(tree: tpd.Match)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { val origTp = tree.tpe // setType origTp intended for CPS -- TODO: is it necessary? - val translated = translator.translateMatch(treeCopy.Match(tree, transform(sel), transformTrees(cases).asInstanceOf[List[CaseDef]])) - try { + val translated = new Translator()(ctx).translator.translateMatch(tree) + /*try { localTyper.typed(translated) setType origTp } catch { case x: (Types#TypeError) => // TODO: this should never happen; error should've been reported during type checking unit.error(tree.pos, "error during expansion of this match (this is a scalac bug).\nThe underlying error was: "+ x.msg) translated - } - }*/ + }*/ + translated + } + + class Translator(implicit ctx: Context) { def translator = { new OptimizingMatchTranslator/*(localTyper)*/ } - class OptimizingMatchTranslator extends MatchOptimizer/*(val typer: analyzer.Typer)*/ /*extends MatchTranslator*/ + class OptimizingMatchTranslator extends MatchOptimizer/*(val typer: analyzer.Typer)*/ with MatchTranslator trait Debugging { @@ -252,8 +257,8 @@ class PatternMatcher extends TreeTransform { - val caseDefs = cases.foldLeft[Tree](catchAllDef.getOrElse(matchEndDef)){ (acc: Tree, mkCase: Casegen => Tree) => - val nextCase = lastSymbol.orElse(matchEnd) + val caseDefs = cases.foldRight[Tree](catchAllDef.getOrElse(matchEndDef)){ (mkCase: Casegen => Tree, acc: Tree) => + val nextCase = if(lastSymbol ne null) lastSymbol else matchEnd DefDef(newCaseSym, _ => Block(List(acc), mkCase(new OptimizedCasegen(matchEnd, nextCase)))) @@ -335,7 +340,7 @@ class PatternMatcher extends TreeTransform { /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// trait TreeMakers extends TypedSubstitution with CodegenCore { def optimizeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type): (List[List[TreeMaker]], List[Tree]) - def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit + def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit = {} def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Symbol => Tree], unchecked: Boolean): Option[Tree] = None @@ -694,10 +699,10 @@ class PatternMatcher extends TreeTransform { **/ case class TypeTestTreeMaker(prevBinder: Symbol, testedBinder: Symbol, expectedTp: Type, nextBinderTp: Type)(override val pos: Position, extractorArgTypeTest: Boolean = false) extends CondTreeMaker { import TypeTestTreeMaker._ - println("TTTM"+((prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp))) + ctx.debuglog("TTTM"+((prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp))) lazy val outerTestNeeded = ( - (expectedTp.normalizedPrefix ne NoPrefix) + (expectedTp.normalizedPrefix.typeSymbol ne NoSymbol) && !expectedTp.normalizedPrefix.typeSymbol.isPackageObject && true //needsOuterTest(expectedTp, testedBinder.info, matchOwner) // todo ) @@ -750,9 +755,9 @@ class PatternMatcher extends TreeTransform { if (extractorArgTypeTest) mkDefault else expectedTp match { case ThisType(sym) if sym.flags is Flags.Module => and(mkEqualsTest(ref(sym)), mkTypeTest) // must use == to support e.g. List() == Nil - case t:SingletonType => mkEqTest(singleton(expectedTp)) // SI-4577, SI-4897 case ConstantType(Constant(null)) if isAnyRef => mkEqTest(expTp(Literal(Constant(null)))) case ConstantType(const) => mkEqualsTest(expTp(Literal(const))) + case t:SingletonType => mkEqTest(singleton(expectedTp)) // SI-4577, SI-4897 case ThisType(sym) => mkEqTest(expTp(This(sym))) case _ => mkDefault } @@ -981,7 +986,7 @@ class PatternMatcher extends TreeTransform { // Always map repeated params to sequences private def setVarInfo(sym: Symbol, info: Type) ={ //setInfo debug.patmatResult(s"changing ${sym.defString} to")(repeatedToSeq(info)) - if(sym.info =:= info) assert(false, "should this happen?") + assert(sym.info =:= info, "bug in porting pattern matcher") sym } @@ -992,7 +997,7 @@ class PatternMatcher extends TreeTransform { } final case class BoundTree(binder: Symbol, tree: Tree) { - private lazy val extractor = ExtractorCall(tree) + private lazy val extractor = ExtractorCall(tree, binder) def pos = tree.pos def tpe = binder.info.dealias.widen // the type of the variable bound to the pattern @@ -1072,7 +1077,7 @@ class PatternMatcher extends TreeTransform { // don't fail here though (or should we?) def nextStep(): TranslationStep = tree match { case WildcardPattern() => noStep() - case _: UnApply | _: Apply => extractorStep() + case _: UnApply | _: Apply| Typed(_: UnApply | _: Apply, _) => extractorStep() case SymbolAndTypeBound(sym, tpe) => typeTestStep(sym, tpe) case TypeBound(tpe) => typeTestStep(binder, tpe) case SymbolBound(sym, expr) => bindingStep(sym, expr) @@ -1338,15 +1343,15 @@ class PatternMatcher extends TreeTransform { object ExtractorCall { // TODO: check unargs == args - def apply(tree: Tree): ExtractorCall = tree match { - case UnApply(unfun, implicits, args) => new ExtractorCallRegular(alignPatterns(tree), unfun, args) // extractor - case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree), fun, args) // case class + def apply(tree: Tree, binder: Symbol): ExtractorCall = tree match { + case UnApply(unfun, implicits, args) => new ExtractorCallRegular(alignPatterns(tree), unfun.appliedTo(ref(binder)), args, tree.tpe) // extractor + case Typed(UnApply(unfun, implicits, args), tpt) => new ExtractorCallRegular(alignPatterns(tree), unfun.appliedTo(ref(binder)), args, tree.tpe) // extractor + case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree), fun, args, fun.tpe) // case class } } abstract class ExtractorCall(val aligner: PatternAligned) { import aligner._ - def fun: Tree def args: List[Tree] // don't go looking for selectors if we only expect one pattern @@ -1359,8 +1364,8 @@ class PatternMatcher extends TreeTransform { def typeOfMemberNamedGet(tp: Type) = typeArgOfBaseTypeOr(tp, defn.OptionClass)(tp.member(nme.get).info) - def resultInMonad = if (isBool) defn.UnitType else typeOfMemberNamedGet(resultType) - def resultType = fun.tpe.finalResultType + def resultInMonad = resultType //if (isBool) defn.UnitType else typeOfMemberNamedGet(resultType) + def resultType: Type /** Create the TreeMaker that embodies this extractor call * @@ -1398,7 +1403,11 @@ class PatternMatcher extends TreeTransform { // codegen.drop(seqTree(binder))(nbIndexingIndices)))).toList protected def seqTree(binder: Symbol) = tupleSel(binder)(firstIndexingBinder + 1) - protected def tupleSel(binder: Symbol)(i: Int): Tree = codegen.tupleSel(binder)(i) + protected def tupleSel(binder: Symbol)(i: Int): Tree = { + val accessors = if(defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.decls.filter(_.is(Flags.CaseAccessor)).toList + if (accessors.isDefinedAt(i-1)) ref(binder).select(accessors(i-1)) + else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN + } // the trees that select the subpatterns on the extractor's result, // referenced by `binder` @@ -1469,7 +1478,7 @@ class PatternMatcher extends TreeTransform { // TODO: to be called when there's a def unapplyProd(x: T): U // U must have N members _1,..., _N -- the _i are type checked, call their type Ti, // for now only used for case classes -- pretending there's an unapplyProd that's the identity (and don't call it) - class ExtractorCallProd(aligner: PatternAligned, val fun: Tree, val args: List[Tree]) extends ExtractorCall(aligner) { + class ExtractorCallProd(aligner: PatternAligned, val fun: Tree, val args: List[Tree], val resultType: Type) extends ExtractorCall(aligner) { /** Create the TreeMaker that embodies this extractor call * * `binder` has been casted to `paramType` if necessary @@ -1492,17 +1501,9 @@ class PatternMatcher extends TreeTransform { // checks binder ne null before chaining to the next extractor ProductExtractorTreeMaker(binder, lengthGuard(binder))(subPatBinders, subPatRefs(binder), mutableBinders, binderKnownNonNull, ignoredSubPatBinders) } - - // reference the (i-1)th case accessor if it exists, otherwise the (i-1)th tuple component - override protected def tupleSel(binder: Symbol)(i: Int): Tree = { - val accessors = product binder - if (accessors isDefinedAt (i-1)) ref(binder).select(accessors(i-1)) - else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN - } } - class ExtractorCallRegular(aligner: PatternAligned, extractorCallIncludingDummy: Tree, val args: List[Tree]) extends ExtractorCall(aligner) { - val Unapplied(fun) = extractorCallIncludingDummy + class ExtractorCallRegular(aligner: PatternAligned, extractorCallIncludingDummy: Tree, val args: List[Tree], val resultType: Type) extends ExtractorCall(aligner) { /** Create the TreeMaker that embodies this extractor call * @@ -1517,7 +1518,7 @@ class PatternMatcher extends TreeTransform { def treeMaker(patBinderOrCasted: Symbol, binderKnownNonNull: Boolean, pos: Position): TreeMaker = { // the extractor call (applied to the binder bound by the flatMap corresponding // to the previous (i.e., enclosing/outer) pattern) - val extractorApply = atPos(pos)(spliceApply(patBinderOrCasted)) + val extractorApply = extractorCallIncludingDummy// spliceApply(patBinderOrCasted) // can't simplify this when subPatBinders.isEmpty, since UnitTpe is definitely // wrong when isSeq, and resultInMonad should always be correct since it comes // directly from the extractor's result type @@ -1534,7 +1535,7 @@ class PatternMatcher extends TreeTransform { } override protected def seqTree(binder: Symbol): Tree = - if (firstIndexingBinder == 0) REF(binder) + if (firstIndexingBinder == 0) ref(binder) else super.seqTree(binder) // the trees that select the subpatterns on the extractor's result, referenced by `binder` @@ -1544,10 +1545,11 @@ class PatternMatcher extends TreeTransform { else super.subPatRefs(binder) protected def spliceApply(binder: Symbol): Tree = { - object splice extends Transformer { + object splice extends TreeMap { def binderRef(pos: Position): Tree = ref(binder) //setPos pos - override def transform(t: Tree) = t match { + + override def transform(t: tpd.Tree)(implicit ctx: Context): tpd.Tree = t match { // duplicated with the extractor Unapplied case Apply(x, List(i @ Ident(nme.SELECTOR_DUMMY))) => cpy.Apply(t, x, binderRef(i.pos) :: Nil) @@ -1724,33 +1726,39 @@ class PatternMatcher extends TreeTransform { } trait ScalacPatternExpander extends PatternExpander[Tree, Type] { def NoPattern = EmptyTree - def NoType = NoType + def NoType = core.Types.NoType def newPatterns(patterns: List[Tree]): Patterns = patterns match { case init :+ last if tpd.isWildcardStarArg(last) => Patterns(init, last) case _ => Patterns(patterns, NoPattern) } + def typeOfMemberNamedHead(tpe: Type): Type = tpe.select(nme.head) + def typeOfMemberNamedApply(tpe: Type): Type = tpe.select(nme.apply) + def typeOfMemberNamedGet(tpe: Type): Type = tpe.select(nme.get) def elementTypeOf(tpe: Type) = { - val seq = repeatedToSeq(tpe) + val seq = tpe //repeatedToSeq(tpe) ( typeOfMemberNamedHead(seq) orElse typeOfMemberNamedApply(seq) - orElse definitions.elementType(ArrayClass, seq) + orElse seq.elemType ) } - def newExtractor(whole: Type, fixed: List[Type], repeated: Repeated): Extractor = - logResult(s"newExtractor($whole, $fixed, $repeated")(Extractor(whole, fixed, repeated)) + def newExtractor(whole: Type, fixed: List[Type], repeated: Repeated): Extractor = { + ctx.log(s"newExtractor($whole, $fixed, $repeated") + Extractor(whole, fixed, repeated) + } // Turn Seq[A] into Repeated(Seq[A], A, A*) def repeatedFromSeq(seqType: Type): Repeated = { val elem = elementTypeOf(seqType) - val repeated = scalaRepeatedType(elem) + val repeated = /*scalaRepeatedType(*/elem//) Repeated(seqType, elem, repeated) } // Turn A* into Repeated(Seq[A], A, A*) def repeatedFromVarargs(repeated: Type): Repeated = - Repeated(repeatedToSeq(repeated), repeatedToSingle(repeated), repeated) + //Repeated(repeatedToSeq(repeated), repeatedToSingle(repeated), repeated) + Repeated(repeated, repeated.elemType, repeated) /** In this case we are basing the pattern expansion on a case class constructor. * The argument is the MethodType carried by the primary constructor. @@ -1758,24 +1766,26 @@ class PatternMatcher extends TreeTransform { def applyMethodTypes(method: Type): Extractor = { val whole = method.finalResultType - method.paramTypes match { - case init :+ last if isScalaRepeatedParamType(last) => newExtractor(whole, init, repeatedFromVarargs(last)) + method.paramTypess.head match { + case init :+ last if last.isRepeatedParam => newExtractor(whole, init, repeatedFromVarargs(last)) case tps => newExtractor(whole, tps, NoRepeated) } } + def hasSelectors(tpe: Type) = tpe.member(nme._1).exists && tpe.member(nme._2).exists // dd todo: ??? + /** In this case, expansion is based on an unapply or unapplySeq method. * Unfortunately the MethodType does not carry the information of whether * it was unapplySeq, so we have to funnel that information in separately. */ def unapplyMethodTypes(method: Type, isSeq: Boolean): Extractor = { - val whole = firstParamType(method) + val whole = method.paramTypess.headOption.flatMap(_.headOption).getOrElse(NoType)//firstParamType(method) val result = method.finalResultType val expanded = ( - if (result =:= BooleanTpe) Nil + if (result =:= ctx.definitions.BooleanType) Nil else typeOfMemberNamedGet(result) match { case rawGet if !hasSelectors(rawGet) => rawGet :: Nil - case rawGet => typesOfSelectors(rawGet) + case rawGet => productSelectorTypes(rawGet) } ) expanded match { @@ -1788,19 +1798,19 @@ class PatternMatcher extends TreeTransform { /** Converts a T => (A, B, C) extractor to a T => ((A, B, CC)) extractor. */ def tupleExtractor(extractor: Extractor): Extractor = - extractor.copy(fixed = tupleType(extractor.fixed) :: Nil) + extractor.copy(fixed = ctx.definitions.tupleType(extractor.fixed) :: Nil) private def validateAligned(tree: Tree, aligned: Aligned): Aligned = { import aligned._ def owner = tree.symbol.owner def offering = extractor.offeringString - def symString = tree.symbol.fullLocationString + def symString = tree.symbol.showLocated def offerString = if (extractor.isErroneous) "" else s" offering $offering" def arityExpected = ( if (extractor.hasSeq) "at least " else "" ) + productArity - def err(msg: String) = currentUnit.error(tree.pos, msg) - def warn(msg: String) = currentUnit.warning(tree.pos, msg) + def err(msg: String) = ctx.error(msg, tree.pos) + def warn(msg: String) = ctx.warning(msg, tree.pos) def arityError(what: String) = err(s"$what patterns for $owner$offerString: expected $arityExpected, found $totalArity") if (isStar && !isSeq) @@ -1835,17 +1845,19 @@ class PatternMatcher extends TreeTransform { def acceptMessage = if (extractor.isErroneous) "" else s" to hold ${extractor.offeringString}" val requiresTupling = isUnapply && patterns.totalArity == 1 && productArity > 1 - if (requiresTupling && effectivePatternArity(args) == 1) - currentUnit.deprecationWarning(sel.pos, s"${sel.symbol.owner} expects $productArity patterns$acceptMessage but crushing into $productArity-tuple to fit single pattern (SI-6675)") + //if (requiresTupling && effectivePatternArity(args) == 1) + // currentUnit.deprecationWarning(sel.pos, s"${sel.symbol.owner} expects $productArity patterns$acceptMessage but crushing into $productArity-tuple to fit single pattern (SI-6675)") val normalizedExtractor = if (requiresTupling) tupleExtractor(extractor) else extractor validateAligned(fn, Aligned(patterns, normalizedExtractor)) } def apply(tree: Tree): Aligned = tree match { + case Typed(tree, _) => apply(tree) case Apply(fn, args) => apply(fn, args) - case UnApply(fn, args) => apply(fn, args) + case UnApply(fn, implicits, args) => apply(fn, args) } } } + } } \ No newline at end of file From f96c17599b256946203b2df28438a9f44ae46ee5 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:57:19 +0200 Subject: [PATCH 18/23] More fixes, added _id for internal tracking of failures --- .../tools/dotc/transform/PatternMatcher.scala | 75 ++++++++++--------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 94eca4683132..5157b46991b5 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -33,7 +33,8 @@ import scala.reflect.internal.util.Collections class PatternMatcher extends TreeTransform { import dotty.tools.dotc.ast.tpd._ - def name: String = "patternMatcher" + def name: String = "patternMatcher" + var _id = 0 /*override def transformCaseDef(tree: CaseDef)(implicit ctx: Context, info: TransformerInfo): Tree = cpy.CaseDef(tree, Literal(Constant("")), tree.guard, tree.body)*/ @@ -46,15 +47,7 @@ class PatternMatcher extends TreeTransform { val origTp = tree.tpe // setType origTp intended for CPS -- TODO: is it necessary? val translated = new Translator()(ctx).translator.translateMatch(tree) - /*try { - localTyper.typed(translated) setType origTp - } catch { - case x: (Types#TypeError) => - // TODO: this should never happen; error should've been reported during type checking - unit.error(tree.pos, "error during expansion of this match (this is a scalac bug).\nThe underlying error was: "+ x.msg) - translated - }*/ - translated + translated } class Translator(implicit ctx: Context) { @@ -72,10 +65,10 @@ class PatternMatcher extends TreeTransform { object debug { //val printPatmat = global.settings.Ypatmatdebug.value final def patmat(s: => String) = /*if (printPatmat) Console.err.*/ - println(s) + ctx.debuglog(s) final def patmatResult[T](s: => String)(result: T): T = { /*if (printPatmat) Console.err.*/ - println(s + ": " + result) + ctx.debuglog(s + ": " + result) result } } @@ -294,14 +287,20 @@ class PatternMatcher extends TreeTransform { // returns MatchMonad[U] def flatMap(prev: Tree, b: Symbol, next: Tree): Tree = { val prevSym = freshSym(prev.pos, prev.tpe, "o") - Block( - List(ValDef(prevSym, prev)), - // must be isEmpty and get as we don't control the target of the call (prev is an extractor call) - ifThenElseZero( - ref(prevSym).select("isEmpty".toTermName).select(ctx.definitions.Boolean_!), - Substitution(b, ref(prevSym).select("get".toTermName))(next) + val getTp = extractorMemberType(prev.tpe, nme.get) + if(getTp.exists) { + Block( + List(ValDef(prevSym, prev)), + // must be isEmpty and get as we don't control the target of the call (prev is an extractor call) + ifThenElseZero( + ref(prevSym).select("isEmpty".toTermName).select(ctx.definitions.Boolean_!), + Substitution(b, ref(prevSym).select("get".toTermName))(next) + ) ) - ) + } else { + assert(defn.isProductSubType(prev.tpe)) + Substitution(b, ref(prevSym))(next) + } } // cond: Boolean @@ -363,7 +362,7 @@ class PatternMatcher extends TreeTransform { private[TreeMakers] def incorporateOuterSubstitution(outerSubst: Substitution): Unit = { if (currSub ne null) { - println("BUG: incorporateOuterSubstitution called more than once for "+ ((this, currSub, outerSubst))) + ctx.debuglog("BUG: incorporateOuterSubstitution called more than once for "+ ((this, currSub, outerSubst))) Thread.dumpStack() } else currSub = outerSubst >> substitution @@ -509,7 +508,7 @@ class PatternMatcher extends TreeTransform { def extraStoredBinders: Set[Symbol] = Set() - println(s""" + ctx.debuglog(s""" |ExtractorTreeMaker($extractor, $extraCond, $nextBinder) { | $subPatBinders | $subPatRefs @@ -845,7 +844,7 @@ class PatternMatcher extends TreeTransform { /*fixerUpper(owner, scrut.pos)*/ { def matchFailGen = matchFailGenOverride orElse Some((arg: Symbol) => Throw(New(defn.MatchErrorType, List(ref(arg))))) - println("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) + ctx.debuglog("combining cases: "+ (casesNoSubstOnly.map(_.mkString(" >> ")).mkString("{", "\n", "}"))) val (suppression, requireSwitch): (Suppression, Boolean) = /*if (settings.XnoPatmatAnalysis)*/ (Suppression.NoSuppression, false) @@ -986,14 +985,15 @@ class PatternMatcher extends TreeTransform { // Always map repeated params to sequences private def setVarInfo(sym: Symbol, info: Type) ={ //setInfo debug.patmatResult(s"changing ${sym.defString} to")(repeatedToSeq(info)) - assert(sym.info =:= info, "bug in porting pattern matcher") +// if(info!= NoType && !(sym.info =:= info)) +// assert(false, "bug in porting pattern matcher") sym } def newBoundTree(tree: Tree, pt: Type): BoundTree = tree match { case SymbolBound(sym, expr) => BoundTree(setVarInfo(sym, pt), expr) - case _ => BoundTree(setVarInfo(freshSym(tree.pos, prefix = "p"), pt), tree) + case _ => BoundTree(freshSym(tree.pos, pt, prefix = "p"), tree) } final case class BoundTree(binder: Symbol, tree: Tree) { @@ -1183,7 +1183,7 @@ class PatternMatcher extends TreeTransform { /*if (phase.id >= currentRun.uncurryPhase.id) devWarning(s"running translateMatch past uncurry (at $phase) on $selector match $cases")*/ - println("translating "+ cases.mkString("{", "\n", "}")) + ctx.debuglog("translating "+ cases.mkString("{", "\n", "}")) //val start = if (Statistics.canEnable) Statistics.startTimer(patmatNanos) else null @@ -1404,7 +1404,7 @@ class PatternMatcher extends TreeTransform { // codegen.drop(seqTree(binder))(nbIndexingIndices)))).toList protected def seqTree(binder: Symbol) = tupleSel(binder)(firstIndexingBinder + 1) protected def tupleSel(binder: Symbol)(i: Int): Tree = { - val accessors = if(defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.decls.filter(_.is(Flags.CaseAccessor)).toList + val accessors = if(defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.decls.filter(x => x.is(Flags.CaseAccessor) && x.is(Flags.Method)).toList if (accessors.isDefinedAt(i-1)) ref(binder).select(accessors(i-1)) else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN } @@ -1645,6 +1645,9 @@ class PatternMatcher extends TreeTransform { * @param repeated The sequence type which is extracted */ final case class Extractor(whole: Type, fixed: List[Type], repeated: Repeated) { + if(fixed.isEmpty) { + println("here") + } require(whole != NoType, s"expandTypes($whole, $fixed, $repeated)") def productArity = fixed.length @@ -1774,19 +1777,23 @@ class PatternMatcher extends TreeTransform { def hasSelectors(tpe: Type) = tpe.member(nme._1).exists && tpe.member(nme._2).exists // dd todo: ??? + /** In this case, expansion is based on an unapply or unapplySeq method. * Unfortunately the MethodType does not carry the information of whether * it was unapplySeq, so we have to funnel that information in separately. */ def unapplyMethodTypes(method: Type, isSeq: Boolean): Extractor = { + _id = _id + 1 + if(_id == 3) + println("here") + val whole = method.paramTypess.headOption.flatMap(_.headOption).getOrElse(NoType)//firstParamType(method) val result = method.finalResultType val expanded = ( if (result =:= ctx.definitions.BooleanType) Nil - else typeOfMemberNamedGet(result) match { - case rawGet if !hasSelectors(rawGet) => rawGet :: Nil - case rawGet => productSelectorTypes(rawGet) - } + else if(defn.isProductSubType(result)) productSelectorTypes(result) + else if(result.classSymbol is Flags.CaseClass) result.decls.filter(x => x.is(Flags.CaseAccessor) && x.is(Flags.Method)).map(_.info).toList + else result.select(nme.get) :: Nil ) expanded match { case init :+ last if isSeq => newExtractor(whole, init, repeatedFromSeq(last)) @@ -1811,7 +1818,7 @@ class PatternMatcher extends TreeTransform { def err(msg: String) = ctx.error(msg, tree.pos) def warn(msg: String) = ctx.warning(msg, tree.pos) - def arityError(what: String) = err(s"$what patterns for $owner$offerString: expected $arityExpected, found $totalArity") + def arityError(what: String) = err(s"${_id} $what patterns for $owner$offerString: expected $arityExpected, found $totalArity") if (isStar && !isSeq) err("Star pattern must correspond with varargs or unapplySeq") @@ -1832,9 +1839,9 @@ class PatternMatcher extends TreeTransform { val isSeq = sel.symbol.name == nme.unapplySeq val isUnapply = sel.symbol.name == nme.unapply val extractor = sel.symbol.name match { - case nme.unapply => unapplyMethodTypes(fn.tpe, isSeq = false) - case nme.unapplySeq => unapplyMethodTypes(fn.tpe, isSeq = true) - case _ => applyMethodTypes(fn.tpe) + case nme.unapply => unapplyMethodTypes(/*fn*/sel.tpe, isSeq = false) + case nme.unapplySeq => unapplyMethodTypes(/*fn*/sel.tpe, isSeq = true) + case _ => applyMethodTypes(/*fn*/sel.tpe) } /** Rather than let the error that is SI-6675 pollute the entire matching From fb3e44afeb5a72b5cff8d037aa248d8e969bebcb Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 15:59:39 +0200 Subject: [PATCH 19/23] Most of tests succeed now. --- .../tools/dotc/transform/PatternMatcher.scala | 120 ++++++++++++------ 1 file changed, 81 insertions(+), 39 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 5157b46991b5..cd8cf19995de 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -19,6 +19,7 @@ import typer.ErrorReporting._ import ast.Trees._ import Applications._ import TypeApplications._ +import TypeUtils._ import dotty.tools.dotc.util.Positions.Position import dotty.tools.dotc.core.Decorators._ @@ -36,13 +37,6 @@ class PatternMatcher extends TreeTransform { def name: String = "patternMatcher" var _id = 0 - /*override def transformCaseDef(tree: CaseDef)(implicit ctx: Context, info: TransformerInfo): Tree = - cpy.CaseDef(tree, Literal(Constant("")), tree.guard, tree.body)*/ - - - /*case Try(block, catches, finalizer) => - treeCopy.Try(tree, transform(block), translator.translateTry(transformTrees(catches).asInstanceOf[List[CaseDef]], tree.tpe, tree.pos), transform(finalizer)) - case _ => super.transform(tree)*/ override def transformMatch(tree: tpd.Match)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { val origTp = tree.tpe // setType origTp intended for CPS -- TODO: is it necessary? @@ -135,7 +129,10 @@ class PatternMatcher extends TreeTransform { DefDef(arg, body) def tupleSel(binder: Symbol)(i: Int): Tree = ref(binder).select(nme.productAccessorName(i)) // make tree that accesses the i'th component of the tuple referenced by binder - def index(tgt: Tree)(i: Int): Tree = tgt.appliedTo(Literal(Constant(i))) + def index(tgt: Tree)(i: Int): Tree = { + if (i > 0) tgt.select(defn.Seq_apply).appliedTo(Literal(Constant(i))) + else tgt.select(defn.Seq_head) + } // Right now this blindly calls drop on the result of the unapplySeq // unless it verifiably has no drop method (this is the case in particular @@ -151,7 +148,7 @@ class PatternMatcher extends TreeTransform { } // NOTE: checker must be the target of the ==, that's the patmat semantics for ya - def _equals(checker: Tree, binder: Symbol): Tree = checker.select(defn.Any_equals).appliedTo(ref(binder)) + def _equals(checker: Tree, binder: Symbol): Tree = checker.select(nme.equals_).appliedTo(ref(binder)) // the force is needed mainly to deal with the GADT typing hack (we can't detect it otherwise as tp nor pt need contain an abstract type, we're just casting wildly) def _asInstanceOf(b: Symbol, tp: Type): Tree = if (b.info <:< tp) ref(b) else ref(b).select(defn.Any_asInstanceOf).appliedToType(tp) @@ -288,15 +285,17 @@ class PatternMatcher extends TreeTransform { def flatMap(prev: Tree, b: Symbol, next: Tree): Tree = { val prevSym = freshSym(prev.pos, prev.tpe, "o") val getTp = extractorMemberType(prev.tpe, nme.get) - if(getTp.exists) { - Block( - List(ValDef(prevSym, prev)), - // must be isEmpty and get as we don't control the target of the call (prev is an extractor call) - ifThenElseZero( - ref(prevSym).select("isEmpty".toTermName).select(ctx.definitions.Boolean_!), - Substitution(b, ref(prevSym).select("get".toTermName))(next) + val isDefined = extractorMemberType(prev.tpe, nme.isDefined) + + if ((isDefined isRef defn.BooleanClass) && getTp.exists) { + Block( + List(ValDef(prevSym, prev)), + // must be isEmpty and get as we don't control the target of the call (prev is an extractor call) + ifThenElseZero( + ref(prevSym).select(nme.isDefined).select(ctx.definitions.Boolean_!), + Substitution(b, ref(prevSym).select("get".toTermName))(next) + ) ) - ) } else { assert(defn.isProductSubType(prev.tpe)) Substitution(b, ref(prevSym))(next) @@ -633,9 +632,9 @@ class PatternMatcher extends TreeTransform { def outerTest(testedBinder: Symbol, expectedTp: Type): Tree = { val expectedOuter = expectedTp.normalizedPrefix match { - case ThisType(clazz) => This(clazz) - case NoType => Literal(Constant(true)) // fallback for SI-6183 todo? - case pre => ref(pre.termSymbol) + //case ThisType(clazz) => This(clazz) + //case NoType => Literal(Constant(true)) // fallback for SI-6183 todo? + case pre => ref(pre.typeSymbol) } // ExplicitOuter replaces `Select(q, outerSym) OBJ_EQ expectedPrefix` by `Select(q, outerAccessor(outerSym.owner)) OBJ_EQ expectedPrefix` @@ -700,10 +699,47 @@ class PatternMatcher extends TreeTransform { import TypeTestTreeMaker._ ctx.debuglog("TTTM"+((prevBinder, extractorArgTypeTest, testedBinder, expectedTp, nextBinderTp))) + def needsOuterTest(patType: Type, selType: Type, currentOwner: Symbol) = false /* todo { + def createDummyClone(pre: Type): Type = { + val dummy = currentOwner.enclClass.newValue(nme.ANYname).setInfo(pre.widen) + singleType(ThisType(currentOwner.enclClass), dummy) + } + def maybeCreateDummyClone(pre: Type, sym: Symbol): Type = pre match { + case SingleType(pre1, sym1) => + if (sym1.isModule && sym1.isStatic) { + NoType + } else if (sym1.isModule && sym.owner == sym1.moduleClass) { + val pre2 = maybeCreateDummyClone(pre1, sym1) + if (pre2 eq NoType) pre2 + else singleType(pre2, sym1) + } else { + createDummyClone(pre) + } + case ThisType(clazz) => + if (clazz.isModuleClass) + maybeCreateDummyClone(clazz.typeOfThis, sym) + else if (sym.owner == clazz && (sym.hasFlag(PRIVATE) || sym.privateWithin == clazz)) + NoType + else + createDummyClone(pre) + case _ => + NoType + } + // See the test for SI-7214 for motivation for dealias. Later `treeCondStrategy#outerTest` + // generates an outer test based on `patType.prefix` with automatically dealises. + patType.dealias match { + case TypeRef(pre, sym, args) => + val pre1 = maybeCreateDummyClone(pre, sym) + (pre1 ne NoType) && isPopulated(copyTypeRef(patType, pre1, sym, args), selType) + case _ => + false + } + }*/ + lazy val outerTestNeeded = ( (expectedTp.normalizedPrefix.typeSymbol ne NoSymbol) && !expectedTp.normalizedPrefix.typeSymbol.isPackageObject - && true //needsOuterTest(expectedTp, testedBinder.info, matchOwner) // todo + && needsOuterTest(expectedTp, testedBinder.info, matchOwner) ) // the logic to generate the run-time test that follows from the fact that @@ -757,7 +793,7 @@ class PatternMatcher extends TreeTransform { case ConstantType(Constant(null)) if isAnyRef => mkEqTest(expTp(Literal(Constant(null)))) case ConstantType(const) => mkEqualsTest(expTp(Literal(const))) case t:SingletonType => mkEqTest(singleton(expectedTp)) // SI-4577, SI-4897 - case ThisType(sym) => mkEqTest(expTp(This(sym))) + //case ThisType(sym) => mkEqTest(expTp(This(sym))) case _ => mkDefault } } @@ -987,6 +1023,7 @@ class PatternMatcher extends TreeTransform { //setInfo debug.patmatResult(s"changing ${sym.defString} to")(repeatedToSeq(info)) // if(info!= NoType && !(sym.info =:= info)) // assert(false, "bug in porting pattern matcher") + ctx.debuglog(s"scalac would be resetting info of $sym from ${sym.info} to $info") sym } @@ -1088,7 +1125,6 @@ class PatternMatcher extends TreeTransform { def translate(): List[TreeMaker] = nextStep() merge (_.translate()) private def setInfo(paramType: Type): Boolean = { - ctx.warning(s"resetting info of $this to $paramType") setVarInfo(binder, paramType) true } @@ -1344,8 +1380,12 @@ class PatternMatcher extends TreeTransform { object ExtractorCall { // TODO: check unargs == args def apply(tree: Tree, binder: Symbol): ExtractorCall = tree match { - case UnApply(unfun, implicits, args) => new ExtractorCallRegular(alignPatterns(tree), unfun.appliedTo(ref(binder)), args, tree.tpe) // extractor - case Typed(UnApply(unfun, implicits, args), tpt) => new ExtractorCallRegular(alignPatterns(tree), unfun.appliedTo(ref(binder)), args, tree.tpe) // extractor + case UnApply(unfun, implicits, args) => + val synth = if(implicits.isEmpty) unfun.appliedTo(ref(binder)) else unfun.appliedTo(ref(binder)).appliedToArgs(implicits) + new ExtractorCallRegular(alignPatterns(synth), synth, args, tree.tpe) // extractor + case Typed(UnApply(unfun, implicits, args), tpt) => + val synth = /*Typed(*/if(implicits.isEmpty) unfun.appliedTo(ref(binder)) else unfun.appliedTo(ref(binder)).appliedToArgs(implicits)//, tpt) + new ExtractorCallRegular(alignPatterns(synth), synth, args, tree.tpe) // extractor case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree), fun, args, fun.tpe) // case class } } @@ -1404,7 +1444,7 @@ class PatternMatcher extends TreeTransform { // codegen.drop(seqTree(binder))(nbIndexingIndices)))).toList protected def seqTree(binder: Symbol) = tupleSel(binder)(firstIndexingBinder + 1) protected def tupleSel(binder: Symbol)(i: Int): Tree = { - val accessors = if(defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.decls.filter(x => x.is(Flags.CaseAccessor) && x.is(Flags.Method)).toList + val accessors = if(defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.caseAccessors if (accessors.isDefinedAt(i-1)) ref(binder).select(accessors(i-1)) else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN } @@ -1437,7 +1477,8 @@ class PatternMatcher extends TreeTransform { else productElemsToN(binder, totalArity) ) - val mathSignum = ref(defn.ScalaMathPackageVal).select("signum".toTermName) + val mathSignymSymbol = defn.ScalaMathPackageVal.requiredMethod("signum".toTermName, List(defn.IntType)) + val mathSignum = ref(defn.ScalaMathPackageVal).select(mathSignymSymbol) private def compareInts(t1: Tree, t2: Tree) = @@ -1486,7 +1527,7 @@ class PatternMatcher extends TreeTransform { * when `binderKnownNonNull` is `true`, `ProductExtractorTreeMaker` does not do a (redundant) null check on binder */ def treeMaker(binder: Symbol, binderKnownNonNull: Boolean, pos: Position): TreeMaker = { - val paramAccessors = binder.info.costrParamAccessors + val paramAccessors = binder.info.caseAccessors // binders corresponding to mutable fields should be stored (SI-5158, SI-6070) // make an exception for classes under the scala package as they should be well-behaved, // to optimize matching on List @@ -1645,7 +1686,7 @@ class PatternMatcher extends TreeTransform { * @param repeated The sequence type which is extracted */ final case class Extractor(whole: Type, fixed: List[Type], repeated: Repeated) { - if(fixed.isEmpty) { + if(fixed.isEmpty || whole == NoType) { println("here") } require(whole != NoType, s"expandTypes($whole, $fixed, $repeated)") @@ -1782,19 +1823,20 @@ class PatternMatcher extends TreeTransform { * Unfortunately the MethodType does not carry the information of whether * it was unapplySeq, so we have to funnel that information in separately. */ - def unapplyMethodTypes(method: Type, isSeq: Boolean): Extractor = { + def unapplyMethodTypes(tree:Tree, fun: Tree, args:List[Tree], isSeq: Boolean): Extractor = { _id = _id + 1 if(_id == 3) println("here") - val whole = method.paramTypess.headOption.flatMap(_.headOption).getOrElse(NoType)//firstParamType(method) - val result = method.finalResultType - val expanded = ( + val whole = fun.tpe.widen.paramTypess.headOption.flatMap(_.headOption).getOrElse(NoType)//firstParamType(method) + val result = tree.tpe.widen// fun.tpe.fin + //println(s"${_id}unapplyArgs(${result.widen}") + val expanded = /*( if (result =:= ctx.definitions.BooleanType) Nil else if(defn.isProductSubType(result)) productSelectorTypes(result) else if(result.classSymbol is Flags.CaseClass) result.decls.filter(x => x.is(Flags.CaseAccessor) && x.is(Flags.Method)).map(_.info).toList else result.select(nme.get) :: Nil - ) + )*/ unapplyArgs(result, fun, args) expanded match { case init :+ last if isSeq => newExtractor(whole, init, repeatedFromSeq(last)) case tps => newExtractor(whole, tps, NoRepeated) @@ -1830,7 +1872,7 @@ class PatternMatcher extends TreeTransform { aligned } - def apply(sel: Tree, args: List[Tree]): Aligned = { + def apply(tree:Tree, sel: Tree, args: List[Tree]): Aligned = { val fn = sel match { case Unapplied(fn) => fn case _ => sel @@ -1839,8 +1881,8 @@ class PatternMatcher extends TreeTransform { val isSeq = sel.symbol.name == nme.unapplySeq val isUnapply = sel.symbol.name == nme.unapply val extractor = sel.symbol.name match { - case nme.unapply => unapplyMethodTypes(/*fn*/sel.tpe, isSeq = false) - case nme.unapplySeq => unapplyMethodTypes(/*fn*/sel.tpe, isSeq = true) + case nme.unapply => unapplyMethodTypes(tree, /*fn*/sel, args, isSeq = false) + case nme.unapplySeq => unapplyMethodTypes(tree, /*fn*/sel, args, isSeq = true) case _ => applyMethodTypes(/*fn*/sel.tpe) } @@ -1861,8 +1903,8 @@ class PatternMatcher extends TreeTransform { def apply(tree: Tree): Aligned = tree match { case Typed(tree, _) => apply(tree) - case Apply(fn, args) => apply(fn, args) - case UnApply(fn, implicits, args) => apply(fn, args) + case Apply(fn, args) => apply(tree, fn, args) + case UnApply(fn, implicits, args) => apply(tree, fn, args) } } } From ba038e54ddbadb2b4095b176d90bfeacf6e6ebe7 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 16:01:13 +0200 Subject: [PATCH 20/23] Everything except unapplySeq doesn't explode. That doesn't mean that it works. --- src/dotty/tools/dotc/Run.scala | 2 +- .../tools/dotc/transform/PatternMatcher.scala | 115 +++++++----------- 2 files changed, 48 insertions(+), 69 deletions(-) diff --git a/src/dotty/tools/dotc/Run.scala b/src/dotty/tools/dotc/Run.scala index a639b20cd770..d3225d6b568c 100644 --- a/src/dotty/tools/dotc/Run.scala +++ b/src/dotty/tools/dotc/Run.scala @@ -54,7 +54,7 @@ class Run(comp: Compiler)(implicit ctx: Context) { private def printTree(ctx: Context) = { val unit = ctx.compilationUnit println(s"result of $unit after ${ctx.phase.prev}:") - println(unit.tpdTree.show(ctx)) + println(unit.tpdTree) } def compile(sourceCode: String): Unit = { diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index cd8cf19995de..d276e689fbf8 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -34,64 +34,39 @@ import scala.reflect.internal.util.Collections class PatternMatcher extends TreeTransform { import dotty.tools.dotc.ast.tpd._ + + /** List of names of phases that should precede this phase */ + override def runsAfter: Set[String] = Set("elimrepeated") + def name: String = "patternMatcher" var _id = 0 override def transformMatch(tree: tpd.Match)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = { - val origTp = tree.tpe - // setType origTp intended for CPS -- TODO: is it necessary? val translated = new Translator()(ctx).translator.translateMatch(tree) translated } class Translator(implicit ctx: Context) { - def translator = { new OptimizingMatchTranslator/*(localTyper)*/ } class OptimizingMatchTranslator extends MatchOptimizer/*(val typer: analyzer.Typer)*/ with MatchTranslator - trait Debugging { - - // TODO: the inliner fails to inline the closures to debug.patmat unless the method is nested in an object - object debug { - //val printPatmat = global.settings.Ypatmatdebug.value - final def patmat(s: => String) = /*if (printPatmat) Console.err.*/ - ctx.debuglog(s) - final def patmatResult[T](s: => String)(result: T): T = { - /*if (printPatmat) Console.err.*/ - ctx.debuglog(s + ": " + result) - result - } - } - } - trait MatchMonadInterface { // val typer: Typer def matchOwner(implicit ctx: Context) = ctx.owner def pureType(tp: Type): Type = tp - def reportUnreachable(pos: Position) = { - ctx.warning("unreachable code", pos) - } - def reportMissingCases(pos: Position, counterExamples: List[String]) = { - val ceString = - if (counterExamples.tail.isEmpty) "input: " + counterExamples.head - else "inputs: " + counterExamples.mkString(", ") - - ctx.warning("match may not be exhaustive.\nIt would fail on the following "+ ceString, pos) - } } trait CodegenCore extends MatchMonadInterface { private var ctr = 0 - def freshName(prefix: String) = ctx.freshName(prefix).toTermName // assert(owner ne null); assert(owner ne NoSymbol) def freshSym(pos: Position, tp: Type = NoType, prefix: String = "x") = - ctx.newSymbol(ctx.owner, freshName(prefix), Flags.Synthetic, tp, coord = pos) + ctx.newSymbol(ctx.owner, ctx.freshName(prefix).toTermName, Flags.Synthetic, tp, coord = pos) def newSynthCaseLabel(name: String, tpe:Type) = ctx.newSymbol(ctx.owner, ctx.freshName(name).toTermName, Flags.Label, tpe) //NoSymbol.newLabel(freshName(name), NoPosition) setFlag treeInfo.SYNTH_CASE_FLAGS @@ -175,12 +150,6 @@ class PatternMatcher extends TreeTransform { // since about half of the typedSubst's end up being no-ops, the check below shaves off 5% of the time spent in typedSubst /*if (!tree.exists { case i@Ident(_) => from contains i.symbol case _ => false}) tree else*/ (new TreeMap { - /*private def typedIfOrigTyped(to: Tree, origTp: Type): Tree = - if (origTp == null || origTp == NoType) to - // important: only type when actually substing and when original tree was typed - // (don't need to use origTp as the expected type, though, and can't always do this anyway due to unknown type params stemming from polymorphic extractors) - else typer.typed(to)*/ - override def transform(tree: Tree)(implicit ctx: Context): Tree = { def subst(from: List[Symbol], to: List[Tree]): Tree = if (from.isEmpty) tree @@ -341,11 +310,11 @@ class PatternMatcher extends TreeTransform { def analyzeCases(prevBinder: Symbol, cases: List[List[TreeMaker]], pt: Type, suppression: Suppression): Unit = {} def emitSwitch(scrut: Tree, scrutSym: Symbol, cases: List[List[TreeMaker]], pt: Type, matchFailGenOverride: Option[Symbol => Tree], unchecked: Boolean): Option[Tree] = - None + None // todo // for catch (no need to customize match failure) def emitTypeSwitch(bindersAndCases: List[(Symbol, List[TreeMaker])], pt: Type): Option[List[CaseDef]] = - None + None // todo abstract class TreeMaker{ def pos: Position @@ -645,7 +614,7 @@ class PatternMatcher extends TreeTransform { } } - object pureTypeTestChecker extends TypeTestCondStrategy { + /*object pureTypeTestChecker extends TypeTestCondStrategy { type Result = Boolean def typeTest(testedBinder: Symbol, expectedTp: Type): Result = true @@ -656,7 +625,7 @@ class PatternMatcher extends TreeTransform { def eqTest(pat: Tree, testedBinder: Symbol): Result = false def and(a: Result, b: Result): Result = false // we don't and type tests, so the conjunction must include at least one false def tru = true - } + }*/ def nonNullImpliedByTestChecker(binder: Symbol) = new TypeTestCondStrategy { type Result = Boolean @@ -802,9 +771,13 @@ class PatternMatcher extends TreeTransform { val res = codegen._asInstanceOf(testedBinder, nextBinderTp) // is this purely a type test, e.g. no outer check, no equality tests (used in switch emission) - def isPureTypeTest = renderCondition(pureTypeTestChecker) + //def isPureTypeTest = renderCondition(pureTypeTestChecker) - def impliesBinderNonNull(binder: Symbol) = renderCondition(nonNullImpliedByTestChecker(binder)) + def impliesBinderNonNull(binder: Symbol):Boolean = + // @odersky: scalac is able to infer in this method that nonNullImpliedByTestChecker.Result, + // dotty instead infers type projection TreeMakers.this.TypeTestTreeMaker.TypeTestCondStrategy#Result + // which in turn doesn't typecheck in this method. Can you please explain why? + renderCondition(nonNullImpliedByTestChecker(binder)).asInstanceOf[Boolean] override def toString = "TT"+((expectedTp, testedBinder.name, nextBinderTp)) } @@ -836,7 +809,7 @@ class PatternMatcher extends TreeTransform { ((casegen: Casegen) => combineExtractors(altTreeMakers :+ TrivialTreeMaker(casegen.one(Literal(Constant(true)))))(casegen)) ) - val findAltMatcher = codegenAlt.matcher(EmptyTree, NoSymbol, ctx.definitions.BooleanType)(combinedAlts, Some(x => Literal(Constant(false)))) + val findAltMatcher = codegenAlt.matcher(EmptyTree, NoSymbol, ctx.definitions.BooleanType)(combinedAlts, Some((x: Symbol) => Literal(Constant(false)))) codegenAlt.ifThenElseZero(findAltMatcher, substitution(next)) } } @@ -854,7 +827,6 @@ class PatternMatcher extends TreeTransform { def combineExtractors(treeMakers: List[TreeMaker])(casegen: Casegen): Tree = treeMakers.foldRight(EmptyTree: Tree)((a, b) => a.chainBefore(b)(casegen)) - def removeSubstOnly(makers: List[TreeMaker]) = makers filterNot (_.isInstanceOf[SubstOnlyTreeMaker]) // a foldLeft to accumulate the localSubstitution left-to-right @@ -920,7 +892,7 @@ class PatternMatcher extends TreeTransform { val (cases, toHoist) = optimizeCases(scrutSym, casesNoSubstOnly, pt) - val matchRes = codegen.matcher(scrut, scrutSym, pt)(cases map combineExtractors, synthCatchAll) + val matchRes = codegen.matcher(scrut, scrutSym, pt)(cases.map(x => combineExtractors(x) _), synthCatchAll) if (toHoist isEmpty) matchRes else Block(toHoist, matchRes) } else { @@ -1043,6 +1015,7 @@ class PatternMatcher extends TreeTransform { case TypeBound(tpe) => tpe case tree => tree.tpe } + def glbWith(other: Type) = ctx.typeComparer.glb(tpe :: other :: Nil)// .normalize object SymbolAndTypeBound { @@ -1086,7 +1059,7 @@ class PatternMatcher extends TreeTransform { // check whether typetest implies binder is not null, // even though the eventual null check will be on typeTest.nextBinder // it'll be equal to binder casted to paramType anyway (and the type test is on binder) - def extraction: TreeMaker = treeMaker(typeTest.nextBinder, typeTest impliesBinderNonNull binder, pos) + def extraction: TreeMaker = treeMaker(typeTest.nextBinder, typeTest.impliesBinderNonNull(binder), pos) // paramType = the type expected by the unapply // TODO: paramType may contain unbound type params (run/t2800, run/t3530) @@ -1247,7 +1220,7 @@ class PatternMatcher extends TreeTransform { // there's no need to check the scrutinee for null -- "throw null" becomes "throw new NullPointerException" // try to simplify to a type-based switch, or fall back to a catch-all case that runs a normal pattern match // unlike translateMatch, we type our result before returning it - def translateTry(caseDefs: List[CaseDef], pt: Type, pos: Position): List[CaseDef] = + /*def translateTry(caseDefs: List[CaseDef], pt: Type, pos: Position): List[CaseDef] = // if they're already simple enough to be handled by the back-end, we're done if (caseDefs forall isCatchCase) caseDefs else { @@ -1274,13 +1247,13 @@ class PatternMatcher extends TreeTransform { CaseDef( Bind(exSym, Ident(??? /*nme.WILDCARD*/)), // TODO: does this need fixing upping? EmptyTree, - combineCasesNoSubstOnly(ref(exSym), scrutSym, casesNoSubstOnly, pt, matchOwner, Some(scrut => Throw(ref(exSym)))) + combineCasesNoSubstOnly(ref(exSym), scrutSym, casesNoSubstOnly, pt, matchOwner, Some((scrut: Symbol) => Throw(ref(exSym)))) ) ) } /*typer.typedCases(*/catches/*, ctx.definitions.ThrowableType, WildcardType)*/ - } + }*/ /** The translation of `pat if guard => body` has two aspects: * 1) the substitution due to the variables bound by patterns @@ -1381,17 +1354,19 @@ class PatternMatcher extends TreeTransform { // TODO: check unargs == args def apply(tree: Tree, binder: Symbol): ExtractorCall = tree match { case UnApply(unfun, implicits, args) => - val synth = if(implicits.isEmpty) unfun.appliedTo(ref(binder)) else unfun.appliedTo(ref(binder)).appliedToArgs(implicits) - new ExtractorCallRegular(alignPatterns(synth), synth, args, tree.tpe) // extractor + val synth = if (implicits.isEmpty) unfun.appliedTo(ref(binder)) else unfun.appliedTo(ref(binder)).appliedToArgs(implicits) + new ExtractorCallRegular(alignPatterns(tree, synth.tpe), synth, args, synth.tpe) // extractor case Typed(UnApply(unfun, implicits, args), tpt) => - val synth = /*Typed(*/if(implicits.isEmpty) unfun.appliedTo(ref(binder)) else unfun.appliedTo(ref(binder)).appliedToArgs(implicits)//, tpt) - new ExtractorCallRegular(alignPatterns(synth), synth, args, tree.tpe) // extractor - case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree), fun, args, fun.tpe) // case class + val synth = /*Typed(*/ if (implicits.isEmpty) unfun.appliedTo(ref(binder)) else unfun.appliedTo(ref(binder)).appliedToArgs(implicits) //, tpt) + new ExtractorCallRegular(alignPatterns(tree, synth.tpe), synth, args, tree.tpe) // extractor + case Apply(fun, args) => new ExtractorCallProd(alignPatterns(tree, tree.tpe), fun, args, fun.tpe) // case class } } abstract class ExtractorCall(val aligner: PatternAligned) { + import aligner._ + def args: List[Tree] // don't go looking for selectors if we only expect one pattern @@ -1402,9 +1377,12 @@ class PatternMatcher extends TreeTransform { case _ => or } - def typeOfMemberNamedGet(tp: Type) = typeArgOfBaseTypeOr(tp, defn.OptionClass)(tp.member(nme.get).info) - - def resultInMonad = resultType //if (isBool) defn.UnitType else typeOfMemberNamedGet(resultType) + def resultInMonad = if (aligner.isBool) defn.UnitType else { + val getTp = extractorMemberType(resultType, nme.get) + if ((extractorMemberType(resultType, nme.isDefined) isRef defn.BooleanClass) && getTp.exists) + getTp + else resultType + } def resultType: Type /** Create the TreeMaker that embodies this extractor call @@ -1444,8 +1422,8 @@ class PatternMatcher extends TreeTransform { // codegen.drop(seqTree(binder))(nbIndexingIndices)))).toList protected def seqTree(binder: Symbol) = tupleSel(binder)(firstIndexingBinder + 1) protected def tupleSel(binder: Symbol)(i: Int): Tree = { - val accessors = if(defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.caseAccessors - if (accessors.isDefinedAt(i-1)) ref(binder).select(accessors(i-1)) + val accessors = if (defn.isProductSubType(binder.info)) productSelectors(binder.info) else binder.info.caseAccessors + if (accessors.isDefinedAt(i - 1)) ref(binder).select(accessors(i - 1)) else codegen.tupleSel(binder)(i) // this won't type check for case classes, as they do not inherit ProductN } @@ -1778,7 +1756,7 @@ class PatternMatcher extends TreeTransform { } def typeOfMemberNamedHead(tpe: Type): Type = tpe.select(nme.head) def typeOfMemberNamedApply(tpe: Type): Type = tpe.select(nme.apply) - def typeOfMemberNamedGet(tpe: Type): Type = tpe.select(nme.get) + def elementTypeOf(tpe: Type) = { val seq = tpe //repeatedToSeq(tpe) @@ -1823,7 +1801,7 @@ class PatternMatcher extends TreeTransform { * Unfortunately the MethodType does not carry the information of whether * it was unapplySeq, so we have to funnel that information in separately. */ - def unapplyMethodTypes(tree:Tree, fun: Tree, args:List[Tree], isSeq: Boolean): Extractor = { + def unapplyMethodTypes(tree:Tree, fun: Tree, args:List[Tree], resultType:Type, isSeq: Boolean): Extractor = { _id = _id + 1 if(_id == 3) println("here") @@ -1843,6 +1821,7 @@ class PatternMatcher extends TreeTransform { } } } + object alignPatterns extends ScalacPatternExpander { /** Converts a T => (A, B, C) extractor to a T => ((A, B, CC)) extractor. */ @@ -1872,7 +1851,7 @@ class PatternMatcher extends TreeTransform { aligned } - def apply(tree:Tree, sel: Tree, args: List[Tree]): Aligned = { + def apply(tree:Tree, sel: Tree, args: List[Tree], resultType: Type): Aligned = { val fn = sel match { case Unapplied(fn) => fn case _ => sel @@ -1881,8 +1860,8 @@ class PatternMatcher extends TreeTransform { val isSeq = sel.symbol.name == nme.unapplySeq val isUnapply = sel.symbol.name == nme.unapply val extractor = sel.symbol.name match { - case nme.unapply => unapplyMethodTypes(tree, /*fn*/sel, args, isSeq = false) - case nme.unapplySeq => unapplyMethodTypes(tree, /*fn*/sel, args, isSeq = true) + case nme.unapply => unapplyMethodTypes(tree, /*fn*/sel, args, resultType, isSeq = false) + case nme.unapplySeq => unapplyMethodTypes(tree, /*fn*/sel, args, resultType, isSeq = true) case _ => applyMethodTypes(/*fn*/sel.tpe) } @@ -1901,10 +1880,10 @@ class PatternMatcher extends TreeTransform { validateAligned(fn, Aligned(patterns, normalizedExtractor)) } - def apply(tree: Tree): Aligned = tree match { - case Typed(tree, _) => apply(tree) - case Apply(fn, args) => apply(tree, fn, args) - case UnApply(fn, implicits, args) => apply(tree, fn, args) + def apply(tree: Tree, resultType: Type): Aligned = tree match { + case Typed(tree, _) => apply(tree, resultType) + case Apply(fn, args) => apply(tree, fn, args, resultType) + case UnApply(fn, implicits, args) => apply(tree, fn, args, resultType) } } } From 074822922e64d16172c3a1eded3b926d1416eabc Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 16:02:46 +0200 Subject: [PATCH 21/23] Fixed unapplySeq. Patmat passes all test suite but typer itself breaks on it. --- .../tools/dotc/transform/PatternMatcher.scala | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index d276e689fbf8..eee49dab85eb 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -1807,14 +1807,23 @@ class PatternMatcher extends TreeTransform { println("here") val whole = fun.tpe.widen.paramTypess.headOption.flatMap(_.headOption).getOrElse(NoType)//firstParamType(method) - val result = tree.tpe.widen// fun.tpe.fin + val resultOfGet = extractorMemberType(resultType, nme.get) + //println(s"${_id}unapplyArgs(${result.widen}") - val expanded = /*( + val expanded:List[Type] = /*( if (result =:= ctx.definitions.BooleanType) Nil else if(defn.isProductSubType(result)) productSelectorTypes(result) else if(result.classSymbol is Flags.CaseClass) result.decls.filter(x => x.is(Flags.CaseAccessor) && x.is(Flags.Method)).map(_.info).toList else result.select(nme.get) :: Nil - )*/ unapplyArgs(result, fun, args) + )*/ + if ((extractorMemberType(resultType, nme.isDefined) isRef defn.BooleanClass) && resultOfGet.exists) + getUnapplySelectors(resultOfGet, args) + else if (defn.isProductSubType(resultType)) productSelectorTypes(resultType) + else { + ctx.error(i"invalid return type in Unapply node: $resultType") + Nil + } + expanded match { case init :+ last if isSeq => newExtractor(whole, init, repeatedFromSeq(last)) case tps => newExtractor(whole, tps, NoRepeated) From 2e2325133abf5d0c4057b7b1775bbda148aac317 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 16:16:16 +0200 Subject: [PATCH 22/23] Remaining definitions for patmat. --- src/dotty/tools/dotc/transform/PatternMatcher.scala | 13 ++++++++++++- src/dotty/tools/dotc/transform/TypeUtils.scala | 5 ++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index eee49dab85eb..185b5baf999b 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -1860,9 +1860,20 @@ class PatternMatcher extends TreeTransform { aligned } + object Applied { + // Duplicated with `spliceApply` + def unapply(tree: Tree): Option[Tree] = tree match { + // SI-7868 Admit Select() to account for numeric widening, e.g. .toInt + case Apply(fun, (Ident(nme.SELECTOR_DUMMY)| Select(Ident(nme.SELECTOR_DUMMY), _)) :: Nil) + => Some(fun) + case Apply(fun, _) => unapply(fun) + case _ => None + } + } + def apply(tree:Tree, sel: Tree, args: List[Tree], resultType: Type): Aligned = { val fn = sel match { - case Unapplied(fn) => fn + case Applied(fn) => fn case _ => sel } val patterns = newPatterns(args) diff --git a/src/dotty/tools/dotc/transform/TypeUtils.scala b/src/dotty/tools/dotc/transform/TypeUtils.scala index f11bb980acfc..74ba0a3db129 100644 --- a/src/dotty/tools/dotc/transform/TypeUtils.scala +++ b/src/dotty/tools/dotc/transform/TypeUtils.scala @@ -10,6 +10,8 @@ import StdNames.nme import NameOps._ import language.implicitConversions +import scala.language.implicitConversions + object TypeUtils { implicit def decorateTypeUtils(tpe: Type): TypeUtils = new TypeUtils(tpe) } @@ -18,6 +20,7 @@ object TypeUtils { * that are needed in the transofmer pipeline (not needed right now) */ class TypeUtils(val self: Type) extends AnyVal { - import TypeUtils._ + + def caseAccessors(implicit ctx:Context) = self.decls.filter(x => x.is(Flags.CaseAccessor) && x.is(Flags.Method)).toList } \ No newline at end of file From dd732c2888fdf0b48ae54edb04d0ee1d66cd2685 Mon Sep 17 00:00:00 2001 From: Dmitry Petrashko Date: Tue, 9 Sep 2014 16:28:11 +0200 Subject: [PATCH 23/23] Remove dead code and add todo about outer tests not being generated --- src/dotty/tools/dotc/transform/PatternMatcher.scala | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dotty/tools/dotc/transform/PatternMatcher.scala b/src/dotty/tools/dotc/transform/PatternMatcher.scala index 185b5baf999b..5f657542b6d9 100644 --- a/src/dotty/tools/dotc/transform/PatternMatcher.scala +++ b/src/dotty/tools/dotc/transform/PatternMatcher.scala @@ -30,6 +30,7 @@ import scala.reflect.internal.util.Collections /** This transform eliminates patterns. Right now it's a dummy. * Awaiting the real pattern matcher. * elimRepeated is required + * TODO: outer tests are not generated yet. */ class PatternMatcher extends TreeTransform { import dotty.tools.dotc.ast.tpd._ @@ -1563,7 +1564,7 @@ class PatternMatcher extends TreeTransform { if (aligner.isSingle) ref(binder) :: Nil // special case for extractors else super.subPatRefs(binder) - protected def spliceApply(binder: Symbol): Tree = { + /*protected def spliceApply(binder: Symbol): Tree = { object splice extends TreeMap { def binderRef(pos: Position): Tree = ref(binder) //setPos pos @@ -1580,7 +1581,7 @@ class PatternMatcher extends TreeTransform { } } splice transform extractorCallIncludingDummy - } + }*/ override def rawSubPatTypes = aligner.extractor.varargsTypes } @@ -1864,8 +1865,8 @@ class PatternMatcher extends TreeTransform { // Duplicated with `spliceApply` def unapply(tree: Tree): Option[Tree] = tree match { // SI-7868 Admit Select() to account for numeric widening, e.g. .toInt - case Apply(fun, (Ident(nme.SELECTOR_DUMMY)| Select(Ident(nme.SELECTOR_DUMMY), _)) :: Nil) - => Some(fun) + /*case Apply(fun, (Ident(nme.SELECTOR_DUMMY)| Select(Ident(nme.SELECTOR_DUMMY), _)) :: Nil) + => Some(fun)*/ case Apply(fun, _) => unapply(fun) case _ => None }