Skip to content

Add error message for missing unapply on pattern matching #5180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ public enum ErrorMessageID {
TypeTestAlwaysSucceedsID,
TermMemberNeedsNeedsResultTypeForImplicitSearchID,
CaseClassCannotExtendEnumID,
ValueClassParameterMayNotBeCallByNameID
ValueClassParameterMayNotBeCallByNameID,
NotAnExtractorID
;

public int errorNumber() {
Expand Down
14 changes: 14 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2117,4 +2117,18 @@ object messages {
override def msg: String = hl"""normal case class cannot extend an enum. case $cls in ${cls.owner} is extending enum ${parent.name}."""
override def explanation: String = ""
}

case class NotAnExtractor(tree: untpd.Tree)(implicit ctx: Context) extends Message(NotAnExtractorID) {
override def msg: String = hl"$tree cannot be used as an extractor in a pattern because it lacks an unapply or unapplySeq method"
override def kind: String = "Syntax"
override def explanation: String =
hl"""|An `unapply` method should be defined in an `object` as follow:
| - If it is just a test, return a `Boolean`. For example `case even()`
| - If it returns a single sub-value of type T, return an `Option[T]`
| - If it returns several sub-values T1,...,Tn, group them in an optional tuple `Option[(T1,...,Tn)]`
|
|Sometimes, the number of sub-values isn’t fixed and we would like to return a sequence.
|For this reason, you can also define patterns through `unapplySeq` which returns `Option[Seq[T]]`.
|This mechanism is used for instance in pattern `case List(x1, ..., xn)`""".stripMargin
}
}
5 changes: 2 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import language.implicitConversions
import reporting.diagnostic.Message
import reporting.trace
import Constants.{Constant, IntTag, LongTag}
import dotty.tools.dotc.reporting.diagnostic.messages.UnapplyInvalidNumberOfArguments
import dotty.tools.dotc.reporting.diagnostic.messages.{NotAnExtractor, UnapplyInvalidNumberOfArguments}

import scala.collection.mutable.ListBuffer

Expand Down Expand Up @@ -895,8 +895,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
def typedUnApply(tree: untpd.Apply, selType: Type)(implicit ctx: Context): Tree = track("typedUnApply") {
val Apply(qual, args) = tree

def notAnExtractor(tree: Tree) =
errorTree(tree, s"${qual.show} cannot be used as an extractor in a pattern because it lacks an unapply or unapplySeq method")
def notAnExtractor(tree: Tree) = errorTree(tree, NotAnExtractor(qual))

/** If this is a term ref tree, try to typecheck with its type name.
* If this refers to a type alias, follow the alias, and if
Expand Down
16 changes: 16 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1511,4 +1511,20 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals(tailRegMessages, Set("variable", "value", "object", "class"))
}

@Test def notAnExtractor() =
checkMessagesAfter(FrontEnd.name) {
"""
| class Foo
| object Test {
| def test(foo: Foo) = foo match {
| case Foo(name) => ???
| }
| }
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val NotAnExtractor(tree) = messages.head
assertEquals("Foo", tree.show)
}
}