summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdriaan Moors <adriaan.moors@epfl.ch>2012-04-24 15:59:58 +0200
committerAdriaan Moors <adriaan.moors@epfl.ch>2012-04-24 17:43:32 +0200
commit2890714d7bcdd55b7a62091dcdf031cc3efe0822 (patch)
tree8b541f1633a4ec36b5a5e52f1a0f7bf0c2266448
parentd9740f82a3aea3b223233776cd7c9460a2e4dfc3 (diff)
downloadscala-2890714d7bcdd55b7a62091dcdf031cc3efe0822.tar.gz
scala-2890714d7bcdd55b7a62091dcdf031cc3efe0822.tar.bz2
scala-2890714d7bcdd55b7a62091dcdf031cc3efe0822.zip
generate well-formed patterns in parser
val pattern: Type = expr desugared to val x = expr match { case pattern: Type => (var_1, ..., var_N)} but `pattern: Type` is only a valid pattern if `pattern` is an Ident thus, we desugar to val x = (expr: Type) match { case pattern => (var_1, ..., var_N)} ... in this case (see def makePatDef) also, MaybeBoundType now fails on illegal patterns (to defend against similar regressions)
-rw-r--r--src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala22
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/PatMatVirtualiser.scala9
-rw-r--r--test/files/run/virtpatmat_valdef.check1
-rw-r--r--test/files/run/virtpatmat_valdef.scala6
4 files changed, 32 insertions, 6 deletions
diff --git a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
index 3a6e26d3b5..de7e6f9c7a 100644
--- a/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
+++ b/src/compiler/scala/tools/nsc/ast/parser/TreeBuilder.scala
@@ -521,11 +521,29 @@ abstract class TreeBuilder {
// val/var x_1 = t$._1
// ...
// val/var x_N = t$._N
- val pat1 = patvarTransformer.transform(pat)
+
+ val rhsUnchecked = gen.mkUnchecked(rhs)
+
+ // TODO: clean this up -- there is too much information packked into makePatDef's `pat` argument
+ // when it's a simple identifier (case Some((name, tpt)) -- above),
+ // pat should have the type ascription that was specified by the user
+ // however, in `case None` (here), we must be careful not to generate illegal pattern trees (such as `(a, b): Tuple2[Int, String]`)
+ // i.e., this must hold: pat1 match { case Typed(expr, tp) => assert(expr.isInstanceOf[Ident]) case _ => }
+ // if we encounter such an erroneous pattern, we strip off the type ascription from pat and propagate the type information to rhs
+ val (pat1, rhs1) = patvarTransformer.transform(pat) match {
+ // move the Typed ascription to the rhs
+ case Typed(expr, tpt) if !expr.isInstanceOf[Ident] =>
+ val rhsTypedUnchecked =
+ if (tpt.isEmpty) rhsUnchecked
+ else Typed(rhsUnchecked, tpt) setPos (rhs.pos union tpt.pos)
+ (expr, rhsTypedUnchecked)
+ case ok =>
+ (ok, rhsUnchecked)
+ }
val vars = getVariables(pat1)
val matchExpr = atPos((pat1.pos union rhs.pos).makeTransparent) {
Match(
- gen.mkUnchecked(rhs),
+ rhs1,
List(
atPos(pat1.pos) {
CaseDef(pat1, EmptyTree, makeTupleTerm(vars map (_._1) map Ident, true))
diff --git a/src/compiler/scala/tools/nsc/typechecker/PatMatVirtualiser.scala b/src/compiler/scala/tools/nsc/typechecker/PatMatVirtualiser.scala
index 02e53cb624..3be8397a9c 100644
--- a/src/compiler/scala/tools/nsc/typechecker/PatMatVirtualiser.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/PatMatVirtualiser.scala
@@ -260,10 +260,11 @@ trait PatMatVirtualiser extends ast.TreeDSL { self: Analyzer =>
* @arg patBinder symbol used to refer to the result of the previous pattern's extractor (will later be replaced by the outer tree with the correct tree to refer to that patterns result)
*/
def unapply(tree: Tree): Option[(Symbol, Type)] = tree match {
- case Bound(subpatBinder, typed@Typed(expr, tpt)) if typed.tpe ne null => Some((subpatBinder, typed.tpe))
- case Bind(_, typed@Typed(expr, tpt)) if typed.tpe ne null => Some((patBinder, typed.tpe))
- case Typed(expr, tpt) if tree.tpe ne null => Some((patBinder, tree.tpe))
- case _ => None
+ // the Ident subpattern can be ignored, subpatBinder or patBinder tell us all we need to know about it
+ case Bound(subpatBinder, typed@Typed(Ident(_), tpt)) if typed.tpe ne null => Some((subpatBinder, typed.tpe))
+ case Bind(_, typed@Typed(Ident(_), tpt)) if typed.tpe ne null => Some((patBinder, typed.tpe))
+ case Typed(Ident(_), tpt) if tree.tpe ne null => Some((patBinder, tree.tpe))
+ case _ => None
}
}
diff --git a/test/files/run/virtpatmat_valdef.check b/test/files/run/virtpatmat_valdef.check
new file mode 100644
index 0000000000..1a45335bd2
--- /dev/null
+++ b/test/files/run/virtpatmat_valdef.check
@@ -0,0 +1 @@
+meh(true,null)
diff --git a/test/files/run/virtpatmat_valdef.scala b/test/files/run/virtpatmat_valdef.scala
new file mode 100644
index 0000000000..f1a9b46cdd
--- /dev/null
+++ b/test/files/run/virtpatmat_valdef.scala
@@ -0,0 +1,6 @@
+object Test extends App {
+ // patterns in valdefs...
+ // TODO: irrefutability should indicate we don't actually need to test, just deconstruct
+ val (modified, result) : (Boolean, String) = (true, null)
+ println("meh"+ (modified, result))
+} \ No newline at end of file