Skip to content

Add error message for wrong number of argument patterns #3508

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
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 @@ -114,6 +114,7 @@ public enum ErrorMessageID {
TraitIsExpectedID,
TraitRedefinedFinalMethodFromAnyRefID,
PackageNameAlreadyDefinedID,
UnapplyInvalidNumberOfArgumentsID,
;

public int errorNumber() {
Expand Down
13 changes: 13 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1929,4 +1929,17 @@ object messages {
val explanation =
hl"An ${"object"} cannot have the same name as an existing ${"package"}. Rename either one of them."
}

case class UnapplyInvalidNumberOfArguments(qual: untpd.Tree, argTypes: List[Type])(implicit ctx: Context)
extends Message(UnapplyInvalidNumberOfArgumentsID) {
val kind = "Syntax"
val msg = hl"Wrong number of argument patterns for $qual; expected: ($argTypes%, %)"
val explanation =
hl"""The Unapply method of $qual was used with incorrect number of arguments.
|Expected usage would be something like:
|case $qual(${argTypes.map(_ => '_')}%, %) => ...
|
|where subsequent arguments would have following types: ($argTypes%, %).
|""".stripMargin
}
}
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotc
package typer

import core._
import ast.{Trees, untpd, tpd, TreeInfo}
import ast.{TreeInfo, Trees, tpd, untpd}
import util.Positions._
import util.Stats.track
import Trees.Untyped
Expand All @@ -26,13 +26,14 @@ import EtaExpansion._
import Inferencing._

import collection.mutable
import config.Printers.{typr, unapp, overload}
import config.Printers.{overload, typr, unapp}
import TypeApplications._

import language.implicitConversions
import reporting.diagnostic.Message
import reporting.trace
import Constants.{Constant, IntTag, LongTag}
import dotty.tools.dotc.reporting.diagnostic.messages.UnapplyInvalidNumberOfArguments

import scala.collection.mutable.ListBuffer

Expand Down Expand Up @@ -969,7 +970,7 @@ trait Applications extends Compatibility { self: Typer with Dynamic =>
case _ => args
}
if (argTypes.length != bunchedArgs.length) {
ctx.error(em"wrong number of argument patterns for $qual; expected: ($argTypes%, %)", tree.pos)
ctx.error(UnapplyInvalidNumberOfArguments(qual, argTypes), tree.pos)
argTypes = argTypes.take(args.length) ++
List.fill(argTypes.length - args.length)(WildcardType)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1189,7 +1189,6 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("method wait", method.show)
}


@Test def packageNameAlreadyDefined =
checkMessagesAfter("frontend") {
"""
Expand All @@ -1203,4 +1202,26 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val PackageNameAlreadyDefined(pkg) = messages.head
assertEquals(pkg.show, "object bar")
}

@Test def unapplyInvalidNumberOfArguments =
checkMessagesAfter("frontend") {
"""
|case class Boo(a: Int, b: String)
|
|object autoTuplingNeg2 {
| val z = Boo(1, "foo")
|
| z match {
| case Boo(a, b, c) => a
| }
|}
""".stripMargin
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val UnapplyInvalidNumberOfArguments(qual, argTypes) :: Nil = messages
assertEquals("Boo", qual.show)
assertEquals("(class Int, class String)", argTypes.map(_.typeSymbol).mkString("(", ", ", ")"))
}
}