From f1e553ecfbea25bcc7e13294f6104d599336f460 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sat, 29 Aug 2015 21:42:29 +1000 Subject: SI-9029 Fix regression in extractor patterns The unified treatment of classical and named-based pattern matching does not correctly handle the generalization of "tuple capture". By "tuple capture", I mean: ``` scala> object Extractor { def unapply(a: Any): Option[(Int, String)] = Some((1, "2")) } defined object Extractor scala> "" match { case Extractor(x: Int, y: String) => } scala> "" match { case Extractor(xy : (Int, String)) => } warning: there was one deprecation warning; re-run with -deprecation for details scala> :warnings :9: warning: object Extractor expects 2 patterns to hold (Int, String) but crushing into 2-tuple to fit single pattern (SI-6675) "" match { case Extractor(xy : (Int, String)) => } ^ ``` Name based pattern matching, new in Scala 2.11, allows one to deconstruct the elements that structurally resembles `ProductN`: ``` scala> class P2(val _1: Int, val _2: String) defined class P2 scala> object Extractor { def unapply(a: Any): Option[P2] = Some(new P2(1, "2")) } defined object Extractor scala> "" match { case Extractor(x: Int, y: String) => } ``` However, attempting to extract the `P2` in its entirety leads to an internal error: ``` scala> "" match { case Extractor(p2: P2) => } :10: warning: fruitless type test: a value of type (Int, String) cannot also be a P2 "" match { case Extractor(p2: P2) => } ^ :10: error: error during expansion of this match (this is a scalac bug). The underlying error was: type mismatch; found : P2 required: (Int, String) "" match { case Extractor(p2: P2) => } ^ ``` Note that this match was legal and warning free in 2.10. This commit avoids the hard-coded assumption that the "tuple capture" results in a `TupleN`, and instead keeps track of the product-ish type from which we extracted the element types. I have also opted not to limit the deprecation warning to `TupleN` extractors. --- .../nsc/transform/patmat/PatternExpander.scala | 18 +++++++++++- .../transform/patmat/ScalacPatternExpanders.scala | 34 ++++++++++++---------- 2 files changed, 36 insertions(+), 16 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/transform/patmat/PatternExpander.scala b/src/compiler/scala/tools/nsc/transform/patmat/PatternExpander.scala index e84ccbf754..1916050dd8 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/PatternExpander.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/PatternExpander.scala @@ -86,9 +86,25 @@ trait PatternExpander[Pattern, Type] { * @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) { + final case class Extractor(whole: Type, fixed: List[Type], repeated: Repeated, typeOfSinglePattern: Type) { require(whole != NoType, s"expandTypes($whole, $fixed, $repeated)") + /** A pattern with arity-1 that doesn't match the arity of the Product-like result of the `get` method, + * will match that result in its entirety. Example: + * + * {{{ + * warning: there was one deprecation warning; re-run with -deprecation for details + * scala> object Extractor { def unapply(a: Any): Option[(Int, String)] = Some((1, "2")) } + * defined object Extractor + * + * scala> "" match { case Extractor(x: Int, y: String) => } + * + * scala> "" match { case Extractor(xy : (Int, String)) => } + * warning: there was one deprecation warning; re-run with -deprecation for details + * }}} + * */ + def asSinglePattern: Extractor = copy(fixed = List(typeOfSinglePattern)) + def productArity = fixed.length def hasSeq = repeated.exists def elementType = repeated.elementType diff --git a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala index b1783dc81f..55d4366a46 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala @@ -43,8 +43,9 @@ trait ScalacPatternExpanders { 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)) + def newExtractor(whole: Type, fixed: List[Type], repeated: Repeated, typeOfSinglePattern: Type): Extractor = + logResult(s"newExtractor($whole, $fixed, $repeated, $typeOfSinglePattern")(Extractor(whole, fixed, repeated, typeOfSinglePattern)) + def newExtractor(whole: Type, fixed: List[Type], repeated: Repeated): Extractor = newExtractor(whole, fixed, repeated, tupleType(fixed)) // Turn Seq[A] into Repeated(Seq[A], A, A*) def repeatedFromSeq(seqType: Type): Repeated = { @@ -74,16 +75,17 @@ trait ScalacPatternExpanders { * it was unapplySeq, so we have to funnel that information in separately. */ def unapplyMethodTypes(whole: Type, result: Type, isSeq: Boolean): Extractor = { - val expanded = ( - if (result =:= BooleanTpe) Nil - else typeOfMemberNamedGet(result) match { + if (result =:= BooleanTpe) newExtractor(whole, Nil, NoRepeated) + else { + val getResult = typeOfMemberNamedGet(result) + val expanded = getResult 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) + expanded match { + case init :+ last if isSeq => newExtractor(whole, init, repeatedFromSeq(last), getResult) + case tps => newExtractor(whole, tps, NoRepeated, getResult) + } } } } @@ -142,12 +144,14 @@ trait ScalacPatternExpanders { def acceptMessage = if (extractor.isErroneous) "" else s" to hold ${extractor.offeringString}" val requiresTupling = isUnapply && patterns.totalArity == 1 && productArity > 1 - if (requiresTupling && effectivePatternArity(args) == 1) { - val sym = sel.symbol.owner - currentRun.reporting.deprecationWarning(sel.pos, sym, s"${sym} expects $productArity patterns$acceptMessage but crushing into $productArity-tuple to fit single pattern (SI-6675)") - } - - val normalizedExtractor = if (requiresTupling) tupleExtractor(extractor) else extractor + val normalizedExtractor = if (requiresTupling) { + val tupled = extractor.asSinglePattern + if (effectivePatternArity(args) == 1 && isTupleType(extractor.typeOfSinglePattern)) { + val sym = sel.symbol.owner + currentRun.reporting.deprecationWarning(sel.pos, sym, s"${sym} expects $productArity patterns$acceptMessage but crushing into $productArity-tuple to fit single pattern (SI-6675)") + } + tupled + } else extractor validateAligned(context, fn, Aligned(patterns, normalizedExtractor)) } -- cgit v1.2.3 From daa421189d9191f05b5006418580eb6c0e0b1ec7 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 31 Aug 2015 13:44:54 +1000 Subject: SI-8989 Better error message for invalid extractor pattern --- .../transform/patmat/ScalacPatternExpanders.scala | 11 ++++++++--- test/files/neg/t4425b.check | 22 +++++++++++++++++----- test/files/neg/t8127a.check | 4 ++++ test/files/neg/t8127a.scala | 12 ++++++++++++ test/files/neg/t8989.check | 4 ++++ test/files/neg/t8989.scala | 14 ++++++++++++++ 6 files changed, 59 insertions(+), 8 deletions(-) create mode 100644 test/files/neg/t8127a.check create mode 100644 test/files/neg/t8127a.scala create mode 100644 test/files/neg/t8989.check create mode 100644 test/files/neg/t8989.scala (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala index 55d4366a46..5678c53e78 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala @@ -74,11 +74,16 @@ trait ScalacPatternExpanders { * Unfortunately the MethodType does not carry the information of whether * it was unapplySeq, so we have to funnel that information in separately. */ - def unapplyMethodTypes(whole: Type, result: Type, isSeq: Boolean): Extractor = { + def unapplyMethodTypes(context: Context, whole: Type, result: Type, isSeq: Boolean): Extractor = { if (result =:= BooleanTpe) newExtractor(whole, Nil, NoRepeated) else { val getResult = typeOfMemberNamedGet(result) + def noGetError() = { + val name = "unapply" + (if (isSeq) "Seq" else "") + context.error(context.tree.pos, s"The result type of an $name method must contain a member `get` to be used as an extractor pattern, no such member exists in ${result}") + } val expanded = getResult match { + case global.NoType => noGetError(); Nil case rawGet if !hasSelectors(rawGet) => rawGet :: Nil case rawGet => typesOfSelectors(rawGet) } @@ -131,8 +136,8 @@ trait ScalacPatternExpanders { val isUnapply = sel.symbol.name == nme.unapply val extractor = sel.symbol.name match { - case nme.unapply => unapplyMethodTypes(firstParamType(fn.tpe), sel.tpe, isSeq = false) - case nme.unapplySeq => unapplyMethodTypes(firstParamType(fn.tpe), sel.tpe, isSeq = true) + case nme.unapply => unapplyMethodTypes(context, firstParamType(fn.tpe), sel.tpe, isSeq = false) + case nme.unapplySeq => unapplyMethodTypes(context, firstParamType(fn.tpe), sel.tpe, isSeq = true) case _ => applyMethodTypes(fn.tpe) } diff --git a/test/files/neg/t4425b.check b/test/files/neg/t4425b.check index 8418b4fd12..a204467586 100644 --- a/test/files/neg/t4425b.check +++ b/test/files/neg/t4425b.check @@ -22,16 +22,28 @@ t4425b.scala:10: error: object X is not a case class, nor does it have an unappl Note: def unapply(x: String)(y: String): Nothing exists in object X, but it cannot be used as an extractor due to its second non-implicit parameter list println((X: Any) match { case X(_, _) => "ok" ; case _ => "fail" }) ^ -t4425b.scala:18: error: too many patterns for object X: expected 1, found 2 +t4425b.scala:18: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in Nothing + println( "" match { case _ X _ => "ok" ; case _ => "fail" }) + ^ +t4425b.scala:18: error: too many patterns for object X offering Boolean: expected 0, found 2 println( "" match { case _ X _ => "ok" ; case _ => "fail" }) ^ -t4425b.scala:19: error: too many patterns for object X: expected 1, found 2 +t4425b.scala:19: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in Nothing + println((X: Any) match { case _ X _ => "ok" ; case _ => "fail" }) + ^ +t4425b.scala:19: error: too many patterns for object X offering Boolean: expected 0, found 2 println((X: Any) match { case _ X _ => "ok" ; case _ => "fail" }) ^ -t4425b.scala:22: error: too many patterns for object X: expected 1, found 2 +t4425b.scala:20: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in Nothing + println( "" match { case X(_) => "ok" ; case _ => "fail" }) + ^ +t4425b.scala:21: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in Nothing + println((X: Any) match { case X(_) => "ok" ; case _ => "fail" }) + ^ +t4425b.scala:22: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in Nothing println( "" match { case X(_, _) => "ok" ; case _ => "fail" }) ^ -t4425b.scala:23: error: too many patterns for object X: expected 1, found 2 +t4425b.scala:23: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in Nothing println((X: Any) match { case X(_, _) => "ok" ; case _ => "fail" }) ^ t4425b.scala:31: error: too many patterns for object X offering Nothing: expected 1, found 2 @@ -46,4 +58,4 @@ t4425b.scala:35: error: too many patterns for object X offering Nothing: expecte t4425b.scala:36: error: too many patterns for object X offering Nothing: expected 1, found 2 println((X: Any) match { case X(_, _) => "ok" ; case _ => "fail" }) ^ -14 errors found +18 errors found diff --git a/test/files/neg/t8127a.check b/test/files/neg/t8127a.check new file mode 100644 index 0000000000..5a30574861 --- /dev/null +++ b/test/files/neg/t8127a.check @@ -0,0 +1,4 @@ +t8127a.scala:7: error: The result type of an unapplySeq method must contain a member `get` to be used as an extractor pattern, no such member exists in Seq[_$1] + case H(v) => + ^ +one error found diff --git a/test/files/neg/t8127a.scala b/test/files/neg/t8127a.scala new file mode 100644 index 0000000000..c05facdac1 --- /dev/null +++ b/test/files/neg/t8127a.scala @@ -0,0 +1,12 @@ +object H { + def unapplySeq(m: Any): Seq[_] = List("") +} + +object Test { + def unapply(m: Any) = m match { + case H(v) => + case _ => + } + // now: too many patterns for object H offering Boolean: expected 0, found 1 + // was: result type Seq[_$2] of unapplySeq defined in method unapplySeq in object H does not conform to Option[_] +} diff --git a/test/files/neg/t8989.check b/test/files/neg/t8989.check new file mode 100644 index 0000000000..4e89b862bd --- /dev/null +++ b/test/files/neg/t8989.check @@ -0,0 +1,4 @@ +t8989.scala:11: error: The result type of an unapply method must contain a member `get` to be used as an extractor pattern, no such member exists in A + val f = p match {case d(1) => true; case _ => false} + ^ +one error found diff --git a/test/files/neg/t8989.scala b/test/files/neg/t8989.scala new file mode 100644 index 0000000000..8ed6a901cd --- /dev/null +++ b/test/files/neg/t8989.scala @@ -0,0 +1,14 @@ +class A extends Product1[Int] { + def _1 = 1 + def isEmpty = false // used by scalac + def isDefined = !isEmpty // used by dotty + def canEqual(a: Any) = true +} + +object d{ + def unapply(a: Any) = new A + val p: Any = ??? + val f = p match {case d(1) => true; case _ => false} +} + + -- cgit v1.2.3 From 955ce9f92b8611cacd355cabe8b4c1ae6ee0ea05 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 31 Aug 2015 13:52:29 +1000 Subject: SI-8127 Remove dead code in Uncurry It remains from the days of yore, when patterns survived this long in the compiler. --- src/compiler/scala/tools/nsc/transform/UnCurry.scala | 7 ------- 1 file changed, 7 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/transform/UnCurry.scala b/src/compiler/scala/tools/nsc/transform/UnCurry.scala index 163c44822e..ee7c839de9 100644 --- a/src/compiler/scala/tools/nsc/transform/UnCurry.scala +++ b/src/compiler/scala/tools/nsc/transform/UnCurry.scala @@ -475,13 +475,6 @@ abstract class UnCurry extends InfoTransform withNeedLift(needLift = true) { super.transform(tree) } else super.transform(tree) - case UnApply(fn, args) => - val fn1 = transform(fn) - val args1 = fn.symbol.name match { - case nme.unapplySeq => transformArgs(tree.pos, fn.symbol, args, patmat.alignPatterns(global.typer.context, tree).expectedTypes) - case _ => args - } - treeCopy.UnApply(tree, fn1, args1) case Apply(fn, args) => val needLift = needTryLift || !fn.symbol.isLabel // SI-6749, no need to lift in args to label jumps. -- cgit v1.2.3 From 3b396884e6c153588ba25123d82eb4ed703fd844 Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Mon, 21 Sep 2015 21:42:55 +1000 Subject: SI-9029 Remove unused method --- .../scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala | 5 ----- 1 file changed, 5 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala index 5678c53e78..d4f44303bb 100644 --- a/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala +++ b/src/compiler/scala/tools/nsc/transform/patmat/ScalacPatternExpanders.scala @@ -95,11 +95,6 @@ trait ScalacPatternExpanders { } } 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(context: Context, tree: Tree, aligned: Aligned): Aligned = { import aligned._ -- cgit v1.2.3