summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2011-08-07 04:38:42 +0000
committerPaul Phillips <paulp@improving.org>2011-08-07 04:38:42 +0000
commit6fe5754cec7f389477e5aa29ef527916d5d615d0 (patch)
treea50c8d472582656d346e7a422c4599d8a04b2a68 /src
parent2b31bc81adfe6470e7da4546a4858482e8a5b61f (diff)
downloadscala-6fe5754cec7f389477e5aa29ef527916d5d615d0.tar.gz
scala-6fe5754cec7f389477e5aa29ef527916d5d615d0.tar.bz2
scala-6fe5754cec7f389477e5aa29ef527916d5d615d0.zip
Better error message for case class/object matc...
Better error message for case class/object match confusion. Closes SI-4879, no review.
Diffstat (limited to 'src')
-rw-r--r--src/compiler/scala/tools/nsc/typechecker/Infer.scala26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Infer.scala b/src/compiler/scala/tools/nsc/typechecker/Infer.scala
index b70f20ea89..354eb52913 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Infer.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Infer.scala
@@ -1540,8 +1540,30 @@ trait Infer {
val pt1 = pt.instantiateTypeParams(ptparams, ptvars)
if (pat.tpe <:< pt1)
ptvars foreach instantiateTypeVar
- else
- error(pat.pos, "pattern type is incompatible with expected type"+foundReqMsg(pat.tpe, pt))
+ else {
+ val sym = pat.tpe.typeSymbol
+ val clazz = sym.companionClass
+ val addendum = (
+ if (sym.isModuleClass && clazz.isCaseClass && (clazz isSubClass pt1.typeSymbol)) {
+ // TODO: move these somewhere reusable.
+ val typeString = clazz.typeParams match {
+ case Nil => "" + clazz.name
+ case xs => xs map (_ => "_") mkString (clazz.name + "[", ",", "]")
+ }
+ val caseString = (
+ clazz.caseFieldAccessors
+ map (_ => "_") // could use the actual param names here
+ mkString (clazz.name + "(", ",", ")")
+ )
+ (
+ "\nNote: if you intended to match against the class, try `case _: " +
+ typeString + "` or `case " + caseString + "`"
+ )
+ }
+ else ""
+ )
+ error(pat.pos, "pattern type is incompatible with expected type"+foundReqMsg(pat.tpe, pt) + addendum)
+ }
}
object toOrigin extends TypeMap {